[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH 1/6] arch/x86: Move `struct __regs` definition to <uk/arch/lcpu.h>


  • To: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
  • From: gaulthier.gain@xxxxxxxxx
  • Date: Thu, 12 Dec 2019 14:36:03 +0100 (CET)
  • Cc: Felipe Huici <felipe.huici@xxxxxxxxx>, minios-devel@xxxxxxxxxxxxx
  • Delivery-date: Thu, 12 Dec 2019 13:36:27 +0000
  • Dkim-filter: OpenDKIM Filter v2.11.0 serv108.segi.ulg.ac.be 5B8F42012377
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>
  • Thread-index: PHFlQVtUZzB6uJ8S1YGPLNUA1RPbKA==
  • Thread-topic: arch/x86: Move `struct __regs` definition to <uk/arch/lcpu.h>

Reviewed-by: Gaulthier Gain <gaulthier.gain@xxxxxxxxx>

----- Mail original -----
De: "Simon Kuenzer" <simon.kuenzer@xxxxxxxxx>
À: minios-devel@xxxxxxxxxxxxx
Cc: "Gaulthier Gain" <gaulthier.gain@xxxxxxxxx>, "Felipe Huici" 
<felipe.huici@xxxxxxxxx>, "Simon Kuenzer" <simon.kuenzer@xxxxxxxxx>
Envoyé: Jeudi 5 Décembre 2019 15:02:17
Objet: [UNIKRAFT PATCH 1/6] arch/x86: Move `struct __regs` definition to 
<uk/arch/lcpu.h>

Expose `struct __regs` as part of the architecture headers. This is
inline with what we did for Arm64 and useful for multiple things:
 - `dump_regs()` could be moved to libukdebug
 - A syscall-ABI-independent interface could be defined for registering
   syscall handler libraries. A syscall handler would get handed over
   a reference to the saved registers state in order to let it handle
   the system call arguments according to the target ABI.

Signed-off-by: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
---
 arch/x86/x86_64/include/uk/asm/lcpu.h      | 64 +++++++++++++++
 include/uk/arch/lcpu.h                     |  4 +
 plat/common/include/uk/plat/common/trace.h |  6 +-
 plat/common/include/x86/regs.h             | 94 ----------------------
 plat/common/include/x86/traps.h            |  2 +-
 plat/kvm/x86/cpu_vectors_x86_64.S          | 11 ++-
 plat/xen/x86/entry64.S                     | 84 +++++++++----------
 7 files changed, 117 insertions(+), 148 deletions(-)
 delete mode 100644 plat/common/include/x86/regs.h

diff --git a/arch/x86/x86_64/include/uk/asm/lcpu.h 
b/arch/x86/x86_64/include/uk/asm/lcpu.h
index 8c70540a..6a471a15 100644
--- a/arch/x86/x86_64/include/uk/asm/lcpu.h
+++ b/arch/x86/x86_64/include/uk/asm/lcpu.h
@@ -32,6 +32,69 @@
 #error Do not include this header directly
 #endif
 
+#ifndef __ASSEMBLY__
+struct __regs {
+       unsigned long pad; /* 8 bytes to make struct size multiple of 16 */
+       unsigned long r15;
+       unsigned long r14;
+       unsigned long r13;
+       unsigned long r12;
+       unsigned long rbp;
+       unsigned long rbx;
+/* arguments: non interrupts/non tracing syscalls only save upto here*/
+       unsigned long r11;
+       unsigned long r10;
+       unsigned long r9;
+       unsigned long r8;
+       unsigned long rax;
+       unsigned long rcx;
+       unsigned long rdx;
+       unsigned long rsi;
+       unsigned long rdi;
+       unsigned long orig_rax;
+/* end of arguments */
+/* cpu exception frame or undefined */
+       unsigned long rip;
+       unsigned long cs;
+       unsigned long eflags;
+       unsigned long rsp;
+       unsigned long ss;
+/* top of stack page */
+};
+#endif /* !__ASSEMBLY__ */
+
+#define __REGS_OFFSETOF_PAD       0
+#define __REGS_OFFSETOF_R15       8
+#define __REGS_OFFSETOF_R14       16
+#define __REGS_OFFSETOF_R13       24
+#define __REGS_OFFSETOF_R12       32
+#define __REGS_OFFSETOF_RBP       40
+#define __REGS_OFFSETOF_RBX       48
+#define __REGS_OFFSETOF_R11       56
+#define __REGS_OFFSETOF_R10       64
+#define __REGS_OFFSETOF_R9        72
+#define __REGS_OFFSETOF_R8        80
+#define __REGS_OFFSETOF_RAX       88
+#define __REGS_OFFSETOF_RCX       96
+#define __REGS_OFFSETOF_RDX       104
+#define __REGS_OFFSETOF_RSI       112
+#define __REGS_OFFSETOF_RDI       120
+#define __REGS_OFFSETOF_ORIG_RAX  128
+#define __REGS_OFFSETOF_RIP       136
+#define __REGS_OFFSETOF_CS        144
+#define __REGS_OFFSETOF_EFLAGS    152
+#define __REGS_OFFSETOF_RSP       160
+#define __REGS_OFFSETOF_SS        168
+
+#define __REGS_PAD_SIZE           __REGS_OFFSETOF_R15
+#define __REGS_SIZEOF             176
+
+/* sanity check */
+#if __REGS_SIZEOF & 0xf
+#error "__regs structure size should be multiple of 16."
+#endif
+
+#ifndef __ASSEMBLY__
 #ifndef mb
 #define mb()    __asm__ __volatile__ ("mfence" : : : "memory")
 #endif
@@ -55,3 +118,4 @@ static inline unsigned long ukarch_read_sp(void)
        __asm__ __volatile__("mov %%rsp, %0" : "=r"(sp));
        return sp;
 }
+#endif /* !__ASSEMBLY__ */
diff --git a/include/uk/arch/lcpu.h b/include/uk/arch/lcpu.h
index 83ea6ecc..c5216287 100644
--- a/include/uk/arch/lcpu.h
+++ b/include/uk/arch/lcpu.h
@@ -36,7 +36,9 @@
 #ifndef __UKARCH_LCPU_H__
 #define __UKARCH_LCPU_H__
 
+#ifndef __ASSEMBLY__
 #include <uk/arch/types.h>
+#endif /* !__ASSEMBLY__ */
 
 #ifdef __cplusplus
 extern "C" {
@@ -44,6 +46,7 @@ extern "C" {
 
 #include <uk/asm/lcpu.h>
 
+#ifndef __ASSEMBLY__
 #ifndef likely
 #define likely(x)   (__builtin_expect((!!(x)), 1))
 #endif
@@ -67,6 +70,7 @@ extern "C" {
 #ifndef wmb
 #define wmb()     barrier()
 #endif
+#endif /* !__ASSEMBLY__ */
 
 #ifdef __cplusplus
 }
diff --git a/plat/common/include/uk/plat/common/trace.h 
b/plat/common/include/uk/plat/common/trace.h
index a423153e..bc3b6de8 100644
--- a/plat/common/include/uk/plat/common/trace.h
+++ b/plat/common/include/uk/plat/common/trace.h
@@ -34,11 +34,7 @@
 #ifndef __PLAT_CMN_TRACE_H__
 #define __PLAT_CMN_TRACE_H__
 
-#ifdef __X86_64__
-#include <x86/regs.h> /* TODO include a generic header */
-#else
-#error "Create regs.h for current architecture"
-#endif
+#include <uk/arch/lcpu.h>
 
 void dump_regs(struct __regs *regs);
 void dump_mem(unsigned long addr);
diff --git a/plat/common/include/x86/regs.h b/plat/common/include/x86/regs.h
deleted file mode 100644
index a6a847e4..00000000
--- a/plat/common/include/x86/regs.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/* SPDX-License-Identifier: BSD-2-Clause */
-/*
- * Copyright (c) 2005, Grzegorz Milos, Intel Research Cambridge
- * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-#ifndef __UKARCH_REGS_H__
-#define __UKARCH_REGS_H__
-
-#ifndef __ASSEMBLY__
-struct __regs {
-       unsigned long pad; /* for 16 bytes alignment */
-       unsigned long r15;
-       unsigned long r14;
-       unsigned long r13;
-       unsigned long r12;
-       unsigned long rbp;
-       unsigned long rbx;
-/* arguments: non interrupts/non tracing syscalls only save upto here*/
-       unsigned long r11;
-       unsigned long r10;
-       unsigned long r9;
-       unsigned long r8;
-       unsigned long rax;
-       unsigned long rcx;
-       unsigned long rdx;
-       unsigned long rsi;
-       unsigned long rdi;
-       unsigned long orig_rax;
-/* end of arguments */
-/* cpu exception frame or undefined */
-       unsigned long rip;
-       unsigned long cs;
-       unsigned long eflags;
-       unsigned long rsp;
-       unsigned long ss;
-/* top of stack page */
-};
-#endif
-
-#define OFFSETOF_REGS_PAD       0
-#define OFFSETOF_REGS_R15       8
-#define OFFSETOF_REGS_R14       16
-#define OFFSETOF_REGS_R13       24
-#define OFFSETOF_REGS_R12       32
-#define OFFSETOF_REGS_RBP       40
-#define OFFSETOF_REGS_RBX       48
-#define OFFSETOF_REGS_R11       56
-#define OFFSETOF_REGS_R10       64
-#define OFFSETOF_REGS_R9        72
-#define OFFSETOF_REGS_R8        80
-#define OFFSETOF_REGS_RAX       88
-#define OFFSETOF_REGS_RCX       96
-#define OFFSETOF_REGS_RDX       104
-#define OFFSETOF_REGS_RSI       112
-#define OFFSETOF_REGS_RDI       120
-#define OFFSETOF_REGS_ORIG_RAX  128
-#define OFFSETOF_REGS_RIP       136
-#define OFFSETOF_REGS_CS        144
-#define OFFSETOF_REGS_EFLAGS    152
-#define OFFSETOF_REGS_RSP       160
-#define OFFSETOF_REGS_SS        168
-
-#define REGS_PAD_SIZE           OFFSETOF_REGS_R15
-#define SIZEOF_REGS             176
-
-#if SIZEOF_REGS & 0xf
-#error "__regs structure size should be multiple of 16."
-#endif
-
-/* This should be better defined in the thread header */
-#define OFFSETOF_UKTHREAD_REGS  16
-
-#endif /* __UKARCH_REGS_H__ */
diff --git a/plat/common/include/x86/traps.h b/plat/common/include/x86/traps.h
index b217b34a..da90f2ea 100644
--- a/plat/common/include/x86/traps.h
+++ b/plat/common/include/x86/traps.h
@@ -36,7 +36,7 @@
 #ifndef __UKARCH_TRAPS_X86_64_H__
 #define __UKARCH_TRAPS_X86_64_H__
 
-#include <x86/regs.h>
+#include <uk/arch/lcpu.h>
 
 #define TRAP_divide_error        0
 #define TRAP_debug               1
diff --git a/plat/kvm/x86/cpu_vectors_x86_64.S 
b/plat/kvm/x86/cpu_vectors_x86_64.S
index 1af2fd00..35aa58d2 100644
--- a/plat/kvm/x86/cpu_vectors_x86_64.S
+++ b/plat/kvm/x86/cpu_vectors_x86_64.S
@@ -25,7 +25,6 @@
 /* Taken from solo5 */
 
 #include <x86/traps.h>
-#include <x86/regs.h>
 
 #define ENTRY(X)     .global X ; .type X, @function ; X:
 
@@ -73,13 +72,13 @@ ENTRY(ASM_TRAP_SYM(\trapname))
        pushq $0                            /* no error code, pass 0 */
 .endif
        PUSH_CALLER_SAVE
-       subq $REGS_PAD_SIZE, %rsp           /* we have some padding */
+       subq $__REGS_PAD_SIZE, %rsp         /* we have some padding */
 
        movq %rsp, %rdi
-       movq OFFSETOF_REGS_ORIG_RAX(%rsp), %rsi
+       movq __REGS_OFFSETOF_ORIG_RAX(%rsp), %rsi
        call do_\trapname
 
-       addq $REGS_PAD_SIZE, %rsp           /* we have some padding */
+       addq $__REGS_PAD_SIZE, %rsp         /* we have some padding */
        POP_CALLER_SAVE
        addq $8, %rsp                       /* discard error code */
 
@@ -92,12 +91,12 @@ ENTRY(cpu_irq_\irqno)
 
        pushq $0                            /* no error code */
        PUSH_CALLER_SAVE
-       subq $REGS_PAD_SIZE, %rsp           /* we have some padding */
+       subq $__REGS_PAD_SIZE, %rsp         /* we have some padding */
 
        movq $\irqno, %rdi
        call _ukplat_irq_handle
 
-       addq $REGS_PAD_SIZE, %rsp           /* we have some padding */
+       addq $__REGS_PAD_SIZE, %rsp         /* we have some padding */
        POP_CALLER_SAVE
        addq $8, %rsp
 
diff --git a/plat/xen/x86/entry64.S b/plat/xen/x86/entry64.S
index d34181be..ae118e1d 100644
--- a/plat/xen/x86/entry64.S
+++ b/plat/xen/x86/entry64.S
@@ -183,44 +183,44 @@ ENTRY(ASM_TRAP_SYM(\trapname))
 .endm
 
 .macro RESTORE_ALL
-       movq OFFSETOF_REGS_R15(%rsp), %r15
-       movq OFFSETOF_REGS_R14(%rsp), %r14
-       movq OFFSETOF_REGS_R13(%rsp), %r13
-       movq OFFSETOF_REGS_R12(%rsp), %r12
-       movq OFFSETOF_REGS_RBP(%rsp), %rbp
-       movq OFFSETOF_REGS_RBX(%rsp), %rbx
-       movq OFFSETOF_REGS_R11(%rsp), %r11
-       movq OFFSETOF_REGS_R10(%rsp), %r10
-       movq OFFSETOF_REGS_R9(%rsp),  %r9
-       movq OFFSETOF_REGS_R8(%rsp),  %r8
-       movq OFFSETOF_REGS_RAX(%rsp), %rax
-       movq OFFSETOF_REGS_RCX(%rsp), %rcx
-       movq OFFSETOF_REGS_RDX(%rsp), %rdx
-       movq OFFSETOF_REGS_RSI(%rsp), %rsi
-       movq OFFSETOF_REGS_RDI(%rsp), %rdi
-       addq $OFFSETOF_REGS_RIP,%rsp
+       movq __REGS_OFFSETOF_R15(%rsp), %r15
+       movq __REGS_OFFSETOF_R14(%rsp), %r14
+       movq __REGS_OFFSETOF_R13(%rsp), %r13
+       movq __REGS_OFFSETOF_R12(%rsp), %r12
+       movq __REGS_OFFSETOF_RBP(%rsp), %rbp
+       movq __REGS_OFFSETOF_RBX(%rsp), %rbx
+       movq __REGS_OFFSETOF_R11(%rsp), %r11
+       movq __REGS_OFFSETOF_R10(%rsp), %r10
+       movq __REGS_OFFSETOF_R9(%rsp),  %r9
+       movq __REGS_OFFSETOF_R8(%rsp),  %r8
+       movq __REGS_OFFSETOF_RAX(%rsp), %rax
+       movq __REGS_OFFSETOF_RCX(%rsp), %rcx
+       movq __REGS_OFFSETOF_RDX(%rsp), %rdx
+       movq __REGS_OFFSETOF_RSI(%rsp), %rsi
+       movq __REGS_OFFSETOF_RDI(%rsp), %rdi
+       addq $__REGS_OFFSETOF_RIP,%rsp
 .endm
 
 .macro SAVE_ALL
        /* rdi slot contains rax, oldrax contains error code */
        cld
-       subq $OFFSETOF_REGS_RDI, %rsp
-       movq %rsi, OFFSETOF_REGS_RSI(%rsp)
-       movq OFFSETOF_REGS_RDI(%rsp), %rsi    /* load rax from rdi slot */
-       movq %rdx, OFFSETOF_REGS_RDX(%rsp)
-       movq %rcx, OFFSETOF_REGS_RCX(%rsp)
-       movq %rsi, OFFSETOF_REGS_RAX(%rsp)    /* store rax */
-       movq %r8,  OFFSETOF_REGS_R8(%rsp)
-       movq %r9,  OFFSETOF_REGS_R9(%rsp)
-       movq %r10, OFFSETOF_REGS_R10(%rsp)
-       movq %r11, OFFSETOF_REGS_R11(%rsp)
-       movq %rbx, OFFSETOF_REGS_RBX(%rsp)
-       movq %rbp, OFFSETOF_REGS_RBP(%rsp)
-       movq %r12, OFFSETOF_REGS_R12(%rsp)
-       movq %r13, OFFSETOF_REGS_R13(%rsp)
-       movq %r14, OFFSETOF_REGS_R14(%rsp)
-       movq %r15, OFFSETOF_REGS_R15(%rsp)
-       movq %rdi, OFFSETOF_REGS_RDI(%rsp)    /* put rdi into the slot */
+       subq $__REGS_OFFSETOF_RDI, %rsp
+       movq %rsi, __REGS_OFFSETOF_RSI(%rsp)
+       movq __REGS_OFFSETOF_RDI(%rsp), %rsi    /* load rax from rdi slot */
+       movq %rdx, __REGS_OFFSETOF_RDX(%rsp)
+       movq %rcx, __REGS_OFFSETOF_RCX(%rsp)
+       movq %rsi, __REGS_OFFSETOF_RAX(%rsp)    /* store rax */
+       movq %r8,  __REGS_OFFSETOF_R8(%rsp)
+       movq %r9,  __REGS_OFFSETOF_R9(%rsp)
+       movq %r10, __REGS_OFFSETOF_R10(%rsp)
+       movq %r11, __REGS_OFFSETOF_R11(%rsp)
+       movq %rbx, __REGS_OFFSETOF_RBX(%rsp)
+       movq %rbp, __REGS_OFFSETOF_RBP(%rsp)
+       movq %r12, __REGS_OFFSETOF_R12(%rsp)
+       movq %r13, __REGS_OFFSETOF_R13(%rsp)
+       movq %r14, __REGS_OFFSETOF_R14(%rsp)
+       movq %r15, __REGS_OFFSETOF_R15(%rsp)
+       movq %rdi, __REGS_OFFSETOF_RDI(%rsp)    /* put rdi into the slot */
 .endm
 
 .macro HYPERVISOR_IRET
@@ -251,8 +251,8 @@ error_entry:
        SAVE_ALL
 
        movq %rsp,%rdi
-       movq OFFSETOF_REGS_ORIG_RAX(%rsp),%rsi  # get error code
-       movq $-1,OFFSETOF_REGS_ORIG_RAX(%rsp)
+       movq __REGS_OFFSETOF_ORIG_RAX(%rsp),%rsi        # get error code
+       movq $-1,__REGS_OFFSETOF_ORIG_RAX(%rsp)
        call *%rax
        jmp error_exit
 
@@ -268,7 +268,7 @@ hypervisor_callback2:
        movq %rdi, %rsp
 
        /* check against event re-entrant */
-       movq OFFSETOF_REGS_RIP(%rsp),%rax
+       movq __REGS_OFFSETOF_RIP(%rsp),%rax
        cmpq $scrit,%rax
        jb 11f
        cmpq $ecrit,%rax
@@ -283,7 +283,7 @@ hypervisor_callback2:
        decl %gs:0
 
 error_exit:
-       movl OFFSETOF_REGS_EFLAGS(%rsp), %eax
+       movl __REGS_OFFSETOF_EFLAGS(%rsp), %eax
        shr $9, %eax                    # EAX[0] == IRET_RFLAGS.IF
        XEN_GET_VCPU_INFO(%rsi)
        andb evtchn_upcall_mask(%rsi),%al
@@ -329,11 +329,11 @@ hypervisor_prologue:
 
 critical_region_fixup:
        # Set up source and destination region pointers
-       leaq OFFSETOF_REGS_RIP(%rsp),%rsi   # esi points at end of src region
+       leaq __REGS_OFFSETOF_RIP(%rsp),%rsi   # esi points at end of src region
        # Acquire interrupted rsp which was saved-on-stack. This points to
        # the end of dst region. Note that it is not necessarily current rsp
        # plus 0xb0, because the second interrupt might align the stack frame.
-       movq OFFSETOF_REGS_RSP(%rsp),%rdi   # edi points at end of dst region
+       movq __REGS_OFFSETOF_RSP(%rsp),%rdi   # edi points at end of dst region
 
        cmpq $restore_end,%rax
        jae  13f
@@ -341,11 +341,11 @@ critical_region_fixup:
        # If interrupted rip is before restore_end
        # then rax hasn't been restored yet
        movq (%rdi),%rax
-       movq %rax, OFFSETOF_REGS_RAX(%rsp)  # save rax
+       movq %rax, __REGS_OFFSETOF_RAX(%rsp)  # save rax
        addq $RSP_OFFSET,%rdi
 
        # Set up the copy
-13:    movq $OFFSETOF_REGS_RIP,%rcx
+13:    movq $__REGS_OFFSETOF_RIP,%rcx
        shr  $3,%rcx          # convert bytes into count of 64-bit entities
 15:    subq $8,%rsi          # pre-decrementing copy loop
        subq $8,%rdi
@@ -353,7 +353,7 @@ critical_region_fixup:
        movq %rax,(%rdi)
        loop 15b
 16:    movq %rdi,%rsp        # final rdi is top of merged stack
-       andb $KERNEL_CS_MASK,OFFSETOF_REGS_CS(%rsp)      # CS might have changed
+       andb $KERNEL_CS_MASK,__REGS_OFFSETOF_CS(%rsp)  # CS might have changed
        jmp  11b
 
 #else
-- 
2.20.1

_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.