@@ -30,6 +30,15 @@ ENTRY(handle_exception)
3030_restore_kernel_tpsp:
3131 csrr tp, CSR_SCRATCH
3232 REG_S sp , TASK_TI_KERNEL_SP(tp)
33+
34+ #ifdef CONFIG_VMAP_STACK
35+ addi sp , sp , -(PT_SIZE_ON_STACK)
36+ srli sp , sp , THREAD_SHIFT
37+ andi sp , sp , 0x1
38+ bnez sp , handle_kernel_stack_overflow
39+ REG_L sp , TASK_TI_KERNEL_SP(tp)
40+ #endif
41+
3342_save_context:
3443 REG_S sp , TASK_TI_USER_SP(tp)
3544 REG_L sp , TASK_TI_KERNEL_SP(tp)
@@ -376,6 +385,105 @@ handle_syscall_trace_exit:
376385 call do_syscall_trace_exit
377386 j ret_from_exception
378387
388+ #ifdef CONFIG_VMAP_STACK
389+ handle_kernel_stack_overflow:
390+ la sp , shadow_stack
391+ addi sp , sp , SHADOW_OVERFLOW_STACK_SIZE
392+
393+ //save caller register to shadow stack
394+ addi sp , sp , -(PT_SIZE_ON_STACK)
395+ REG_S x1, PT_RA(sp )
396+ REG_S x5, PT_T0(sp )
397+ REG_S x6, PT_T1(sp )
398+ REG_S x7, PT_T2(sp )
399+ REG_S x10, PT_A0(sp )
400+ REG_S x11, PT_A1(sp )
401+ REG_S x12, PT_A2(sp )
402+ REG_S x13, PT_A3(sp )
403+ REG_S x14, PT_A4(sp )
404+ REG_S x15, PT_A5(sp )
405+ REG_S x16, PT_A6(sp )
406+ REG_S x17, PT_A7(sp )
407+ REG_S x28, PT_T3(sp )
408+ REG_S x29, PT_T4(sp )
409+ REG_S x30, PT_T5(sp )
410+ REG_S x31, PT_T6(sp )
411+
412+ la ra, restore_caller_reg
413+ tail get_overflow_stack
414+
415+ restore_caller_reg:
416+ //save per-cpu overflow stack
417+ REG_S a0 , -8 (sp )
418+ //restore caller register from shadow_stack
419+ REG_L x1, PT_RA(sp )
420+ REG_L x5, PT_T0(sp )
421+ REG_L x6, PT_T1(sp )
422+ REG_L x7, PT_T2(sp )
423+ REG_L x10, PT_A0(sp )
424+ REG_L x11, PT_A1(sp )
425+ REG_L x12, PT_A2(sp )
426+ REG_L x13, PT_A3(sp )
427+ REG_L x14, PT_A4(sp )
428+ REG_L x15, PT_A5(sp )
429+ REG_L x16, PT_A6(sp )
430+ REG_L x17, PT_A7(sp )
431+ REG_L x28, PT_T3(sp )
432+ REG_L x29, PT_T4(sp )
433+ REG_L x30, PT_T5(sp )
434+ REG_L x31, PT_T6(sp )
435+
436+ //load per-cpu overflow stack
437+ REG_L sp , -8 (sp )
438+ addi sp , sp , -(PT_SIZE_ON_STACK)
439+
440+ //save context to overflow stack
441+ REG_S x1, PT_RA(sp )
442+ REG_S x3, PT_GP(sp )
443+ REG_S x5, PT_T0(sp )
444+ REG_S x6, PT_T1(sp )
445+ REG_S x7, PT_T2(sp )
446+ REG_S x8, PT_S0(sp )
447+ REG_S x9, PT_S1(sp )
448+ REG_S x10, PT_A0(sp )
449+ REG_S x11, PT_A1(sp )
450+ REG_S x12, PT_A2(sp )
451+ REG_S x13, PT_A3(sp )
452+ REG_S x14, PT_A4(sp )
453+ REG_S x15, PT_A5(sp )
454+ REG_S x16, PT_A6(sp )
455+ REG_S x17, PT_A7(sp )
456+ REG_S x18, PT_S2(sp )
457+ REG_S x19, PT_S3(sp )
458+ REG_S x20, PT_S4(sp )
459+ REG_S x21, PT_S5(sp )
460+ REG_S x22, PT_S6(sp )
461+ REG_S x23, PT_S7(sp )
462+ REG_S x24, PT_S8(sp )
463+ REG_S x25, PT_S9(sp )
464+ REG_S x26, PT_S10(sp )
465+ REG_S x27, PT_S11(sp )
466+ REG_S x28, PT_T3(sp )
467+ REG_S x29, PT_T4(sp )
468+ REG_S x30, PT_T5(sp )
469+ REG_S x31, PT_T6(sp )
470+
471+ REG_L s0, TASK_TI_KERNEL_SP(tp)
472+ csrr s1, CSR_STATUS
473+ csrr s2, CSR_EPC
474+ csrr s3, CSR_TVAL
475+ csrr s4, CSR_CAUSE
476+ csrr s5, CSR_SCRATCH
477+ REG_S s0, PT_SP(sp )
478+ REG_S s1, PT_STATUS(sp )
479+ REG_S s2, PT_EPC(sp )
480+ REG_S s3, PT_BADADDR(sp )
481+ REG_S s4, PT_CAUSE(sp )
482+ REG_S s5, PT_TP(sp )
483+ move a0 , sp
484+ tail handle_bad_stack
485+ #endif
486+
379487END (handle_exception)
380488
381489ENTRY (ret_from_fork)
0 commit comments