Skip to content

Commit

Permalink
system call return values
Browse files Browse the repository at this point in the history
initialize 2nd cpu's idt
  • Loading branch information
rtm committed Jun 26, 2006
1 parent a44ee3c commit b61c254
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
3 changes: 2 additions & 1 deletion defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ void sleep(void *);
void wakeup(void *);

// trap.c
void tinit(void);
void tvinit(void);
void idtinit(void);

// string.c
void * memcpy(void *dst, void *src, unsigned n);
Expand Down
4 changes: 3 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ main()
cprintf("an application processor\n");
release_spinlock(&kernel_lock);
acquire_spinlock(&kernel_lock);
idtinit();
lapic_init(cpu());
curproc[cpu()] = &proc[0]; // XXX
swtch();
Expand All @@ -37,7 +38,8 @@ main()

mp_init(); // multiprocessor
kinit(); // physical memory allocator
tinit(); // traps and interrupts
tvinit(); // trap vectors
idtinit(); // CPU's idt
pic_init();

// create fake process zero
Expand Down
1 change: 1 addition & 0 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ newproc()
// set up kernel stack to return to user space
np->tf = (struct Trapframe *) (np->kstack + KSTACKSIZE - sizeof(struct Trapframe));
*(np->tf) = *(op->tf);
np->tf->tf_regs.reg_eax = 0; // so fork() returns 0 in child
sp = (unsigned *) np->tf;
*(--sp) = (unsigned) &trapret; // for return from swtch()
*(--sp) = 0; // previous bp for leave in swtch()
Expand Down
33 changes: 21 additions & 12 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ fetcharg(int argno, int *ip)
return fetchint(curproc[cpu()], esp + 8 + 4*argno, ip);
}

void
int
sys_fork()
{
newproc();
struct proc *np;

np = newproc();
return np->pid;
}

void
int
sys_exit()
{
struct proc *p;
Expand All @@ -67,14 +70,16 @@ sys_exit()
p->pid = 1;

swtch();

return 0;
}

void
int
sys_wait()
{
struct proc *p;
struct proc *cp = curproc[cpu()];
int any;
int any, pid;

cprintf("waid pid %d ppid %d\n", cp->pid, cp->ppid);

Expand All @@ -84,53 +89,57 @@ sys_wait()
if(p->state == ZOMBIE && p->ppid == cp->pid){
kfree(p->mem, p->sz);
kfree(p->kstack, KSTACKSIZE);
pid = p->pid;
p->state = UNUSED;
cprintf("%x collected %x\n", cp, p);
return;
return pid;
}
if(p->state != UNUSED && p->ppid == cp->pid)
any = 1;
}
if(any == 0){
cprintf("%x nothing to wait for\n", cp);
return;
return -1;
}
sleep(cp);
}
}

void
int
sys_cons_putc()
{
int c;

fetcharg(0, &c);
cons_putc(c & 0xff);
return 0;
}

void
syscall()
{
struct proc *cp = curproc[cpu()];
int num = cp->tf->tf_regs.reg_eax;
int ret = -1;

cprintf("%x sys %d\n", cp, num);
switch(num){
case SYS_fork:
sys_fork();
ret = sys_fork();
break;
case SYS_exit:
sys_exit();
ret = sys_exit();
break;
case SYS_wait:
sys_wait();
ret = sys_wait();
break;
case SYS_cons_putc:
sys_cons_putc();
ret = sys_cons_putc();
break;
default:
cprintf("unknown sys call %d\n", num);
// XXX fault
break;
}
cp->tf->tf_regs.reg_eax = ret;
}
7 changes: 6 additions & 1 deletion trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ extern void trapenter();
extern void trapenter1();

void
tinit()
tvinit()
{
int i;

for(i = 0; i < 256; i++){
SETGATE(idt[i], 1, SEG_KCODE << 3, vectors[i], 0);
}
SETGATE(idt[T_SYSCALL], T_SYSCALL, SEG_KCODE << 3, vectors[48], 3);
}

void
idtinit()
{
asm volatile("lidt %0" : : "g" (idt_pd.pd_lim));
}

Expand Down
14 changes: 10 additions & 4 deletions user1.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void
int
fork()
{
asm("mov $1, %eax");
Expand All @@ -12,19 +12,25 @@ cons_putc(int c)
asm("int $48");
}

void
int
puts(char *s)
{
int i;

for(i = 0; s[i]; i++)
cons_putc(s[i]);
return i;
}

main()
{
// fork();
puts("hello!\n");
int pid;
pid = fork();
if(pid == 0){
cons_putc('C');
} else {
cons_putc('P');
}
while(1)
;
}

0 comments on commit b61c254

Please sign in to comment.