@@ -27,12 +27,17 @@ volatile uint64_t userland_resume_r12 = 0;
2727volatile uint64_t userland_resume_r13 = 0 ;
2828volatile uint64_t userland_resume_r14 = 0 ;
2929volatile uint64_t userland_resume_r15 = 0 ;
30+ volatile uint64_t userland_resume_ret_rip = 0 ;
31+ volatile uint64_t userland_resume_ret_rsp = 0 ;
3032static volatile int userland_last_exit_code = 0 ;
3133static inline void wrmsr64_local (uint32_t msr , uint64_t value );
3234static void userland_unmap_all (void );
3335void userland_heap_init (void );
3436
35- __attribute__((noinline )) static void userland_finish_exit (void ) {
37+ __attribute__((noinline , noreturn )) static void userland_finish_exit (void ) {
38+ uint64_t return_rip = userland_resume_ret_rip ;
39+ uint64_t return_rsp = userland_resume_ret_rsp ;
40+
3641 userland_running = false;
3742 userland_should_return_kernel = false;
3843 userland_resume_rip = 0 ;
@@ -43,14 +48,26 @@ __attribute__((noinline)) static void userland_finish_exit(void) {
4348 userland_resume_r13 = 0 ;
4449 userland_resume_r14 = 0 ;
4550 userland_resume_r15 = 0 ;
51+ userland_resume_ret_rip = 0 ;
52+ userland_resume_ret_rsp = 0 ;
4653 kernel_stack_top = userland_saved_kernel_stack_top ;
4754 tss .rsp0 = userland_saved_tss_rsp0 ;
4855 userland_saved_kernel_stack_top = 0 ;
4956 userland_saved_tss_rsp0 = 0 ;
5057 wrmsr64_local (IA32_FS_BASE_MSR , 0 );
5158 userland_unmap_all ();
5259 userland_heap_init ();
60+ tty_flush_input ();
5361 printf (blue_color "\n[process exited with code %d]" reset_color , userland_last_exit_code );
62+
63+ asm volatile (
64+ "xor %%rax, %%rax\n"
65+ "mov %0, %%rsp\n"
66+ "jmp *%1\n"
67+ :
68+ : "r" (return_rsp ), "r" (return_rip )
69+ : "memory" , "rax" );
70+ __builtin_unreachable ();
5471}
5572
5673static void debug_dump_initial_stack (uint64_t stack_top ) {
@@ -451,6 +468,9 @@ int userland_exec(const char* path, int argc, const char* const* argv, const cha
451468 asm volatile ("mov %%r13, %0" : "=r" (userland_resume_r13 ));
452469 asm volatile ("mov %%r14, %0" : "=r" (userland_resume_r14 ));
453470 asm volatile ("mov %%r15, %0" : "=r" (userland_resume_r15 ));
471+ void * frame = __builtin_frame_address (0 );
472+ userland_resume_ret_rip = (uint64_t )__builtin_return_address (0 );
473+ userland_resume_ret_rsp = (uint64_t )frame + 16 ;
454474 userland_resume_rsp = kernel_rsp ;
455475 userland_resume_rip = (uint64_t )userland_finish_exit ;
456476 userland_should_return_kernel = false;
@@ -484,6 +504,5 @@ int userland_exec(const char* path, int argc, const char* const* argv, const cha
484504 : "memory" , "rax" , "rbx" , "rcx" , "rdx" , "rsi" , "rdi" , "r8" , "r9" , "r10" , "r11"
485505 );
486506
487- userland_finish_exit ();
488- return 0 ;
507+ __builtin_unreachable ();
489508}
0 commit comments