Skip to content

Commit e7d3dd1

Browse files
finikorgjhedberg
authored andcommitted
arch: x86_64: Using right exception stack with KPTI
With kernel page table isolation (KPTI) we cannot use right exception stack since after using trampoline stack there was always switch to 7th IST stack (__x86_tss64_t_ist7_OFFSET). Make this configurable as a parameter in EXCEPT(nr, ist) and EXCEPT_CODE(nr, ist). For the NMI we would use ist6 (_nmi_stack). Signed-off-by: Andrei Emeltchenko <[email protected]>
1 parent ab60e15 commit e7d3dd1

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

arch/x86/core/intel64/locore.S

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,20 @@ __resume:
429429
iretq
430430

431431

432-
433-
432+
#ifdef CONFIG_X86_KPTI
433+
#define EXCEPT_CODE(nr, ist) \
434+
vector_ ## nr: pushq %gs:__x86_tss64_t_ist ## ist ## _OFFSET; \
435+
pushq $nr; \
436+
jmp except
437+
#define EXCEPT(nr, ist) \
438+
vector_ ## nr: pushq $0; \
439+
pushq %gs:__x86_tss64_t_ist ## ist ## _OFFSET; \
440+
pushq $nr; \
441+
jmp except
442+
#else
434443
#define EXCEPT_CODE(nr) vector_ ## nr: pushq $nr; jmp except
435444
#define EXCEPT(nr) vector_ ## nr: pushq $0; pushq $nr; jmp except
445+
#endif
436446

437447
/*
438448
* When we arrive at 'except' from one of the EXCEPT(X) stubs,
@@ -445,6 +455,7 @@ __resume:
445455
* CS
446456
* RIP
447457
* Error Code if pushed by CPU, else 0
458+
* IST index in TSS
448459
* Vector number <- RSP points here
449460
*
450461
*/
@@ -458,7 +469,7 @@ except: /*
458469

459470
#ifdef CONFIG_USERSPACE
460471
/* Swap GS register values and page tables if we came from user mode */
461-
testb $0x3, 32(%rsp)
472+
testb $0x3, 40(%rsp)
462473
jz 1f
463474
swapgs
464475
#ifdef CONFIG_X86_KPTI
@@ -475,16 +486,16 @@ except: /*
475486
/* Save old trampoline stack pointer in R11 */
476487
movq %rsp, %r11
477488

478-
/* Switch to the exception stack */
479-
movq %gs:__x86_tss64_t_ist7_OFFSET, %rsp
489+
/* Switch to the correct stack */
490+
movq 16(%r11), %rsp
480491

481492
/* Transplant trampoline stack contents */
482-
pushq 56(%r11) /* SS */
483-
pushq 48(%r11) /* RSP */
484-
pushq 40(%r11) /* RFLAGS */
485-
pushq 32(%r11) /* CS */
486-
pushq 24(%r11) /* RIP */
487-
pushq 16(%r11) /* Error code */
493+
pushq 64(%r11) /* SS */
494+
pushq 56(%r11) /* RSP */
495+
pushq 48(%r11) /* RFLAGS */
496+
pushq 40(%r11) /* CS */
497+
pushq 32(%r11) /* RIP */
498+
pushq 24(%r11) /* Error code */
488499
pushq 8(%r11) /* Vector */
489500
pushq (%r11) /* Stashed R15 */
490501
movq $0, (%r11) /* Cover our tracks */
@@ -562,6 +573,21 @@ except: /*
562573

563574
iretq
564575

576+
#ifdef CONFIG_X86_KPTI
577+
EXCEPT ( 0, 7); EXCEPT ( 1, 7); EXCEPT (2, 6); EXCEPT ( 3, 7)
578+
EXCEPT ( 4, 7); EXCEPT ( 5, 7); EXCEPT (6, 7); EXCEPT ( 7, 7)
579+
EXCEPT_CODE ( 8, 7); EXCEPT ( 9, 7); EXCEPT_CODE (10, 7); EXCEPT_CODE (11, 7)
580+
EXCEPT_CODE (12, 7); EXCEPT_CODE (13, 7); EXCEPT_CODE (14, 7); EXCEPT (15, 7)
581+
EXCEPT (16, 7); EXCEPT_CODE (17, 7); EXCEPT (18, 7); EXCEPT (19, 7)
582+
EXCEPT (20, 7); EXCEPT (21, 7); EXCEPT (22, 7); EXCEPT (23, 7)
583+
EXCEPT (24, 7); EXCEPT (25, 7); EXCEPT (26, 7); EXCEPT (27, 7)
584+
EXCEPT (28, 7); EXCEPT (29, 7); EXCEPT (30, 7); EXCEPT (31, 7)
585+
586+
/* Vector reserved for handling a kernel oops; treat as an exception
587+
* and not an interrupt
588+
*/
589+
EXCEPT(Z_X86_OOPS_VECTOR, 7);
590+
#else
565591
EXCEPT ( 0); EXCEPT ( 1); EXCEPT ( 2); EXCEPT ( 3)
566592
EXCEPT ( 4); EXCEPT ( 5); EXCEPT ( 6); EXCEPT ( 7)
567593
EXCEPT_CODE ( 8); EXCEPT ( 9); EXCEPT_CODE (10); EXCEPT_CODE (11)
@@ -575,6 +601,7 @@ EXCEPT (28); EXCEPT (29); EXCEPT (30); EXCEPT (31)
575601
* and not an interrupt
576602
*/
577603
EXCEPT(Z_X86_OOPS_VECTOR);
604+
#endif /* CONFIG_X86_KPTI */
578605

579606
/*
580607
* When we arrive at 'irq' from one of the IRQ(X) stubs,

0 commit comments

Comments
 (0)