|
6 | 6 |
|
7 | 7 | .include "assembly-helpers.s" |
8 | 8 |
|
| 9 | +# Symbolic names for the satck high water mark registers until |
| 10 | +# the assembler knows about them. |
| 11 | + |
| 12 | +/** |
| 13 | + * Machine-mode stack high water mark CSR |
| 14 | + */ |
| 15 | +#define CSR_MSHWM 0xbc1 |
| 16 | +/** |
| 17 | + * Machine mode stack high water mark stack base CSR |
| 18 | + */ |
| 19 | +#define CSR_MSHWMB 0xbc2 |
| 20 | + |
9 | 21 | #define MAX_FAULTS_PER_COMPARTMENT_CALL 1024 |
10 | 22 |
|
11 | 23 | # Global for the sealing key. Stored in the switcher's code section. |
@@ -105,11 +117,10 @@ switcher_scheduler_entry_csp: |
105 | 117 | .endm |
106 | 118 |
|
107 | 119 | /** |
108 | | - * Zero the stack. The three operands are the base address (modified during |
109 | | - * this call, will point at the top at the end), the top address, and a scratch |
110 | | - * register to use. The base must be a capability but it must be provided |
111 | | - * without the c prefix because it is used as both a capability and integer |
112 | | - * register. Top and scratch are both clobbered. |
| 120 | + * Zero the stack. The three operands are the base address, the top address, |
| 121 | + * and a scratch register to use. The base must be a capability but it must |
| 122 | + * be provided without the c prefix because it is used as both a capability |
| 123 | + * and integer register. All three registers are clobbered. |
113 | 124 | */ |
114 | 125 | .macro zero_stack base top scratch |
115 | 126 | addi \scratch, \top, -32 |
@@ -181,8 +192,24 @@ compartment_switcher_entry: |
181 | 192 | cgetbase s1, csp |
182 | 193 | csetaddr csp, csp, s1 |
183 | 194 | sub s1, s0, s1 |
184 | | - csetboundsexact csp, csp, s1 |
185 | | - zero_stack sp, s0, gp |
| 195 | + csetboundsexact ct2, csp, s1 |
| 196 | + csetaddr csp, ct2, s0 |
| 197 | +#ifdef CONFIG_MSHWM |
| 198 | + // read and align the stack high water mark |
| 199 | + csrr gp, CSR_MSHWM |
| 200 | + and gp, gp, ~0xf |
| 201 | + // skip zeroing if high water mark >= stack poitner |
| 202 | + bge t2, sp, after_zero |
| 203 | + // use stack high water mark as base address for zeroing |
| 204 | + // XXX could be out of bounds / unrepresentable if bad csp? |
| 205 | + csetaddr ct2, csp, gp |
| 206 | +#endif |
| 207 | + zero_stack t2, s0, gp |
| 208 | +after_zero: |
| 209 | +#ifdef CONFIG_MSHWM |
| 210 | + // store new stack top as stack high water mark |
| 211 | + csrw CSR_MSHWM, sp |
| 212 | +#endif |
186 | 213 | #endif // CONFIG_NO_SWITCHER_SAFETY |
187 | 214 | .Lout: |
188 | 215 | // Fetch the sealing key |
@@ -315,6 +342,12 @@ exception_entry_asm: |
315 | 342 | csc ct0, TrustedStack_offset_mepcc(csp) |
316 | 343 | csrr t1, mstatus |
317 | 344 | csw t1, TrustedStack_offset_mstatus(csp) |
| 345 | +#ifdef CONFIG_MSHWM |
| 346 | + csrr t1, CSR_MSHWM |
| 347 | + csw t1, TrustedStack_offset_mshwm(csp) |
| 348 | + csrr t1, CSR_MSHWMB |
| 349 | + csw t1, TrustedStack_offset_mshwmb(csp) |
| 350 | +#endif |
318 | 351 | csrr t1, mcause |
319 | 352 | csw t1, TrustedStack_offset_mcause(csp) |
320 | 353 |
|
@@ -394,6 +427,12 @@ exception_entry_asm: |
394 | 427 | .Linstall_context: |
395 | 428 | clw x1, TrustedStack_offset_mstatus(csp) |
396 | 429 | csrw mstatus, x1 |
| 430 | +#ifdef CONFIG_MSHWM |
| 431 | + clw x1, TrustedStack_offset_mshwm(csp) |
| 432 | + csrw CSR_MSHWM, x1 |
| 433 | + clw x1, TrustedStack_offset_mshwmb(csp) |
| 434 | + csrw CSR_MSHWMB, x1 |
| 435 | +#endif |
397 | 436 | cspecialw mepcc, ct2 |
398 | 437 | csb zero, TrustedStack_offset_inForcedUnwind(csp) |
399 | 438 | // c2 is csp, which will be loaded last and will overwrite the trusted |
@@ -694,9 +733,21 @@ exception_entry_asm: |
694 | 733 | // Update the current frame offset. |
695 | 734 | csw t2, TrustedStack_offset_frameoffset(ctp) |
696 | 735 | #ifndef CONFIG_NO_SWITCHER_SAFETY |
| 736 | +#ifdef CONFIG_MSHWM |
| 737 | + // read and align the stack high water mark |
| 738 | + // we will use this as base address for stack clearing |
| 739 | + // note that it cannot be greater than stack top as we |
| 740 | + // we set it to stack top when we pushed to trusted stack frame |
| 741 | + csrr tp, CSR_MSHWM |
| 742 | + and tp, tp, ~0xf |
| 743 | +#else |
697 | 744 | cgetbase tp, csp |
| 745 | +#endif |
698 | 746 | cgetaddr t1, csp |
699 | 747 | csetaddr ct2, csp, tp |
700 | 748 | zero_stack t2, t1, tp |
| 749 | +#ifdef CONFIG_MSHWM |
| 750 | + csrw CSR_MSHWM, sp |
| 751 | +#endif |
701 | 752 | #endif // CONFIG_NO_SWITCHER_SAFETY |
702 | 753 | cret |
0 commit comments