Implement basis of fides_file

master
rubenwardy 2017-03-31 15:58:51 +01:00
parent 2672528849
commit 21891004c8
9 changed files with 187 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "blockedqueue.h"
#include "fides.h"
#include "fides_pipe.h"
#include "fides_file.h"
#include "fides_terminal.h"
#include "utils.h"
@ -134,3 +135,30 @@ void blockedqueue_checkForBlockedInReads() {
}
}
}
void blockedqueue_checkForBlockedFile(u32 fid) {
for (int i = 0; i < MAX_PROCESSES; i++) {
BlockedProcess *blocked = &blockedProcesses[i];
if (blocked->pid > 0 && blocked->reason == BLOCKED_FILE) {
FiDes *fides = fides_get(blocked->pid, blocked->fid);
if (fides && fides->id == fid && fides_file_is_file(fides)) {
size_t res = fides->read(fides, blocked->ret2, blocked->meta1);
if (res == SIZE_MAX) {
printError("[c4bf] THIS SHOULD NEVER HAPPEN");
} else {
printLine("[c4bf] unblocking process");
pcb_t *pcb = processes_get(processes_findByPID(blocked->pid));
if (pcb) {
blocked->pid = 0;
pcb->ctx.gpr[0] = res;
processes_unblockProcess(pcb);
} else {
printError("[c4bf] Program does not exist!");
}
}
} else {
printLine("[c4bf] Is not file fides, or not right file fides");
}
}
}
}

View File

@ -20,5 +20,6 @@ extern void blockedqueue_addFileRead(pid_t pid, u32 fid, char *x, int max);
extern BlockedProcess *blockedqueue_popNextProcessExit(pid_t pid, pid_t parent);
extern void blockedqueue_checkForBlockedPipes(u32 pipe_id);
extern void blockedqueue_checkForBlockedInReads();
extern void blockedqueue_checkForBlockedFileOpen(u32 fid);
#endif

92
kernel/fides_file.c Normal file
View File

@ -0,0 +1,92 @@
#include "fides_file.h"
#include "blockedqueue.h"
#include "utils.h"
#include "fs/fs.h"
#include "fs/blocks.h"
typedef struct {
u32 id;
INode inode;
int ptr;
char data[256];
} FidesFileEntry;
#define MAX_FIDES_FILES 10
FidesFileEntry _fides_files[MAX_FIDES_FILES];
int fides_files_count;
void fides_file_init() {
fides_files_count = 0;
memset(&_fides_files[0], 0, sizeof(FidesFileEntry) * MAX_FIDES_FILES);
}
FidesFileEntry *_fides_file_allocateEntry() {
for (size_t i = 0; i < MAX_FIDES_FILES; i++) {
if (_fides_files[i].id == 0) {
_fides_files[i].id = ++fides_files_count;
_fides_files[i].inode.id = 0;
return &_fides_files[i];
}
}
return NULL;
}
size_t fides_file_read(FiDes *node, char *data, size_t max) {
return 0;
}
size_t fides_file_write(FiDes *node, const char *data, size_t len) {
return 0;
}
void fides_file_grab(FiDes *node) {
// TODO: unimplemented
}
void fides_file_drop(FiDes *node) {
// TODO: unimplemented
}
void _fides_file_handle_read_data(u32 block_num, char *resp, void *meta) {
FidesFileEntry *entry = (FidesFileEntry*)meta;
if (!entry) {
printError("[FidesFile] Unable to get entry linked to inode! rdata");
return;
}
memcpy(&entry->data[0], resp, fs_blocks_getBlockSize());
}
void _fides_file_handle_fetch_inode(INode *inode, void *meta) {
FidesFileEntry *entry = (FidesFileEntry*)meta;
if (!entry) {
printError("[FidesFile] Unable to get entry linked to inode! rinode");
return;
}
printError("Fetched INode!");
memcpy(&entry->inode, inode, sizeof(INode));
fs_blocks_readBlock(inode->block_num, &_fides_file_handle_read_data, meta);
}
void fides_file_create(FiDes *one, char *path, char mode) {
if (mode == 'r') {
one->read = &fides_file_read;
} else {
one->write = &fides_file_write;
}
one->grab = &fides_file_grab;
one->drop = &fides_file_drop;
FidesFileEntry *entry = _fides_file_allocateEntry();
one->data = entry->id;
fs_fetchINode(path, &_fides_file_handle_fetch_inode, (void*)entry);
}
int fides_file_is_file(FiDes *fides) {
return fides->read == &fides_file_read || fides->write == &fides_file_write;
}

