XV6 using lazy allocation
When the process requires more memory, the call to malloc requests more heap memory, and the system calls SBRK () to complete the work
However, some processes will apply for a large amount of memory at a time, but may not be used at all, for example sparse array
So the complex kernel involves deferring the actual allocation work to the actual use, the page fault, and then the actual assignment.
Part1.
The actual implementation of the system call SBRK SYS_SBRK modified, only the memory space of the process to increase n, not the actual allocation
System call malloc use is SBRK
The address returned is the beginning of the newly allocated address space, which is the end of the original address space, where the virtual address
Then the result of running at this point is
Because when you need to run echo in the shell, the code that runs to the following section
That is, when you run malloc, the return is successful, but when the program tries to manipulate the memory area that the CMD points to, it finds that the memory area is not owned by the current process, because it is not assigned at all in SYS_SBRK.
Part2.
Modify the code in the TRAP.C so that the system can handle the above error
In Trap.c, when the discovery is a page fault error, you can follow the current process of the Proc->sz to actually allocate memory, note that this time the SZ size is not the actual size, but the desired size value
So the first should get the virtual address of the page fault moment, the part after that address should be allocated but actually not allocated, and the actual need to allocate how much, should be based on the size of Proc->sz to be determined.
Because the address at the time of page fault is the address that we return to the process after malloc, and this address is the original Proc->sz, so the size of the virtual address should be the actual size of the memory
Mit-xv6/jos Hw:lazy Page Allocation