Skip to content

Commit

Permalink
syscall: cleanup interface, registration and argument parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Mahavir Jain <[email protected]>
  • Loading branch information
mahavirj committed Mar 11, 2017
1 parent 630e1c1 commit e5e918b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 34 deletions.
6 changes: 6 additions & 0 deletions boot/syscall.s
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ execve:
mov eax, 2
int 64
ret

[GLOBAL read]
sleep:
mov eax, 3
int 64
ret
20 changes: 14 additions & 6 deletions drivers/vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string.h>
#include <helper.h>
#include <vm.h>
#include <syscall.h>

extern void memsetw(void *src, uint16_t value, size_t size);

Expand Down Expand Up @@ -65,7 +66,7 @@ static void cls()
move_cursor(_x, _y);
}

void vga_write_char(char c)
void sys_write_char(char c)
{
uint16_t *loc = fb + (_y * 80 + _x);
uint8_t attr = COLOR_ATTR;
Expand Down Expand Up @@ -99,14 +100,21 @@ void vga_write_char(char c)
move_cursor(_x, _y);
}

void vga_write_buf(char *buf, int len)
int sys_write()
{
const char *buf;
int len;

argstr(1, (char **) &buf);
argint(2, &len);

if (!buf || !len)
return;
return 0;

while (len--)
vga_write_char(*buf++);
return;
int i;
for (i = 0; i < len; i++)
sys_write_char(buf[i]);
return len;
}

void init_vga()
Expand Down
4 changes: 2 additions & 2 deletions include/drivers/vga.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define __VGA_H__

void init_vga();
void vga_write_char(char ch);
void vga_write_buf(char *buf, int len);
void sys_write_char(char ch);
int sys_write(char *buf, int len);
int printk(const char *fmt, ...);

#endif /* __VGA_H__ */
8 changes: 8 additions & 0 deletions include/kernel/syscall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __SYSCALL_H__
#define __SYSCALL_H__

void syscall_handler(registers_t *r);
int argint(int n, int *p);
int argstr(int n, char **p);

#endif /* __SYSCALL_H__ */
2 changes: 1 addition & 1 deletion include/kernel/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void init_scheduler();
void tiny_schedule(void);
int create_init_task(void);
int sys_fork(void);
int sys_exec(const char *fname);
int sys_exec(void);
void swtch(struct context **old, struct context *new);
void yield();
void sched();
Expand Down
21 changes: 1 addition & 20 deletions kernel/isr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <vga.h>
#include <helper.h>
#include <task.h>
#include <syscall.h>

static void *irq_handlers[256];

Expand Down Expand Up @@ -73,26 +74,6 @@ void irq_uninstall_handler(int irq)
irq_handlers[irq] = 0;
}

static void syscall_handler(registers_t *r)
{
/* write system call */
if (r->eax == 0) {
/* First argument is fd, ignore for now */
/* buffer to print on VGA */
char *buf = (char *) (*(int *)(r->useresp + 8));
/* length of buffer */
int len = *(int *) (r->useresp + 12);
vga_write_buf(buf, len);
/* fork system call */
} else if (r->eax == 1) {
/* FIXME: do we need to set current task `irqf` to `r` here? */
r->eax = sys_fork();
} else if (r->eax == 2) {
char *fname = (char *) (*(int *)(r->useresp + 4));
r->eax = sys_exec(fname);
}
}

// This gets called from our ASM interrupt handler stub.
void irq_handler(registers_t *r)
{
Expand Down
2 changes: 1 addition & 1 deletion kernel/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void keyboard_handler(registers_t *r)
* to the above layout to correspond to 'shift' being
* held. If shift is held using the larger lookup table,
* you would add 128 to the scancode when you look for it */
vga_write_char(kbdus[scancode]);
sys_write_char(kbdus[scancode]);
}
}

Expand Down
42 changes: 42 additions & 0 deletions kernel/syscall.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <isr.h>
#include <vga.h>
#include <helper.h>
#include <task.h>

static registers_t *irqf;

static void *sys_handlers[] = {
sys_write,
sys_fork,
sys_exec,
//sys_read,
};

int argint(int n, int *p)
{
/* FIXME: Need some error checking here */
*p = *(int *)(irqf->useresp + 4 + 4*n);
return 0;
}

int argstr(int n, char **p)
{
/* FIXME: Need some error checking here */
*p = *(char **)(irqf->useresp + 4 + 4*n);
return 0;
}

void syscall_handler(registers_t *r)
{
int num = r->eax;
int (*func) (void);

irqf = r;
if (num < ARR_SIZE(sys_handlers) && sys_handlers[r->eax]) {
func = sys_handlers[r->eax];
r->eax = func();
} else {
printk("Unsupported syscall %d\n", num);
}
irqf = NULL;
}
7 changes: 6 additions & 1 deletion kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <gdt.h>
#include <cpio_parser.h>
#include <elf.h>
#include <syscall.h>

/* Task list */
static list_head_t *task_list;
Expand Down Expand Up @@ -197,10 +198,14 @@ int sys_fork()
return pid;
}

int sys_exec(const char *fname)
int sys_exec()
{
char *fname;
int ret;
unsigned long size;

argstr(0, &fname);

void *fstart = cpio_get_file(_binary_ramfs_cpio_start,
fname, &size);
if (!fstart) {
Expand Down
6 changes: 3 additions & 3 deletions stdlib/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern int dec_to_str(const int num, char buf[]);
static void write_str(const char *buf)
{
while (*buf)
vga_write_char(*buf++);
sys_write_char(*buf++);
}

int printk(const char *fmt, ...)
Expand All @@ -22,7 +22,7 @@ int printk(const char *fmt, ...)
va_start(ap, fmt);
for (p = fmt; *p; p++) {
if (*p != '%') {
vga_write_char(*p);
sys_write_char(*p);
continue;
}
switch (*++p) {
Expand Down Expand Up @@ -53,7 +53,7 @@ int printk(const char *fmt, ...)
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
vga_write_char(c);
sys_write_char(c);
break;
}
}
Expand Down

0 comments on commit e5e918b

Please sign in to comment.