9
kernel/fides_file.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef FIDES_FILE_H
#define FIDES_FILE_H
#include "fides.h"
extern void fides_file_init();
extern void fides_file_create(FiDes *one, char *path, char mode);
extern int fides_file_is_file(FiDes *fides);
#endif

View File

@ -80,6 +80,13 @@ void _fs_handle_readSuperBlock(u32 block_num, char *resp, void *meta) {
inode_set_perms(&inode, p, p, p);
_fs_writeINode(1, &inode);
u32 blockSize = fs_blocks_getBlockSize();
char mem[blockSize];
char *dat = "File content";
memcpy(&mem[0], dat, strlen(dat) * sizeof(char));
memset(&mem[strlen(dat)], 0, blockSize - 12);
fs_blocks_writeBlock(2, mem, NULL, NULL);
} else {
printError("[Fs] Found filesystem super block");
_fs_index_modified = false;

View File

@ -3,6 +3,7 @@
#include "blockedqueue.h"
#include "fides.h"
#include "fides_pipe.h"
#include "fides_file.h"
#include "fides_terminal.h"
#include "fs/fs.h"
#include "utils.h"
@ -74,6 +75,8 @@ void hilevel_handler_rst(ctx_t *ctx) {
fides_init();
fides_pipe_init();
fs_init();
fides_file_init();
processes_start(PRIORITY_NORMAL, 0x50, getProgramInstAddress("console"));
printLine(" - Setting current & ctx");
@ -476,6 +479,26 @@ u32 svc_handle_setpriority(ctx_t *ctx, pcb_t *current) {
return 0;
}
u32 svc_handle_fopen(ctx_t *ctx, pcb_t *current) {
kprint(" - fopen ");
char *path = (char*)(ctx->gpr[0]);
char mode = (char )(ctx->gpr[1]);
kprint(path);
kprint(" mode=");
PL011_putc(UART0, mode, true);
kprint("\n");
FiDes *fides = fides_create(current->pid, current->fid_counter);
if (fides) {
fides_file_create(fides, path, mode);
return fides->id;
}
return -1;
}
//
// Handle system calls
//
@ -492,6 +515,7 @@ u32 svc_handle_setpriority(ctx_t *ctx, pcb_t *current) {
#define SYS_DUP2 ( 0x10 )
#define SYS_FD_SETBLOCK ( 0x11 )
#define SYS_SETPRIORITY ( 0x12 )
#define SYS_FOPEN ( 0x13 )
void hilevel_handler_svc(ctx_t *ctx, u32 id) {
printLine("SVC");
@ -548,6 +572,9 @@ void hilevel_handler_svc(ctx_t *ctx, u32 id) {
case SYS_SETPRIORITY:
ctx->gpr[0] = svc_handle_setpriority(ctx, current);
break;
case SYS_FOPEN:
ctx->gpr[0] = svc_handle_fopen(ctx, current);
break;
case SYS_KILL:
ctx->gpr[0] = svc_handle_kill(ctx, current);
break;

View File

@ -3,6 +3,7 @@
#include <stdint.h>
#include "libc.h"
#include "stdio.h"
uint32_t gcd( uint32_t x, uint32_t y ) {
if ( x == y ) {
@ -17,6 +18,12 @@ uint32_t gcd( uint32_t x, uint32_t y ) {
}
void main_P4() {
int fd = fopen("a.txt", "r");
if (fd < 0) {
printf("FDError!\n");
}
write( STDOUT_FILENO, "P4", 2 );
exit( EXIT_SUCCESS );
while( 1 ) {

View File

@ -219,6 +219,19 @@ int fd_setblock(int fd, int isblocking) {
return r;
}
int fopen(char *path, char mode) {
int r;
asm volatile( "mov r0, %2 \n" // assign r0 = pid
"mov r1, %3 \n" // assign r0 = pid
"svc %1 \n" // make system call SYS_CLOSE
"mov %0, r0 \n" // assign r = r0
: "=r" (r)
: "I" (SYS_FOPEN), "r" (path), "r" (mode)
: "r0" );
return r;
}
int setpriority(int who, int prio) {
int r;
asm volatile( "mov r0, %2 \n" // assign r0 = pid

View File

@ -36,6 +36,7 @@ typedef int pid_t;
#define SYS_DUP2 ( 0x10 )
#define SYS_FD_SETBLOCK ( 0x11 )
#define SYS_SETPRIORITY ( 0x12 )
#define SYS_FOPEN ( 0x13 )
#define SIG_TERM ( 0x00 )
#define SIG_QUIT ( 0x01 )
@ -82,6 +83,8 @@ extern int fd_setblock(int fd, int isblocking);
extern int setpriority(int who, int prio);
extern int fopen(char *path, char mode);
// signal process identified by pid with signal x
extern int kill( pid_t pid, int x );