我正在尝试在os161中实现fork调用,但是在运行内核后出现以下错误:
我的sys_fork函数的伪流程:
-
创建新的地址空间,陷阱帧
-
声明新线程指针 - ptrthread - 我在这里没有分配内存
-
as_copy(源>目标)
-
线程分叉(名称,新陷阱帧,(无符号长)PTRthread->VMspace,分叉函数,PTR线程)
-
返回 pid()
伪分叉:
1.新的陷阱框架=叉入的陷阱框架参数
-
curthread->vmspace = addrspace arg of forkentry
-
actvate(curthread->vmspace)
-
在新的陷阱框架中设置一些vard
-
mips_usermode....
当我运行内核时,发生以下错误并且内核停止执行:
sys161: System/161 release 1.14, compiled Aug 24 2011 10:55:58
OS/161 base system version 1.11
Copyright (c) 2000, 2001, 2002, 2003
President and Fellows of Harvard College. All rights rescheduler: Dropping thread <boot/menu>.
panic: Assertion failed: SAME_STACK(curkstack-1, (vaddr_t)tf), at ../../arch/mips/mips/trap.c:220 (mips_trap)
sys161: 930837 cycles (827682k, 0u, 103155i)
sys161: 130 irqs 20 exns 0r/0w disk 0r/279w console 0r/0w/1m emufs 0r/0w net
sys161: Elapsed real time: 0.087962 seconds (10.5823 mhz)
sys161: Elapsed virtual time: 0.037233480 seconds (25 mhz)
我基于某种相同的方法对fork系统调用进行了编码。您可以查看以下方法:
https://github.com/prathammalik/OS161/blob/master/kern/syscall/psyscall.c
错误似乎是陷阱帧需要与新线程位于同一堆栈上,而不是堆中的某个位置。上述断言声明上方的评论中也写了同样的内容。
It's necessary for the trap frame used here to be on the current thread's own stack. It cannot correctly be on either another thread's stack or in the kernel heap.
因此,您需要将陷阱帧从堆复制到当前线程的堆栈上。