From 431a07c7a2a0048cc274642f84f90139576d23c5 Mon Sep 17 00:00:00 2001 From: visa Date: Thu, 24 Feb 2022 14:19:10 +0000 Subject: [PATCH] Fix kernel stack alignment on riscv64 Pad trapframe and switchframe structs so that their size is a multiple of 16 bytes. This makes context switching and exception handlers keep kernel stack properly aligned. OK kettenis@ --- sys/arch/riscv64/include/frame.h | 4 +++- sys/arch/riscv64/riscv64/vm_machdep.c | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/sys/arch/riscv64/include/frame.h b/sys/arch/riscv64/include/frame.h index ab34499a366..bfce36b52ae 100644 --- a/sys/arch/riscv64/include/frame.h +++ b/sys/arch/riscv64/include/frame.h @@ -1,4 +1,4 @@ -/* $OpenBSD: frame.h,v 1.2 2021/05/12 01:20:52 jsg Exp $ */ +/* $OpenBSD: frame.h,v 1.3 2022/02/24 14:19:10 visa Exp $ */ /* * Copyright (c) 2019 Brian Bamsch @@ -64,6 +64,7 @@ typedef struct trapframe { register_t tf_sstatus; register_t tf_stval; register_t tf_scause; + register_t tf_pad; } trapframe_t; /* @@ -85,6 +86,7 @@ struct sigframe { struct switchframe { register_t sf_s[12]; register_t sf_ra; + register_t sf_pad; }; struct callframe { diff --git a/sys/arch/riscv64/riscv64/vm_machdep.c b/sys/arch/riscv64/riscv64/vm_machdep.c index f94b7c2777b..fd4caec1d6d 100644 --- a/sys/arch/riscv64/riscv64/vm_machdep.c +++ b/sys/arch/riscv64/riscv64/vm_machdep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vm_machdep.c,v 1.9 2022/02/24 14:16:53 visa Exp $ */ +/* $OpenBSD: vm_machdep.c,v 1.10 2022/02/24 14:19:10 visa Exp $ */ /*- * Copyright (c) 1995 Charles M. Hannum. All rights reserved. @@ -62,6 +62,10 @@ cpu_fork(struct proc *p1, struct proc *p2, void *stack, void *tcb, struct trapframe *tf; struct switchframe *sf; + /* Ensure proper stack alignment. */ + CTASSERT((sizeof(struct trapframe) & STACKALIGNBYTES) == 0); + CTASSERT((sizeof(struct switchframe) & STACKALIGNBYTES) == 0); + /* Save FPU state to PCB if necessary. */ if (pcb1->pcb_flags & PCB_FPU) fpu_save(p1, pcb1->pcb_tf); -- 2.20.1