system call return values

initialize 2nd cpu's idt
This commit is contained in:
rtm 2006-06-26 20:31:52 +00:00
parent a44ee3cde8
commit b61c2547b8
6 changed files with 43 additions and 19 deletions

3
defs.h
View file

@ -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);

4
main.c
View file

@ -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();
@ -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

1
proc.c
View file

@ -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()

View file

@ -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;
@ -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);
@ -84,28 +89,30 @@ 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
@ -113,24 +120,26 @@ 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
trap.c
View file

@ -14,7 +14,7 @@ extern void trapenter();
extern void trapenter1();
void
tinit()
tvinit()
{
int i;
@ -22,6 +22,11 @@ tinit()
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));
}

14
user1.c
View file

@ -1,4 +1,4 @@
void
int
fork()
{
asm("mov $1, %eax");
@ -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)
;
}