Mailboxes can now be found by their names.

This commit is contained in:
pjht 2019-09-05 09:39:13 -05:00
parent fa60cba73a
commit f88622c154
9 changed files with 84 additions and 10 deletions

View File

@ -99,6 +99,7 @@ int main() {
uint32_t datapos=find_loc("vfs",initrd);
load_task(datapos,initrd);
yield(); // Bochs fails here
rescan_vfs();
datapos=find_loc("devfs",initrd);
load_task(datapos,initrd);
yieldToPID(3);

View File

@ -243,6 +243,8 @@ void isr_handler(registers_t r) {
memcpy((char*)r.ebx,initrd,initrd_sz);
} else if (r.eax==20) {
r.ebx=(pid_t)getPID();
} else if (r.eax==21) {
r.ebx=kernel_mailbox_find_by_name((char*)r.ebx);
}
break;
}

View File

@ -90,3 +90,12 @@ void kernel_mailbox_get_msg(uint32_t box, Message* recv_msg, uint32_t buffer_sz)
serial_printf("Box %s got a message from box %s.\n",mailboxes[box].name,mailboxes[recv_msg->from].name);
mailboxes[box]=mailbox;
}
uint32_t kernel_mailbox_find_by_name(char* name) {
for (uint32_t i=1;i<next_box;i++) {
if (strcmp(mailboxes[i].name,name)==0) {
return i;
}
}
return 0;
}

View File

@ -8,5 +8,6 @@ uint32_t kernel_mailbox_new(uint16_t size,char* name);
void kernel_mailbox_free(uint32_t box);
void kernel_mailbox_send_msg(Message* user_msg);
void kernel_mailbox_get_msg(uint32_t box, Message* recv_msg, uint32_t buffer_sz);
uint32_t kernel_mailbox_find_by_name(char* name);
#endif

View File

@ -23,3 +23,13 @@ void mailbox_get_msg(uint32_t box, Message* recv_msg, uint32_t buffer_sz) {
int $80; \
"::"b"(box),"c"(recv_msg),"d"(buffer_sz));
}
uint32_t mailbox_find_by_name(char* name) {
uint32_t box;
asm volatile(" \
mov $21, %%eax; \
int $80; \
":"=b"(box):"b"(name));
return box;
}

View File

