@@ -13,8 +13,15 @@ static uint64_t user_heap_mapped_end = USER_HEAP_VADDR;
1313static uint64_t user_mmap_cursor = USER_MMAP_VADDR ;
1414static uint64_t user_mmap_end = USER_MMAP_VADDR ;
1515static volatile bool userland_running = false;
16- static volatile uint64_t userland_resume_rip = 0 ;
17- static volatile uint64_t userland_resume_rsp = 0 ;
16+ volatile bool userland_should_return_kernel = false;
17+ volatile uint64_t userland_resume_rip = 0 ;
18+ volatile uint64_t userland_resume_rsp = 0 ;
19+ volatile uint64_t userland_resume_rbx = 0 ;
20+ volatile uint64_t userland_resume_rbp = 0 ;
21+ volatile uint64_t userland_resume_r12 = 0 ;
22+ volatile uint64_t userland_resume_r13 = 0 ;
23+ volatile uint64_t userland_resume_r14 = 0 ;
24+ volatile uint64_t userland_resume_r15 = 0 ;
1825static volatile int userland_last_exit_code = 0 ;
1926
2027static void debug_dump_initial_stack (uint64_t stack_top ) {
@@ -329,17 +336,13 @@ uint64_t userland_mmap_anon(uint64_t length) {
329336}
330337
331338bool userland_prepare_exit (syscall_frame_t * frame , uint64_t exit_code ) {
332- if (!frame || !userland_running || !userland_resume_rip || !userland_resume_rsp )
339+ (void )frame ;
340+
341+ if (!userland_running || !userland_resume_rip || !userland_resume_rsp )
333342 return false;
334343
335344 userland_last_exit_code = (int )exit_code ;
336-
337- frame -> rip = userland_resume_rip ;
338- frame -> cs = 0x08 ;
339- frame -> rflags = 0x202 ;
340- frame -> rsp = userland_resume_rsp ;
341- frame -> ss = 0x10 ;
342- frame -> rax = exit_code ;
345+ userland_should_return_kernel = true;
343346 return true;
344347}
345348
@@ -413,8 +416,15 @@ int userland_exec(const char* path, int argc, const char* const* argv, const cha
413416
414417 uint64_t kernel_rsp = 0 ;
415418 asm volatile ("mov %%rsp, %0" : "=r" (kernel_rsp ));
419+ asm volatile ("mov %%rbx, %0" : "=r" (userland_resume_rbx ));
420+ asm volatile ("mov %%rbp, %0" : "=r" (userland_resume_rbp ));
421+ asm volatile ("mov %%r12, %0" : "=r" (userland_resume_r12 ));
422+ asm volatile ("mov %%r13, %0" : "=r" (userland_resume_r13 ));
423+ asm volatile ("mov %%r14, %0" : "=r" (userland_resume_r14 ));
424+ asm volatile ("mov %%r15, %0" : "=r" (userland_resume_r15 ));
416425 userland_resume_rsp = kernel_rsp ;
417426 userland_resume_rip = (uint64_t )& & userland_return_label ;
427+ userland_should_return_kernel = false;
418428 userland_last_exit_code = 0 ;
419429 userland_running = true;
420430
@@ -443,12 +453,18 @@ int userland_exec(const char* path, int argc, const char* const* argv, const cha
443453
444454userland_return_label :
445455 userland_running = false;
456+ userland_should_return_kernel = false;
446457 userland_resume_rip = 0 ;
447458 userland_resume_rsp = 0 ;
459+ userland_resume_rbx = 0 ;
460+ userland_resume_rbp = 0 ;
461+ userland_resume_r12 = 0 ;
462+ userland_resume_r13 = 0 ;
463+ userland_resume_r14 = 0 ;
464+ userland_resume_r15 = 0 ;
448465 wrmsr64_local (IA32_FS_BASE_MSR , 0 );
449466 userland_unmap_all ();
450467 userland_heap_init ();
451468 printf (blue_color "\n[process exited with code %d]" reset_color , userland_last_exit_code );
452- hcf2 ();
453469 return 0 ;
454470}
0 commit comments