I am doing an Operating System assignment, which is adding a new system call. The system call, which is called “dumbfork”, needs to fork a process without using copy-on-write policy. So basically it has to copy the entire address space to the child process.
I was able to set up and recompile the system kernel. I can invoke my custom system call, but I don’t know how to actually implement the dumbfork to disable COW feature. One of the source code shows me how sys_vfork is calling do_fork. Dumbfork should be similar to sys_vfork. I don’t know how I can set the parameters of do_fork. I tried to mimick how sys_fork is implemented, and it gives me a NULL pointer dereferencing error. Can anyone enlighten me on this problem.
asmlinkage long sys_dumbfork(struct pt_regs *regs)
{
return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
}
do_forkis a very general function which is used infork,vforkandclonecalls. The most similar one to what you want to achieve is obviouslyfork(cloneis mainly intended for threads,vforkdoes not copy address space at all).In your case, the best solution seems to be making
do_forkaccept new flag, somthing likeCLONE_COPY_EVERYTHING_RIGHT_NOW. Then you should find code responsible for copy-on-write and depending on whether this flag was passed todo_fork: copy entire adress space or (if flag was not passed, what would indicate thatdo_forkwas called from one of the original system calls) use existing copy-on-write policy. Yourdumbforksystem call will look almost the same likeforkcall, except the additional flag.This page, although outdated, may be very helpful, especially the sections Fork ICA and Copy on Write. There is a function called during
forksyscallcopy_page_rangewhich marks pages read-only. That looks like the place you should consider for adding your memory copying code. Moreover, functiondo_wp_pageis called during page faults in order to copy previously shared page someone wanted write to, that may be a good example how such copying should look like.