简体   繁体   English

如何通过sys_fork()派生一个子进程以使其内核堆栈包含陷阱帧?

[英]how to have a child process forked through sys_fork() to have its kernel stack contain the trap frame?

I am working on the implementation of system call sys_fork() on the kernel level. 我正在内核级别上实现系统调用sys_fork()的工作。 I did the copying of the parent process to the child process as per requirements. 我按照要求将父流程复制到了子流程。 The problem is how to copy the child's trapframe (copy of the parent trapframe) onto the child's kernel stack to pass the assertion in mips_usermode()? 问题是如何将子级的陷印框(父级陷印框的副本)复制到子级的内核堆栈中,以在mips_usermode()中传递断言?

I figured out my problem. 我发现了我的问题。

But what I am going to say is related to OS161 system. 但是我要说的与OS161系统有关。 So anyone working on this system, this will be helpful. 因此,使用此系统的任何人都将有所帮助。

Ok, there is a function on the kernel side which deals with context switch. 好的,内核方面有一个函数可以处理上下文切换。 This function stores all the data related to context switch frame onto the threads kernel stack. 该函数将所有与上下文切换帧相关的数据存储到线程内核堆栈中。

So all you need to do is follow the same steps and instead of switch frame structure, you need to replace it with trap frame structure. 因此,您所需要做的就是遵循相同的步骤,而不是用开关框架结构代替开关框架结构。

Here is the implementation for it:- 这是它的实现:

vaddr_t stacktop;
struct trapframe *tf;
stacktop = ((vaddr_t)thread->t_stack) + STACK_SIZE;  //t_stack is the kernel stack
tf = ((struct trapframe *) stacktop) - 1;

t_stack is just a chunk of memory on the kernel side to store anything related to exceptions or context switch. t_stack只是内核侧的一块内存,用于存储与异常或上下文切换有关的任何内容。

Be sure to first clean out the t_stack before you load it with trapframe as it will contain data related to context switch frame incase of sys_fork implementation. 确保在使用trapframe加载t_stack之前先对其进行清理,因为在sys_fork实现的情况下,它将包含与上下文切换帧相关的数据。

Any corrections or comments on this are welcomed. 欢迎对此进行任何更正或评论。

I'm working on OS161 too. 我也在OS161上工作。 Here is how I tackle the problem. 这是我解决问题的方法。

In sys_fork, I copy parent's trapframe into a kernel heap space allocated via kmalloc: 在sys_fork中,我将双亲的trapframe复制到通过kmalloc分配的内核堆空间中:

struct trapframe* ctf = (struct trapframe*)kmalloc(sizeof(struct trapframe));
*ctf = *tf; // tf points to parent's trapframe;

Then I use thread_fork to create a child thread: 然后,我使用thread_fork创建一个子线程:

// passing address space using the second parameter of 
// child_forkentry, quite dirty
thread_fork(curthread->t_name, child_forkentry, ctf, (unsigned long)as, NULL);

In child_forkentry, which is the first function called by the child, I do the following: 在child_forkentry中,这是孩子调用的第一个函数,我执行以下操作:

struct trapframe tf; // tf will be allocated on child's kernel stack
tf = *ctf 
misp_usermode(&tf);

This will pass the stack check in mips_usermode. 这将通过mips_usermode中的堆栈检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由“fork()”生成的子进程的进程ID是否比其父进程小? - Can a child process generated by “fork()” ever have a process id lesser than its parent? 为什么glibc的fork实现没有使用sys_fork? - Why is sys_fork not used by glibc's implementation of fork? fork()-让父进程执行工作而无需等待子进程 - fork() - have parent process do work without waiting for child process 如何产生一个由fork()创建的子进程,然后其他子进程有机会运行? - How to yield a child process created by fork(), then other child processes have chance to run? 为什么我在 C 中调用 fork() 而是使用 sys_clone() 系统调用? 为什么不是 sys_fork() 系统调用? - Why I call fork() in C but make sys_clone() syscall instead? Why not sys_fork() syscall? 调用fork()然后又调用sys_fork()的过程是什么? - What's the procedure when calling fork() then it becomes calling sys_fork()? 通过C中的fork()创建子进程 - Child Process Creation through fork() in C fork() 对创建新进程的数量有限制吗? - does fork() have limit to how much it can create new process? 如何使用 CLion 调试分叉的子进程 - How to debug a forked child process using CLion 可以反转堆栈帧吗? - Is possible to have a Stack Frame reversed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM