Skip to contentSkip to conversation

tcg: Use correct trap number for page faults on *BSD systems

PATCH Applied Warner Losh 2 participants 5 messages May 6, 2021 – May 14, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tcg: Use correct trap number for page faults on *BSD systems

The trap number for a page fault on BSD systems is T_PAGEFLT not 0xe. 0xe is used by Linux and represents the intel hardware trap vector. The BSD kernels, however, translate this to T_PAGEFLT in their Xpage, Xtrap0e, Xtrap14, etc fault handlers. This is true for i386 and x86_64, though the name of the trap hanlder can very on the flavor of BSD. As far as I can tell, Linux doesn't provide a define for this value. Invent a new one (PAGE_FAULT_TRAP) and use it instead to avoid uglier ifdefs.

[ Rework to avoid ifdefs and expand it to i386 ]
---
 accel/tcg/user-exec.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
accel/tcg/user-exec.c +12 -2
index 0d8cc27b21..959fec1257 100644
@@ -260,6 +260,7 @@ void *probe_access(CPUArchState *env, target_ulong addr, int size,
 #define TRAP_sig(context)    ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
 #define ERROR_sig(context)   ((context)->uc_mcontext.__gregs[_REG_ERR])
 #define MASK_sig(context)    ((context)->uc_sigmask)
+#define PAGE_FAULT_TRAP      T_PAGEFLT
 #elif defined(__FreeBSD__) || defined(__DragonFly__)
 #include <ucontext.h>
 
@@ -267,16 +268,19 @@ void *probe_access(CPUArchState *env, target_ulong addr, int size,
 #define TRAP_sig(context)    ((context)->uc_mcontext.mc_trapno)
 #define ERROR_sig(context)   ((context)->uc_mcontext.mc_err)
 #define MASK_sig(context)    ((context)->uc_sigmask)
+#define PAGE_FAULT_TRAP      T_PAGEFLT
 #elif defined(__OpenBSD__)
 #define EIP_sig(context)     ((context)->sc_eip)
 #define TRAP_sig(context)    ((context)->sc_trapno)
 #define ERROR_sig(context)   ((context)->sc_err)
 #define MASK_sig(context)    ((context)->sc_mask)
+#define PAGE_FAULT_TRAP      T_PAGEFLT
 #else
 #define EIP_sig(context)     ((context)->uc_mcontext.gregs[REG_EIP])
 #define TRAP_sig(context)    ((context)->uc_mcontext.gregs[REG_TRAPNO])
 #define ERROR_sig(context)   ((context)->uc_mcontext.gregs[REG_ERR])
 #define MASK_sig(context)    ((context)->uc_sigmask)
+#define PAGE_FAULT_TRAP      0xe
 #endif
 
 int cpu_signal_handler(int host_signum, void *pinfo,
@@ -302,7 +306,8 @@ int cpu_signal_handler(int host_signum, void *pinfo,
     pc = EIP_sig(uc);
     trapno = TRAP_sig(uc);
     return handle_cpu_signal(pc, info,
-                             trapno == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
+                             trapno == PAGE_FAULT_TRAP ?
+                             (ERROR_sig(uc) >> 1) & 1 : 0,
                              &MASK_sig(uc));
 }
 
@@ -313,11 +318,13 @@ int cpu_signal_handler(int host_signum, void *pinfo,
 #define TRAP_sig(context)     ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
 #define ERROR_sig(context)    ((context)->uc_mcontext.__gregs[_REG_ERR])
 #define MASK_sig(context)     ((context)->uc_sigmask)
+#define PAGE_FAULT_TRAP       T_PAGEFLT
 #elif defined(__OpenBSD__)
 #define PC_sig(context)       ((context)->sc_rip)
 #define TRAP_sig(context)     ((context)->sc_trapno)
 #define ERROR_sig(context)    ((context)->sc_err)
 #define MASK_sig(context)     ((context)->sc_mask)
+#define PAGE_FAULT_TRAP       T_PAGEFLT
 #elif defined(__FreeBSD__) || defined(__DragonFly__)
 #include <ucontext.h>
 
@@ -325,11 +332,13 @@ int cpu_signal_handler(int host_signum, void *pinfo,
 #define TRAP_sig(context)     ((context)->uc_mcontext.mc_trapno)
 #define ERROR_sig(context)    ((context)->uc_mcontext.mc_err)
 #define MASK_sig(context)     ((context)->uc_sigmask)
+#define PAGE_FAULT_TRAP       T_PAGEFLT
 #else
 #define PC_sig(context)       ((context)->uc_mcontext.gregs[REG_RIP])
 #define TRAP_sig(context)     ((context)->uc_mcontext.gregs[REG_TRAPNO])
 #define ERROR_sig(context)    ((context)->uc_mcontext.gregs[REG_ERR])
 #define MASK_sig(context)     ((context)->uc_sigmask)
+#define PAGE_FAULT_TRAP       0xe
 #endif
 
 int cpu_signal_handler(int host_signum, void *pinfo,
@@ -347,7 +356,8 @@ int cpu_signal_handler(int host_signum, void *pinfo,
 
     pc = PC_sig(uc);
     return handle_cpu_signal(pc, info,
-                             TRAP_sig(uc) == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
+                             TRAP_sig(uc) == PAGE_FAULT_TRAP ?
+                             (ERROR_sig(uc) >> 1) & 1 : 0,
                              &MASK_sig(uc));
 }
 
Signature · 1 line
2.22.1
#1 lore raw
Quoted 8 lines · Warner Losh

The trap number for a page fault on BSD systems is T_PAGEFLT not 0xe. 0xe is used by Linux and represents the intel hardware trap vector. The BSD kernels, however, translate this to T_PAGEFLT in their Xpage, Xtrap0e, Xtrap14, etc fault handlers. This is true for i386 and x86_64, though the name of the trap hanlder can very on the flavor of BSD. As far as I can tell, Linux doesn't provide a define for this value. Invent a new one (PAGE_FAULT_TRAP) and use it instead to avoid uglier ifdefs.

Quoted 7 lines
[ Rework to avoid ifdefs and expand it to i386 ]
---
  accel/tcg/user-exec.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)

Queued to tcg-next, thanks.

Looks like this area could use a bit of cleanup...

r~

#2 lore raw
Quoted 23 lines · Richard Henderson
Quoted 1 line · Warner Losh

The trap number for a page fault on BSD systems is T_PAGEFLT not 0xe.

0xe is

Quoted 1 line

used by Linux and represents the intel hardware trap vector. The BSD

kernels,

Quoted 1 line

however, translate this to T_PAGEFLT in their Xpage, Xtrap0e, Xtrap14,

etc fault

Quoted 1 line

handlers. This is true for i386 and x86_64, though the name of the trap

hanlder

Quoted 1 line

can very on the flavor of BSD. As far as I can tell, Linux doesn't

provide a

Quoted 1 line

define for this value. Invent a new one (PAGE_FAULT_TRAP) and use it

instead to

Quoted 9 lines

avoid uglier ifdefs.

[ Rework to avoid ifdefs and expand it to i386 ]
---
  accel/tcg/user-exec.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
Quoted 4 lines

Queued to tcg-next, thanks.

Looks like this area could use a bit of cleanup...

No arguments from me there... Thanks!

Warner

#3 lore raw
Quoted 25 lines · Warner Losh

On Thu, May 6, 2021 at 11:51 AM Richard Henderson <[email protected] <mailto:[email protected]>> wrote:

On 5/6/21 10:38 AM, Warner Losh wrote:
> The trap number for a page fault on BSD systems is T_PAGEFLT not 0xe. 0xe is > used by Linux and represents the intel hardware trap vector. The BSD
kernels,
> however, translate this to T_PAGEFLT in their Xpage, Xtrap0e, Xtrap14,
etc fault
> handlers. This is true for i386 and x86_64, though the name of the trap
hanlder
> can very on the flavor of BSD. As far as I can tell, Linux doesn't provide a > define for this value. Invent a new one (PAGE_FAULT_TRAP) and use it
instead to
> avoid uglier ifdefs.
>
> Signed-off-by: Mark Johnston<[email protected]>
> Signed-off-by: Juergen Lock<[email protected]>
> [ Rework to avoid ifdefs and expand it to i386 ]
> Signed-off-by: Warner Losh<[email protected] <mailto:[email protected]>> > ---
>   accel/tcg/user-exec.c | 14 ++++++++++++--
>   1 file changed, 12 insertions(+), 2 deletions(-)

Quoted 6 lines

Queued to tcg-next, thanks.

Looks like this area could use a bit of cleanup...

No arguments from me there... Thanks!

Dequeueing.

This doesn't work on our "make vm-build-{freebsd,openbsd} images, as Peter helpfully pointed out after I sent the pull request. I don't know enough about any of the BSDs to know what's expected.

r~

#4 lore raw
Quoted 40 lines · Richard Henderson
Quoted 3 lines · Warner Losh

On Thu, May 6, 2021 at 11:51 AM Richard Henderson <

[email protected]

Quoted 4 lines

<mailto:[email protected]>> wrote:

On 5/6/21 10:38 AM, Warner Losh wrote:
> The trap number for a page fault on BSD systems is T_PAGEFLT not

0xe. 0xe is

Quoted 1 line

> used by Linux and represents the intel hardware trap vector. The

BSD

Quoted 2 lines

kernels,
> however, translate this to T_PAGEFLT in their Xpage, Xtrap0e,

Xtrap14,

Quoted 2 lines

etc fault
> handlers. This is true for i386 and x86_64, though the name of

the trap

Quoted 2 lines

hanlder
> can very on the flavor of BSD. As far as I can tell, Linux

doesn't provide a

Quoted 1 line

> define for this value. Invent a new one (PAGE_FAULT_TRAP) and use

it

Quoted 17 lines

instead to
> avoid uglier ifdefs.
>
> Signed-off-by: Mark Johnston<[email protected]>
> Signed-off-by: Juergen Lock<[email protected]>
> [ Rework to avoid ifdefs and expand it to i386 ]
> Signed-off-by: Warner Losh<[email protected] <mailto:[email protected]

Quoted 1 line

> ---
> accel/tcg/user-exec.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)

Queued to tcg-next, thanks.

Looks like this area could use a bit of cleanup...

Quoted 9 lines
Quoted 1 line

No arguments from me there... Thanks!

Dequeueing.

This doesn't work on our "make vm-build-{freebsd,openbsd} images, as Peter helpfully pointed out after I sent the pull request. I don't know enough about
any of the BSDs to know what's expected.

OK. I'll take a closer look. It's survived the make vm-build-freebsd in the past, so
I'm a little surprised at this...

Warner