@ -7,11 +7,11 @@
#include <dbg.h>
#include <limits.h>
#include <unistd.h>
#define VFS_MBOX 3
#define VFS_PID 2
static uint32_t box;
static uint32_t vfs_box;
FILE* __stdio_stdin;
FILE* __stdio_stdout;
FILE* __stdio_stderr;
@ -30,6 +30,10 @@ void __stdio_init() {
*__stdio_stdout=1;
__stdio_stderr=malloc(sizeof(FILE*));
*__stdio_stderr=2;
vfs_box=mailbox_find_by_name("vfs");
if (vfs_box==0) {
serial_print("Cannot find VFS box\n");
}
}
static vfs_message* make_msg(vfs_message_type type,const char* mode,const char* path, uint32_t fd, int data) {
@ -50,13 +54,17 @@ static vfs_message* make_msg(vfs_message_type type,const char* mode,const char*
}
FILE* fopen(char* filename,char* mode) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return NULL;
}
if (strlen(filename)>4096 || strlen(mode)>10) {
return NULL;
}
vfs_message* msg_data=make_msg(VFS_OPEN,mode,filename,0,0);
Message msg;
msg.from=box;
msg.to=VFS_MBOX;
msg.to=vfs_box;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
@ -81,6 +89,10 @@ FILE* fopen(char* filename,char* mode) {
int putc(int c, FILE* stream) __attribute__ ((alias ("fputc")));
int fputc(int c, FILE* stream) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return EOF;
}
char str[]={c,'\0'};
if (fputs(str,stream)==0) {
return c;
@ -93,6 +105,10 @@ int fputc(int c, FILE* stream) {
int getc(FILE* stream) __attribute__ ((alias ("fgetc")));
int fgetc(FILE* stream) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return EOF;
}
char c[2];
if (fgets(&c[0],1,stream)==NULL) {
return EOF;
@ -107,10 +123,14 @@ char* gets(char* s) {
}
char* fgets(char* str,int count,FILE* stream) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return NULL;
}
vfs_message* msg_data=make_msg(VFS_GETS,NULL,NULL,*stream,count);
Message msg;
msg.from=box;
msg.to=VFS_MBOX;
msg.to=vfs_box;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
@ -140,12 +160,16 @@ char* fgets(char* str,int count,FILE* stream) {
}
size_t fread(void* buffer_ptr,size_t size,size_t count,FILE* stream) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return 0;
}
char* buffer=(char*)buffer_ptr;
size_t bytes=size*count;
vfs_message* msg_data=make_msg(VFS_GETS,NULL,NULL,*stream,bytes);
Message msg;
msg.from=box;
msg.to=VFS_MBOX;
msg.to=vfs_box;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
@ -174,10 +198,14 @@ size_t fread(void* buffer_ptr,size_t size,size_t count,FILE* stream) {
}
int fputs(const char* s, FILE* stream) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return EOF;
}
vfs_message* msg_data=make_msg(VFS_PUTS,NULL,NULL,*stream,strlen(s));
Message msg;
msg.from=box;
msg.to=VFS_MBOX;
msg.to=vfs_box;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
@ -204,12 +232,16 @@ int fputs(const char* s, FILE* stream) {
}
size_t fwrite(void* buffer_ptr,size_t size,size_t count,FILE* stream) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return 0;
}
char* buffer=(char*)buffer_ptr;
size_t bytes=size*count;
vfs_message* msg_data=make_msg(VFS_PUTS,NULL,NULL,*stream,bytes);
Message msg;
msg.from=box;
msg.to=VFS_MBOX;
msg.to=vfs_box;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
@ -236,10 +268,14 @@ size_t fwrite(void* buffer_ptr,size_t size,size_t count,FILE* stream) {
}
void register_fs(const char* name,uint32_t mbox) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return;
}
vfs_message* msg_data=make_msg(VFS_REGISTER_FS,name,NULL,mbox,0);
Message msg;
msg.from=box;
msg.to=VFS_MBOX;
msg.to=vfs_box;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
@ -255,10 +291,14 @@ void register_fs(const char* name,uint32_t mbox) {
}
void mount(char* file,char* type,char* path) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return;
}
vfs_message* msg_data=make_msg(VFS_MOUNT,type,path,0,strlen(file)+1);
Message msg;
msg.from=box;
msg.to=VFS_MBOX;
msg.to=vfs_box;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
@ -276,6 +316,10 @@ void mount(char* file,char* type,char* path) {
}
int vfprintf(FILE* stream,const char* format,va_list arg) {
if (vfs_box==0) {
serial_print("The VFS box has not been found\n");
return EOF;
}
int c;
for(;*format!='\0';format++) {
if(*format!='%') {
@ -363,3 +407,7 @@ int printf(const char* format,...) {
return EOF;
}
}
void rescan_vfs() {
vfs_box=mailbox_find_by_name("vfs");
}

View File

@ -21,5 +21,6 @@ typedef struct {
uint32_t mailbox_new(uint16_t size,char* name);
void mailbox_send_msg(Message* msg);
void mailbox_get_msg(uint32_t box, Message* recv_msg, uint32_t buffer_sz);
uint32_t mailbox_find_by_name(char* name);
#endif

View File

@ -40,5 +40,6 @@ int feof(FILE* stream);
int ferror(FILE* stream);
int fflush(FILE *stream); // GCC required
void setbuf(FILE *restrict stream, char *restrict buf); // GCC required
void rescan_vfs();
#endif

View File

@ -9,11 +9,12 @@
#include "vga.h"
#include <dbg.h>
#define DEVFS_BOX 6
uint32_t devfs_box;
#define DEVFS_PID 3
int main() {
uint32_t box=mailbox_new(16,"vga");
devfs_box=mailbox_find_by_name("devfs_devfs");
text_fb_info info;
info.address=map_phys((void*)0xB8000,10);
info.width=80;
@ -27,7 +28,7 @@ int main() {
strcpy(&msg_data->name[0],"vga");
Message msg;
msg.from=box;
msg.to=DEVFS_BOX;
msg.to=devfs_box;
msg.size=sizeof(devfs_message);
msg.msg=msg_data;
mailbox_send_msg(&msg);