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 *); void wakeup(void *);
// trap.c // trap.c
void tinit(void); void tvinit(void);
void idtinit(void);
// string.c // string.c
void * memcpy(void *dst, void *src, unsigned n); void * memcpy(void *dst, void *src, unsigned n);

4
main.c
View file

@ -25,6 +25,7 @@ main()
cprintf("an application processor\n"); cprintf("an application processor\n");
release_spinlock(&kernel_lock); release_spinlock(&kernel_lock);
acquire_spinlock(&kernel_lock); acquire_spinlock(&kernel_lock);
idtinit();
lapic_init(cpu()); lapic_init(cpu());
curproc[cpu()] = &proc[0]; // XXX curproc[cpu()] = &proc[0]; // XXX
swtch(); swtch();
@ -37,7 +38,8 @@ main()
mp_init(); // multiprocessor mp_init(); // multiprocessor
kinit(); // physical memory allocator kinit(); // physical memory allocator
tinit(); // traps and interrupts tvinit(); // trap vectors
idtinit(); // CPU's idt
pic_init(); pic_init();
// create fake process zero // create fake process zero

1
proc.c
View file

@ -73,6 +73,7 @@ newproc()
// set up kernel stack to return to user space // set up kernel stack to return to user space
np->tf = (struct Trapframe *) (np->kstack + KSTACKSIZE - sizeof(struct Trapframe)); np->tf = (struct Trapframe *) (np->kstack + KSTACKSIZE - sizeof(struct Trapframe));
*(np->tf) = *(op->tf); *(np->tf) = *(op->tf);
np->tf->tf_regs.reg_eax = 0; // so fork() returns 0 in child
sp = (unsigned *) np->tf; sp = (unsigned *) np->tf;
*(--sp) = (unsigned) &trapret; // for return from swtch() *(--sp) = (unsigned) &trapret; // for return from swtch()
*(--sp) = 0; // previous bp for leave in 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); return fetchint(curproc[cpu()], esp + 8 + 4*argno, ip);
} }
void int
sys_fork() sys_fork()
{ {
newproc(); struct proc *np;
np = newproc();
return np->pid;
} }
void int
sys_exit() sys_exit()
{ {
struct proc *p; struct proc *p;
@ -67,14 +70,16 @@ sys_exit()
p->pid = 1; p->pid = 1;
swtch(); swtch();
return 0;
} }
void int
sys_wait() sys_wait()
{ {
struct proc *p; struct proc *p;
struct proc *cp = curproc[cpu()]; struct proc *cp = curproc[cpu()];
int any; int any, pid;
cprintf("waid pid %d ppid %d\n", cp->pid, cp->ppid); 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){ if(p->state == ZOMBIE && p->ppid == cp->pid){
kfree(p->mem, p->sz); kfree(p->mem, p->sz);
kfree(p->kstack, KSTACKSIZE); kfree(p->kstack, KSTACKSIZE);
pid = p->pid;
p->state = UNUSED; p->state = UNUSED;
cprintf("%x collected %x\n", cp, p); cprintf("%x collected %x\n", cp, p);
return; return pid;
} }
if(p->state != UNUSED && p->ppid == cp->pid) if(p->state != UNUSED && p->ppid == cp->pid)
any = 1; any = 1;
} }
if(any == 0){ if(any == 0){
cprintf("%x nothing to wait for\n", cp); cprintf("%x nothing to wait for\n", cp);
return; return -1;
} }
sleep(cp); sleep(cp);
} }
} }
void int
sys_cons_putc() sys_cons_putc()
{ {
int c; int c;
fetcharg(0, &c); fetcharg(0, &c);
cons_putc(c & 0xff); cons_putc(c & 0xff);
return 0;
} }
void void
@ -113,24 +120,26 @@ syscall()
{ {
struct proc *cp = curproc[cpu()]; struct proc *cp = curproc[cpu()];
int num = cp->tf->tf_regs.reg_eax; int num = cp->tf->tf_regs.reg_eax;
int ret = -1;
cprintf("%x sys %d\n", cp, num); cprintf("%x sys %d\n", cp, num);
switch(num){ switch(num){
case SYS_fork: case SYS_fork:
sys_fork(); ret = sys_fork();
break; break;
case SYS_exit: case SYS_exit:
sys_exit(); ret = sys_exit();
break; break;
case SYS_wait: case SYS_wait:
sys_wait(); ret = sys_wait();
break; break;
case SYS_cons_putc: case SYS_cons_putc:
sys_cons_putc(); ret = sys_cons_putc();
break; break;
default: default:
cprintf("unknown sys call %d\n", num); cprintf("unknown sys call %d\n", num);
// XXX fault // XXX fault
break; break;
} }
cp->tf->tf_regs.reg_eax = ret;
} }

7
trap.c
View file

@ -14,7 +14,7 @@ extern void trapenter();
extern void trapenter1(); extern void trapenter1();
void void
tinit() tvinit()
{ {
int i; int i;
@ -22,6 +22,11 @@ tinit()
SETGATE(idt[i], 1, SEG_KCODE << 3, vectors[i], 0); SETGATE(idt[i], 1, SEG_KCODE << 3, vectors[i], 0);
} }
SETGATE(idt[T_SYSCALL], T_SYSCALL, SEG_KCODE << 3, vectors[48], 3); SETGATE(idt[T_SYSCALL], T_SYSCALL, SEG_KCODE << 3, vectors[48], 3);
}
void
idtinit()
{
asm volatile("lidt %0" : : "g" (idt_pd.pd_lim)); asm volatile("lidt %0" : : "g" (idt_pd.pd_lim));
} }

14
user1.c
View file

@ -1,4 +1,4 @@
void int
fork() fork()
{ {
asm("mov $1, %eax"); asm("mov $1, %eax");
@ -12,19 +12,25 @@ cons_putc(int c)
asm("int $48"); asm("int $48");
} }
void int
puts(char *s) puts(char *s)
{ {
int i; int i;
for(i = 0; s[i]; i++) for(i = 0; s[i]; i++)
cons_putc(s[i]); cons_putc(s[i]);
return i;
} }
main() main()
{ {
// fork(); int pid;
puts("hello!\n"); pid = fork();
if(pid == 0){
cons_putc('C');
} else {
cons_putc('P');
}
while(1) while(1)
; ;
} }