Cleanup of Xeon Phi Kernel:
authorReto Achermann <reto.achermann@inf.ethz.ch>
Wed, 26 Nov 2014 15:50:05 +0000 (16:50 +0100)
committerReto Achermann <reto.achermann@inf.ethz.ch>
Wed, 26 Nov 2014 15:50:05 +0000 (16:50 +0100)
Merged duplicated code from k1om architecture with the X86_64 architecture.
No-longer files in arch/k1om have been deleted.

35 files changed:
hake/K1om.hs
hake/menu.lst.k1om
hake/menu.lst.x86_64
hake/symbolic_targets.mk
kernel/Hakefile
kernel/arch/k1om/boot.S [deleted file]
kernel/arch/k1om/debug.c [deleted file]
kernel/arch/k1om/entry.S [deleted file]
kernel/arch/k1om/exec.c [deleted file]
kernel/arch/k1om/gdb_arch.c [deleted file]
kernel/arch/k1om/init.c
kernel/arch/k1om/irq.c [deleted file]
kernel/arch/k1om/kernel.S [deleted file]
kernel/arch/k1om/linker.lds.in
kernel/arch/k1om/microbenchmarks.c [deleted file]
kernel/arch/k1om/page_mappings_arch.c [deleted file]
kernel/arch/k1om/paging.c [deleted file]
kernel/arch/k1om/startup_arch.c
kernel/arch/k1om/syscall.c [deleted file]
kernel/arch/k1om/vmkit.c [deleted file]
kernel/arch/x86_64/boot.S
kernel/arch/x86_64/entry.S
kernel/arch/x86_64/exec.c
kernel/arch/x86_64/gdb_arch.c
kernel/arch/x86_64/irq.c
kernel/arch/x86_64/page_mappings_arch.c
kernel/arch/x86_64/paging.c
kernel/arch/x86_64/startup_arch.c
kernel/arch/x86_64/syscall.c
kernel/capabilities.c
kernel/include/arch/k1om/paging_kernel_arch.h
kernel/include/target/k1om/offsets_target.h
kernel/include/target/k1om/paging_kernel_target.h [deleted file]
kernel/include/target/x86_64/offsets_target.h
kernel/include/target/x86_64/paging_kernel_target.h

index 301da55..2be496e 100644 (file)
@@ -103,7 +103,7 @@ kernelCFlags = [ Str s | s <- [ "-fno-builtin",
                                 "-std=c99",
                                 "-m64",
                                 "-fPIE", 
-                                "-e kernel_start",
+                                "-e start",
                                 "-mno-red-zone",
                                 "-mk1om",
                                 "-Wa,-march=k1om",
@@ -144,7 +144,7 @@ kernelLdFlags = [ Str s | s <- [ "-Wl,-N ",
                                  "-pie ",
                                  "-Wl,-melf_k1om ",
                                  "-fno-builtin ",
-                                 "-e kernel_start",
+                                 "-e start",
                                  "-nostdlib ",
                                  "-Wl,--fatal-warnings ",
                                  "-m64 " ] ]
index 89a2c70..ef07dad 100644 (file)
@@ -23,9 +23,12 @@ module       /k1om/sbin/monitor
 module  /k1om/sbin/ramfsd boot
 module  /k1om/sbin/skb boot
 module  /k1om/sbin/xeon_phi boot
-module  /k1om/sbin/spawnd boot bootk1om=57
+module  /k1om/sbin/spawnd boot
 module  /k1om/sbin/startd boot
 
+# drivers
+module /k1om/sbin/corectrl boot 1:10
+
 # GDDR Memory we have 6GB on our Xeon PHi
 mmap map 0x0000000000 0x00FEE00000 1
 # memory hole for the APIC and the flash rom 
index 60f35f8..0ee2956 100644 (file)
@@ -48,3 +48,4 @@ module /x86_64/sbin/netd auto
 module /x86_64/sbin/serial
 module  /x86_64/sbin/fish nospawn
 module /x86_64/sbin/angler serial0.terminal xterm
+
index cc410f5..4cf0d42 100644 (file)
@@ -27,7 +27,7 @@ ARM_OBJCOPY?=arm-linux-gnueabi-objcopy
 K1OM_OBJCOPY?=k1om-mpss-linux-objcopy
 
 # upload Xeon Phi images to nfs share (leave blank to cancel)
-BARRELFISH_NFS_DIR ?="emmentaler.ethz.ch:/local/nfs/barrelfish/xeon_phi"
+BARRELFISH_NFS_DIR ?="emmentaler.ethz.ch:/mnt/local/nfs/barrelfish/xeon_phi"
 
 # All binaries of the RCCE LU benchmark
 BIN_RCCE_LU= \
@@ -241,9 +241,10 @@ MODULES_x86_64= \
        sbin/corectrl 
 
 MODULES_k1om= \
-        sbin/weever \
+       sbin/weever \
        sbin/cpu \
        sbin/xeon_phi \
+       sbin/corectrl \
        xeon_phi_multiboot \
        $(GREEN_MARL)
 
index 389f2dc..8c176a7 100644 (file)
@@ -138,29 +138,14 @@ let
   --
   cpuDriver { 
      architectures = [ "k1om" ],
-     assemblyFiles = [ "arch/k1om/boot.S",
-                       "arch/k1om/entry.S", 
+     assemblyFiles = [ "arch/x86_64/boot.S",
+                       "arch/x86_64/entry.S", 
                        "../usr/drivers/cpuboot/init_ap_x86_64.S", 
                        "../usr/drivers/cpuboot/init_ap_x86_32.S" ],
-     cFiles = [ "arch/k1om/debug.c", 
-                "arch/k1om/gdb_arch.c", 
-                "arch/k1om/init.c", 
-                "arch/k1om/irq.c",
-                "arch/k1om/startup_arch.c", 
-                "arch/k1om/exec.c", 
-                "arch/k1om/mcheck.c", 
-                "arch/k1om/syscall.c", 
-                "arch/k1om/paging.c",
-                "arch/k1om/vmkit.c" , 
-                "arch/k1om/page_mappings_arch.c",
-                "arch/k1om/serial.c",
-                "arch/k1om/xeon_phi.c",
-                "arch/x86/apic.c", 
-                "arch/x86/pic.c", 
-                "arch/x86/cmos.c", 
+     cFiles = [ "arch/x86/apic.c", 
+    --          "arch/x86/pic.c", 
                 "arch/x86/misc.c",
                 "arch/x86/multiboot.c", 
-                "arch/x86/conio.c", 
                 "arch/x86/syscall.c", 
                 "arch/x86/debugregs.c",
                 "arch/x86/perfmon.c", 
@@ -169,8 +154,21 @@ let
                 "arch/x86/rtc.c", 
                 "arch/x86/timing.c",
                 "arch/x86/startup_x86.c", 
-                "arch/x86/ipi_notify.c"
-              ],
+                "arch/x86/ipi_notify.c",
+                "arch/x86_64/syscall.c",
+                "arch/x86_64/debug.c", 
+                "arch/x86_64/exec.c", 
+                "arch/x86_64/irq.c",
+                "arch/x86_64/paging.c",
+                "arch/x86_64/page_mappings_arch.c",
+                "arch/x86_64/gdb_arch.c", 
+                "arch/k1om/init.c", 
+                "arch/k1om/startup_arch.c", 
+                "arch/k1om/mcheck.c", 
+                "arch/k1om/serial.c",
+                "arch/k1om/xeon_phi.c"
+
+              ] ++ (if Config.microbenchmarks then ["arch/x86_64/microbenchmarks.c"] else []),
      mackerelDevices = [ "lpc_pic", 
                          "pc16550d", 
                          "ia32", 
diff --git a/kernel/arch/k1om/boot.S b/kernel/arch/k1om/boot.S
deleted file mode 100644 (file)
index fbb7d1b..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * \file
- * \brief Bootstrap the kernel.
- */
-
-/*
- * Copyright (c) 2007, 2008, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <multiboot.h>
-#include <target/k1om/offsets_target.h>
-
-/* The flags for the Multiboot header */
-#define MB_FLAGS (MULTIBOOT_HEADER_FLAG_MODS_PGALIGNED | MULTIBOOT_HEADER_FLAG_NEED_MEMINFO)
-
-          .text
-          .globl    kernel_start, halt
-          /* Multiboot header, 4-byte aligned */
-          .align    4
-          .long     MULTIBOOT_HEADER_MAGIC               /* magic */
-          .long     MB_FLAGS                             /* flags */
-          .long     -(MULTIBOOT_HEADER_MAGIC + MB_FLAGS) /* checksum */
-
-kernel_start:
-          /* Initialize the stack pointer */
-          lea       (k1om_kernel_stack + K1OM_KERNEL_STACK_SIZE)(%rip), %rsp
-
-          /* Reset EFLAGS */
-          pushq     $0
-          popf
-
-          /* Enter architecture-specific init -- this should never return */
-          movq      %rax, %rdi          /* Multiboot magic value */
-          movq      %rbx, %rsi          /* Pointer to multiboot info struct */
-          call      arch_init
-
-          /* Halt -- this should never be reached */
-halt:     hlt
-          jmp       halt
diff --git a/kernel/arch/k1om/debug.c b/kernel/arch/k1om/debug.c
deleted file mode 100644 (file)
index d51787a..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/**
- * \file
- * \brief Kernel debugging functions
- */
-
-/*
- * Copyright (c) 2008, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <kernel.h>
-#include <stdio.h>
-#include <arch/x86/debug.h>
-#include <paging_kernel_arch.h>
-
-union lin_addr {
-    uint64_t raw;
-    struct {
-        uint64_t  offset       :12;
-        uint64_t  ptable       :9;
-        uint64_t  pdir         :9;
-        uint64_t  pdpt         :9;
-        uint64_t  pml4         :9;
-        uint64_t  sign_extend  :16;
-    } d;
-};
-
-void debug_vaddr_identify(lvaddr_t debug_pml4, lvaddr_t vaddr)
-{
-    int i;
-    printf("cr3 register      %lx\n", debug_pml4);
-    printf("identifying vaddr %lx\n", vaddr);
-
-    volatile uint64_t *temp = (uint64_t*)vaddr;
-
-    for(i = 0; i < 512; i++) {
-        printf("at addr %lx content is %lx\n", (uint64_t)(temp + i), *(temp + i));
-    }
-    printf("\n");
-
-    union lin_addr lin_addr;
-    lin_addr.raw = (uint64_t)vaddr;
-
-    printf("vaddr broken down\n");
-    printf("sign_extend = %x\n", lin_addr.d.sign_extend);
-    printf("pml4        = %x\n", lin_addr.d.pml4);
-    printf("pdpt        = %x\n", lin_addr.d.pdpt);
-    printf("pdir        = %x\n", lin_addr.d.pdir);
-    printf("ptable      = %x\n", lin_addr.d.ptable);
-    printf("offset      = %x\n", lin_addr.d.offset);
-
-    uint64_t *pml4et;
-    pml4et = (uint64_t*)(debug_pml4 +
-                         (lin_addr.d.pml4 * sizeof(union x86_64_pdir_entry)));
-    printf("addr = %lx ", (uint64_t)pml4et);
-    printf("content = %lx\n", *pml4et);
-
-    lvaddr_t pdpt_addr;
-    pdpt_addr = local_phys_to_mem(((union x86_64_pdir_entry*)pml4et)->d.base_addr << 12);
-    uint64_t *pdptet;
-    pdptet = (uint64_t*)(pdpt_addr +
-                         (lin_addr.d.pdpt * sizeof(union x86_64_pdir_entry)));
-    printf("addr = %lx ", (uint64_t)pdptet);
-    printf("content = %lx\n", *pdptet);
-
-    lvaddr_t pdir_addr;
-    pdir_addr = local_phys_to_mem(((union x86_64_pdir_entry*)pdptet)->d.base_addr << 12);
-    uint64_t *pdiret;
-    pdiret = (uint64_t*)(pdir_addr +
-                         (lin_addr.d.pdir * sizeof(union x86_64_pdir_entry)));
-    printf("addr = %lx ", (uint64_t)pdiret);
-    printf("content = %lx\n", *pdiret);
-
-    lvaddr_t ptable_addr;
-    ptable_addr = local_phys_to_mem(((union x86_64_pdir_entry*)pdiret)->d.base_addr << 12);
-    uint64_t *ptableet;
-    ptableet = (uint64_t*)(ptable_addr +
-                         (lin_addr.d.ptable * sizeof(union x86_64_pdir_entry)));
-    printf("addr = %lx ", (uint64_t)ptableet);
-    printf("content = %lx\n", *ptableet);
-
-    lpaddr_t addr = ((union x86_64_ptable_entry*)ptableet)->base.base_addr << 12;
-    printf("addr = %lx\n", addr);
-}
diff --git a/kernel/arch/k1om/entry.S b/kernel/arch/k1om/entry.S
deleted file mode 100644 (file)
index e8a382a..0000000
+++ /dev/null
@@ -1,393 +0,0 @@
-/**
- * \file
- * \brief System call entry point to the kernel and LRPC fast-path
- */
-
-/*
- * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <barrelfish_kpi/syscalls.h>
-#include <barrelfish_kpi/capabilities.h>
-#include <barrelfish_kpi/lmp.h>
-#include <target/k1om/offsets_target.h>
-#include <x86.h>
-#include <asmoffsets.h>
-
-    .text
-    .globl syscall_entry
-
-syscall_entry:
-        /* is this an LRPC or a normal syscall? */
-        cmp $SYSCALL_LRPC, %rdi
-        jne  syscall_path   /* normal syscall, branch off */
-
-       /* Load pointer to current DCB */
-       mov     dcb_current(%rip), %rdi
-
-       /* TODO: Check that caller is not disabled */
-
-        /* dcb_current->disabled=false */
-        movb $0, OFFSETOF_DCB_DISABLED(%rdi)
-
-        /* Save caller's registers */
-       mov     OFFSETOF_DCB_DISP(%rdi), %rdi
-       lea     OFFSETOF_DISP_X86_64_ENABLED_AREA(%rdi), %rdi
-       movq    $SYS_ERR_OK, OFFSETOF_RAX_REG(%rdi)
-       mov     %rcx, OFFSETOF_RIP_REG(%rdi)
-       mov     %r11, OFFSETOF_EFLAGS_REG(%rdi)
-       mov     %rsp, OFFSETOF_RSP_REG(%rdi)
-       mov     %fs, OFFSETOF_FS_REG(%rdi)
-       mov     %gs, OFFSETOF_GS_REG(%rdi)
-
-       /* Load pointer to root CNode cap */
-       mov     dcb_current(%rip), %rdi
-       lea     OFFSETOF_DCB_CSPACE_CAP(%rdi), %rdi
-
-       /* Check that slot number is within CNode */
-        movb OFFSETOF_CAP_CNODE_BITS(%rdi), %cl
-        mov $1, %r15
-        shl %cl, %r15
-       cmp     %r15, %rsi
-       jae     err_slot
-
-       /* Load pointer to endpoint cap */
-       shl     $OBJBITS_CTE, %rsi
-       mov     OFFSETOF_CAP_CNODE_CNODE(%rdi), %rcx
-       // XXX: this shoud not be hardcoded! -RA
-       mov     $0xffffff0000000000, %rdi       // phys_to_mem()
-       add     %rdi, %rcx
-       add     %rsi, %rcx
-
-       /* Check that it's an endpoint */
-       cmpl    $OBJTYPE_ENDPOINT, OFFSETOF_CAP_TYPE(%rcx)
-       jne     err_endpoint
-
-       /* TODO: Check rights on the endpoint */
-
-       /* Set epoffset for receiver, load epbuflen */
-       mov     OFFSETOF_CAP_ENDPOINT_EPOFFSET(%rcx), %rdi
-        mov     OFFSETOF_CAP_ENDPOINT_EPBUFLEN(%rcx), %r13d /* r13d = epbuflen */
-
-       /* Load pointer to listener's DCB */
-       mov     OFFSETOF_CAP_ENDPOINT_LISTENER(%rcx), %rsi
-
-       /* Check whether listener is runnable */
-#if defined(CONFIG_SCHEDULER_RR)
-       cmpl    $0, OFFSETOF_DCB_RR_PREV(%rsi)
-       je      lrpc_make_runnable
-#elif defined(CONFIG_SCHEDULER_RBED)
-       cmpl    $0, OFFSETOF_DCB_RBED_NEXT(%rsi)
-       je      lrpc_rbed_check_runnable
-#else
-# error Must define a kernel scheduling policy!
-#endif
-
-lrpc_check_runnable_continue:
-       /* Check whether listener is disabled */
-       cmpb    $0, OFFSETOF_DCB_DISABLED(%rsi)
-       jne     err_disabled
-
-        /* RCX = target dispatcher */
-        mov OFFSETOF_DCB_DISP(%rsi), %rcx
-
-        /* Remember LRPC entry point on target (R15) */
-        mov OFFSETOF_DISP_LRPC(%rcx), %r15
-
-        /* check that the receiver has space in their buffer */
-        add %rdi, %rcx /* add epoffset to dispatcher: rcx = endpoint pointer */
-        mov OFFSETOF_LMP_ENDPOINT_DELIVERED(%rcx), %r11d /* r11d = delivered */
-        mov %r11d, %r12d /* r12d = delivered */
-        mov OFFSETOF_LMP_ENDPOINT_CONSUMED(%rcx), %r14d /* r14d = consumed */
-
-        /*
-         *  newpos = delivered + len;
-         *  if (newpos >= consumed && consumed > delivered)
-         *    goto err_buflen;
-         *  if (newpos >= epbuflen) {
-         *    newpos -= epbuflen;
-         *    if (newpos >= consumed)
-         *      goto err_buflen;
-         *  }
-         *  delivered = newpos
-         */
-
-        add $(LRPC_MSG_LENGTH + LMP_RECV_HEADER_LENGTH), %r11d /* r11d (newpos) = delivered + len */
-
-        cmp %r14d, %r11d
-        jb 1f /* if newpos < consumed */
-        cmp %r12d, %r14d
-        ja err_buflen /* if consumed > delivered */
-
-1:
-        cmp %r13d, %r11d
-        jb 2f /* if newpos < epbuflen */
-
-        /* newpos >= epbuflen */
-        sub %r13d, %r11d /* newpos (r11d) -= epbuflen (r13d) */
-        cmp %r14d, %r11d /* if newpos >= consumed */
-        jae err_buflen
-
-2:      /* there's enough space, reserve it by updating delivered = newpos */
-        mov %r11d, OFFSETOF_LMP_ENDPOINT_DELIVERED(%rcx)
-
-       /* Set current domain to receiver */
-       mov     %rsi, dcb_current(%rip)
-
-       /* Switch to listener address space */
-       mov     OFFSETOF_DCB_VSPACE(%rsi), %rax
-       mov     %rax, %cr3
-
-        /* Zero registers to avoid the receiver getting hold of them
-         * FIXME: should zero all non-payload registers */
-        xor     %eax, %eax
-        mov     %eax, %fs
-        mov     %eax, %gs
-
-       /* Get new dispatcher pointer */
-       mov     OFFSETOF_DCB_DISP(%rsi), %rax
-       /* Disable target dispatcher -- gotta do it here for TLB hit reasons */
-       movl    $1, OFFSETOF_DISP_DISABLED(%rax)
-        /* update dispatcher's global delivered count */
-        addl    $(LRPC_MSG_LENGTH + LMP_RECV_HEADER_LENGTH), OFFSETOF_DISP_LMP_DELIVERED(%rax)
-        /* update systime field in dispatcher from kernel_now variable */
-        movq    kernel_now(%rip), %r11
-        movq    %r11, OFFSETOF_DISP_SYSTIME(%rax)
-
-        /* Check if it's necessary to load a new LDT */
-       mov     OFFSETOF_DISP_X86_64_LDT_BASE(%rax), %r11
-        mov     OFFSETOF_DISP_X86_64_LDT_NPAGES(%rax), %r14
-        cmp     current_ldt_base(%rip), %r11
-        jne load_ldt
-
-        cmp     current_ldt_npages(%rip), %r14
-        jne load_ldt
-
-load_ldt_continue:
-       /* Enter at LRPC entry point */
-        mov     %r12d, %esi            /* bufpos of reserved space in EP buffer */
-       mov     %r15, %rcx             /* saved LRPC EP */
-        movq    OFFSETOF_DISP_UDISP(%rax), %rax /* user-level dispatcher pointer */
-       mov     $USER_RFLAGS, %r11  /* eflags */
-       sysretq
-
-load_ldt: /* Load a new LDT: r11 = base, r14 = npages, rcx = temp for descriptor */
-
-        /* If either base or npages is zero, load an invalid LDT */
-        cmpq    $0, %r11
-        je load_ldt_invalid
-        cmpq    $0, %r14
-        je load_ldt_invalid
-
-        /* Update segment descriptor for LDT */
-
-        movq    %r11, current_ldt_base(%rip)
-        movq    %r14, current_ldt_npages(%rip)
-
-        /* Format of high word of descriptor is:
-         * 32 bits of zero/reserved
-         * Base bits 63-32 */
-        mov %r11, %rcx
-        shr $32, %rcx
-        shl $32, %rcx
-
-        // Store new descriptor (high half) to GDT
-        mov %rcx, (gdt + 8*LDT_HI_SEL)(%rip)
-
-        /* Format of low word of descriptor is:
-         * Base bits 31-24 (top 8 bits of 32-bit addr)
-         * 16 bits of flags/miscellany: 0x80e2
-         *   granularity = 1
-         *   operation_size = irrelevant
-         *   long_mode = irrelevant
-         *   available = irrelevant
-         *   4 high bits of limit address = 0 (assuming LDT is < 2**16 * 4k)
-         *   present = 1
-         *   privilege_level (2 bits wide) = 3 (user privilege)
-         *   system descriptor = 0
-         *   type (4 bits wide) = 2
-         * low 24 bits of base addr
-         * low 16 bits of limit
-         */
-
-        // bits 24:31 of base
-        mov %r11, %rcx
-        shr $24, %rcx
-
-        // flags/misc
-        shl $16, %rcx
-        or  $0x80e2, %rcx
-
-        // low 24 bits of base
-        shl $24, %rcx
-        shl $40, %r11
-        shr $40, %r11
-        or  %r11, %rcx
-
-        // low 16 bits of limit
-        shl $16, %rcx
-        shl $48, %r14
-        shr $48, %r14
-        or  %r14, %rcx
-
-        // Store new descriptor (low half) to GDT
-        mov %rcx, (gdt + 8*LDT_LO_SEL)(%rip)
-
-        // Construct segment selector and load it
-        mov     $LDT_SELECTOR, %cx
-        lldt    %cx
-       jmp     load_ldt_continue
-
-load_ldt_invalid:  /* Load an invalid LDT */
-        mov     $0, %cx
-        lldt    %cx
-        movq    $0, current_ldt_base(%rip)
-        movq    $0, current_ldt_npages(%rip)
-        jmp     load_ldt_continue
-
-err_slot:      // Wrong slot
-       mov     $SYS_ERR_LRPC_SLOT_INVALID, %rax
-       jmp     err
-
-err_endpoint:  // Not an endpoint
-       mov     $SYS_ERR_LRPC_NOT_ENDPOINT, %rax
-       /* jmp  err  - fall through */
-
-    /* An error occured */
-err:
-    /* Restore user's state */
-    mov dcb_current(%rip), %rdi
-    mov OFFSETOF_DCB_DISP(%rdi), %rdi
-    lea OFFSETOF_DISP_X86_64_ENABLED_AREA(%rdi), %rdi
-    mov OFFSETOF_RIP_REG(%rdi), %rcx
-    mov OFFSETOF_EFLAGS_REG(%rdi), %r11
-    mov OFFSETOF_RSP_REG(%rdi), %rsp
-    sysretq
-
-err_disabled:  // Target disabled
-    /* Return error to caller in their enabled save area */
-    mov dcb_current(%rip), %rdi
-    mov OFFSETOF_DCB_DISP(%rdi), %rdi
-    lea OFFSETOF_DISP_X86_64_ENABLED_AREA(%rdi), %rdi
-    movq $SYS_ERR_LMP_TARGET_DISABLED, OFFSETOF_RAX_REG(%rdi)
-
-    /* Yield to target (call dispatch(target) in C) */
-    mov %rsi, %rdi /* rdi = target DCB */
-    lea (k1om_kernel_stack + K1OM_KERNEL_STACK_SIZE)(%rip), %rsp
-    jmp dispatch /* no return */
-
-err_buflen:     /* Target's endpoint buffer is full */
-    /* Return error to caller in their enabled save area */
-    mov dcb_current(%rip), %rdi
-    mov OFFSETOF_DCB_DISP(%rdi), %rdi
-    lea OFFSETOF_DISP_X86_64_ENABLED_AREA(%rdi), %rdi
-    movq $SYS_ERR_LMP_BUF_OVERFLOW, OFFSETOF_RAX_REG(%rdi)
-
-    /* Yield to target (call dispatch(target) in C) */
-    mov %rsi, %rdi /* rdi = target DCB */
-    lea (k1om_kernel_stack + K1OM_KERNEL_STACK_SIZE)(%rip), %rsp
-    jmp dispatch /* no return */
-
-#ifdef CONFIG_SCHEDULER_RBED
-lrpc_rbed_check_runnable:
-       cmp     queue_tail(%rip), %rsi
-       jne     lrpc_make_runnable
-       jmp     lrpc_check_runnable_continue
-#endif
-
-lrpc_make_runnable:
-       /* Save user stack */
-       movq    %rsp, user_stack_save(%rip)
-
-       /* Get kernel stack */
-       lea     (k1om_kernel_stack + K1OM_KERNEL_STACK_SIZE)(%rip), %rsp
-
-       // Save complete register state
-       pushq   %rdx
-       pushq   %rcx
-       pushq   %rbx
-       pushq   %rax
-       pushq   %r15
-       pushq   %r14
-       pushq   %r13
-       pushq   %r12
-       pushq   %r11
-       pushq   %r10
-       pushq   %r9
-       pushq   %r8
-       pushq   %rbp
-       pushq   %rdi
-       pushq   %rsi
-
-       // Call make runnable in C
-       movq    %rsi, %rdi
-       callq   make_runnable
-
-       // Restore complete register state
-       popq    %rsi
-       popq    %rdi
-       popq    %rbp
-       popq    %r8
-       popq    %r9
-       popq    %r10
-       popq    %r11
-       popq    %r12
-       popq    %r13
-       popq    %r14
-       popq    %r15
-       popq    %rax
-       popq    %rbx
-       popq    %rcx
-       popq    %rdx
-
-       /* Restore user stack */
-       movq    user_stack_save(%rip), %rsp
-
-       // Jump back
-       jmp     lrpc_check_runnable_continue
-
-
-/* regular syscall path */
-syscall_path:
-        /* Save user stack */
-        movq    %rsp, user_stack_save(%rip)
-
-        /* Get kernel stack */
-        lea (k1om_kernel_stack + K1OM_KERNEL_STACK_SIZE)(%rip), %rsp
-
-        pushq   %rcx            /* Save user-space RIP */
-        pushq   %r11            /* Save user-space RFLAGS */
-
-        pushq   %rbx            /* arg11 */
-        pushq   %rbp            /* arg10 */
-        pushq   %rax            /* arg9 */
-        pushq   %r15            /* arg8 */
-        pushq   %r14            /* arg7 */
-        pushq   %r13            /* arg6 */
-        pushq   %r12            /* arg5 */
-        pushq   %r9             /* arg4 */
-        pushq   %r8             /* arg3 */
-        pushq   %r10            /* arg2 in r10, NOT rcx from syscall */
-
-        /* syscall number is in rdi (1st function argument) */
-        /* arg0 is in rsi (2nd function argument) */
-        /* arg1 is in rdx (3rd function argument) */
-        movq    %r11, %r8   /* 5th function argument is user's flags */
-        movq    %rcx, %r9   /* 6th function argument is user's IP */
-        movq    %rsp, %rcx  /* 4th function argument is pointer to arg buffer */
-
-        callq   sys_syscall     /* Process system call in C */
-
-        addq    $0x50, %rsp     /* Remove buffer from stack */
-        popq    %r11            /* Restore RFLAGS */
-        popq    %rcx            /* Restore RIP */
-        movq    user_stack_save(%rip), %rsp /* Restore user stack */
-        sysretq             /* Return to user-space */
-
-       .bss
-       .comm   user_stack_save, 8
diff --git a/kernel/arch/k1om/exec.c b/kernel/arch/k1om/exec.c
deleted file mode 100644 (file)
index c67a7b7..0000000
+++ /dev/null
@@ -1,259 +0,0 @@
-/**
- * \file
- * \brief x86-64 execution and miscellany
- */
-
-/*
- * Copyright (c) 2007, 2008, 2009, 2010, 2011, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <kernel.h>
-#include <init.h>
-#include <barrelfish_kpi/cpu.h>
-#include <barrelfish_kpi/cpu_arch.h>
-#include <exec.h>
-#include <irq.h>
-#include <x86.h>
-#include <dispatch.h>
-#include <target/k1om/barrelfish_kpi/cpu_target.h>
-
-/**
- * \brief Reboots the system.
- *
- * This function tries hard not to return.
- */
-void reboot(void)
-{
-    struct region_descriptor region = {
-        .rd_limit = 0,
-        .rd_base = 0
-    };
-
-    printk(LOG_NOTE, "Rebooting...\n");
-
-    // try PCI reset register
-    uint8_t val = inb(0xcf9) & ~0x6;
-    val |= 0x2; // hard reset mode
-    outb(0xcf9, val);
-    val |= 0x4; // do the reset!
-    outb(0xcf9, val);
-
-    // try to reboot using keyboard controller hack (this works on QEMU)
-    printk(LOG_NOTE, "PCI reset failed, trying keyboard controller\n");
-    // try 10 times!
-    for (int i = 0; i < 10; i++) {
-        // toggle reset line
-        outb(0x64, 0xfe);
-    }
-
-    // Otherwise load invalid IDT and cause illegal opcode for triple fault
-    printk(LOG_NOTE, "Keyboard controller reset failed, trying triple fault\n");
-    __asm volatile("lidt        %[region]       \n\t"
-                   "ud2                         \n\t"
-                   : /* No output */
-                   : [region] "m" (region)
-                   );
-
-    halt(); // trick GCC into thinking we don't return
-}
-
-/**
- * \brief Triggers a debugger breakpoint.
- */
-void breakpoint(void)
-{
-    if(idt_initialized) {
-        hw_breakpoint();
-    } else {
-        printk(LOG_PANIC,
-               "Cannot trap into debugger -- Interrupts not set up yet!\n");
-    }
-}
-
-/**
- * \brief Go to user-space at entry point 'entry'.
- *
- * This function goes to user-space and starts executing the program at
- * its entry point at virtual address 'entry'.
- *
- * \param entry Entry point address of program to execute.
- */
-void __attribute__ ((noreturn))
-execute(lvaddr_t entry)
-{
-    // FIXME: make argument
-    uintptr_t arg = get_dispatcher_shared_generic(dcb_current->disp)->udisp;
-
-    /*
-     * Go to user-space using SYSRETQ -- the Q is very important, so operand
-     * size is 64-bit. Otherwise we return to compatibility mode.
-     *
-     * We set the startup contents of the RFLAGS register into R11. RCX is
-     * set to the entry point address of the user-space program. All other
-     * general-purpose registers are zeroed.
-     */
-    __asm volatile ("movq       %[flags], %%r11         \n\t" 
-                    "movq       $0, %%rsi               \n\t"
-                    "movq       $0, %%rdx               \n\t"
-                    "movq       $0, %%r8                \n\t"
-                    "movq       $0, %%r9                \n\t"
-                    "movq       $0, %%r10               \n\t"
-                    "movq       $0, %%r12               \n\t"
-                    "movq       $0, %%r13               \n\t"
-                    "movq       $0, %%r14               \n\t"
-                    "movq       $0, %%r15               \n\t"
-                    "movq       $0, %%rax               \n\t"
-                    "movq       $0, %%rbx               \n\t"
-                    "movq       $0, %%rbp               \n\t"
-                    "movq       $0, %%rsp               \n\t"
-                    "mov        %%dx, %%fs              \n\t"
-                    "mov        %%dx, %%gs              \n\t"
-                    "sysretq                            \n\t"
-                    : /* No output */
-                    :
-                    [entry] "c" (entry),
-                    [disp] "D" (arg),
-                    [flags] "i" (USER_RFLAGS)
-                    );
-
-    // Trick GCC to believe us not to return
-    halt();
-}
-
-/**
- * \brief Resume the given user-space snapshot.
- *
- * This function resumes user-space execution by restoring the CPU
- * registers with the ones given in the array, pointed to by 'regs'.
- */
-void __attribute__ ((noreturn)) resume(arch_registers_state_t *state)
-{
-    struct registers_x86_64 *regs = state;
-    __asm volatile ("pushq      %[ss]                   \n\t"
-                    "pushq       7*8(%[regs])           \n\t"   // RSP
-                    "pushq      %[rflags]               \n\t"
-                    "pushq      %[cs]                   \n\t"
-                    "pushq      16*8(%[regs])           \n\t"   // RIP
-                    "mov         %[fs], %%fs            \n\t"
-                    "mov         %[gs], %%gs            \n\t"
-                    "movq        0*8(%[regs]), %%rax    \n\t"
-                    "movq        2*8(%[regs]), %%rcx    \n\t"
-                    "movq        3*8(%[regs]), %%rdx    \n\t"
-                    "movq        4*8(%[regs]), %%rsi    \n\t"
-                    "movq        5*8(%[regs]), %%rdi    \n\t"
-                    "movq        6*8(%[regs]), %%rbp    \n\t"
-                    "movq        8*8(%[regs]), %%r8     \n\t"
-                    "movq        9*8(%[regs]), %%r9     \n\t"
-                    "movq       10*8(%[regs]), %%r10    \n\t"
-                    "movq       11*8(%[regs]), %%r11    \n\t"
-                    "movq       12*8(%[regs]), %%r12    \n\t"
-                    "movq       13*8(%[regs]), %%r13    \n\t"
-                    "movq       14*8(%[regs]), %%r14    \n\t"
-                    "movq       15*8(%[regs]), %%r15    \n\t"
-                    "movq        1*8(%[regs]), %%rbx    \n\t"   // RBX was base register
-                    "iretq                              \n\t"
-                    : /* No output */
-                    :
-                    [regs] "b" (regs),
-                    [ss] "i" (GSEL(USTACK_SEL, SEL_UPL)),
-                    [cs] "i" (GSEL(UCODE_SEL, SEL_UPL)),
-                    [fs] "m" (regs->fs),
-                    [gs] "m" (regs->gs),
-                    [rflags] "r" ((regs->eflags & USER_RFLAGS_MASK)
-                                  | USER_RFLAGS)
-                    );
-
-    // Trick GCC to believe us not to return
-    halt();
-}
-
-/**
- * \brief Halt processor until an interrupt arrives
- *
- * For use in the idle loop when nothing is runnable.
- */
-void __attribute__ ((noreturn)) wait_for_interrupt(void)
-{
-    __asm volatile("lea k1om_kernel_stack(%%rip), %%rsp\n\t"
-                   "addq %[stack_size], %%rsp\n\t"
-                   "sti                 \n\t"
-                   // The instruction right after STI is still in interrupt
-                   // shadow. To avoid unecessary calls to HLT we insert a nop
-                   // to make sure pending interrupts are handeled immediately.
-                   "nop                 \n\t"
-                   "hlt                 \n\t"
-                   :: [stack_size] "i" (K1OM_KERNEL_STACK_SIZE) : "rsp" );
-    panic("hlt should not return");
-}
-
-/**
- * \brief Use MONITOR/MWAIT to block until a given word changes
- *
- * \param base      Virtual address of 64-bit word to monitor
- * \param lastval   Previous value of word
- * \param extensions Processor-specific extensions (zero for defaults)
- * \param hints     Processor-specific hints (zero for defaults)
- *
- * Returns when the 64-bit word at base is not equal to lastval.
- */
-void monitor_mwait(lvaddr_t base, uint64_t lastval, uint32_t extensions,
-                   uint32_t hints)
-{
-    volatile uint64_t *val = (uint64_t *)base;
-
-    assert(extensions == 0);
-    assert(hints == 0);
-
-    while(*val == lastval) {
-        monitor(base, extensions, hints);
-        if(*val != lastval) {
-            return;
-        }
-        mwait(hints, extensions);
-    }
-}
-
-/// Remember current LDT pointer, so we can avoid reloading it
-lvaddr_t current_ldt_base = -1;
-size_t current_ldt_npages;
-
-void maybe_reload_ldt(struct dcb *dcb, bool force_reload)
-{
-    struct dispatcher_shared_x86_64 *disp =
-        get_dispatcher_shared_x86_64(dcb->disp);
-
-    /* Read fields from user dispatcher once for consistency */
-    lvaddr_t ldt_base = disp->ldt_base;
-    size_t ldt_npages = disp->ldt_npages;
-
-    /* optimize out if this is the same as the previous LDT */
-    if (!force_reload && ldt_base == current_ldt_base
-        && ldt_npages == current_ldt_npages) {
-        return;
-    }
-
-    uint16_t selector = 0;
-
-    if (ldt_base != 0 && ldt_npages != 0) {
-        extern union segment_descriptor *ldt_descriptor;
-        ldt_descriptor[0].sys_lo.lo_base = ldt_base & ((1ul << 24) - 1);
-        ldt_descriptor[0].sys_lo.hi_base = (ldt_base >> 24) & 0xff;
-        ldt_descriptor[1].sys_hi.base = ldt_base >> 32;
-        assert(ldt_descriptor[0].sys_lo.granularity != 0);
-        ldt_descriptor[0].sys_lo.lo_limit = ldt_npages;
-
-        selector = GSEL(LDT_LO_SEL, SEL_UPL);
-    }
-
-    __asm volatile("lldt %%ax"
-                   : /* No output */
-                   : "a" (selector));
-
-    current_ldt_base = ldt_base;
-    current_ldt_npages = ldt_npages;
-}
diff --git a/kernel/arch/k1om/gdb_arch.c b/kernel/arch/k1om/gdb_arch.c
deleted file mode 100644 (file)
index 61cc2da..0000000
+++ /dev/null
@@ -1,286 +0,0 @@
-/** \file
- * \brief x86-specific parts of in-kernel GDB stub.
- *
- * This file implements x86 architecture support for the kernel-side GDB stubs.
- */
-
-/*
- * Copyright (c) 2007, 2008, 2010, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <kernel.h>
-#include <stdio.h>
-#include <paging_kernel_arch.h>
-#include <gdb_stub.h>
-#include <arch_gdb_stub.h>
-#include <barrelfish_kpi/cpu.h>
-
-/** \brief GDB register save area / frame.
- *
- * Stores pointer to current save frame used by GDB. Used to read/modify
- * register contents, and reloaded when program execution resumes. */
-uintptr_t *gdb_arch_registers;
-
-/** \brief Separate stack area for the stub to run on */
-static uintptr_t gdb_stack[K1OM_KERNEL_STACK_SIZE/sizeof(uintptr_t)];
-/** \brief Pointer to top of GDB stack area. */
-uintptr_t * SNT gdb_stack_top = &gdb_stack[K1OM_KERNEL_STACK_SIZE/sizeof(uintptr_t)];
-
-/** \brief Converts exception vector to signal number.
- *
- * This function takes an x86 exception vector and attempts to
- * translate this number into a Unix-compatible signal value.
- */
-static int exception_to_signal(int vector)
-{
-    switch (vector) {
-        case 0:     return 8;   // divide by zero
-        case 1:     return 5;   // debug exception
-        case 3:     return 5;   // breakpoint
-        case 4:     return 16;  // into instruction (overflow)
-        case 5:     return 16;  // bound instruction
-        case 6:     return 4;   // Invalid opcode
-        case 7:     return 8;   // coprocessor not available
-        case 8:     return 7;   // double fault
-        case 9:     return 11;  // coprocessor segment overrun
-        case 10:    return 11;  // Invalid TSS
-        case 11:    return 11;  // Segment not present
-        case 12:    return 11;  // stack exception
-        case 13:    return 11;  // general protection
-        case 14:    return 11;  // page fault
-        case 16:    return 7;   // coprocessor error
-        default:    return 7;   // "software generated"
-    }
-}
-
-__asm__ (
-    ".global gdb_handle_exception       \n"
-    "gdb_handle_exception:              \n"
-    "mov gdb_stack_top(%rip), %rsp      \n"
-    "jmp gdb_handle_exception_onstack   \n"
-);
-
-/** \brief Entry point for an exception; we are now on our own stack.
- *
- * This function sets up the GDB-format register save frame, constructs the
- * initial message to the remote GDB and calls into the generic debugger entry
- * point.
- */
-void gdb_handle_exception_onstack(int vector, uintptr_t * NONNULL
-        COUNT(GDB_X86_64_NUM_REGS) save_area) __attribute__((noreturn));
-void gdb_handle_exception_onstack(int vector, uintptr_t * NONNULL
-        COUNT(GDB_X86_64_NUM_REGS) save_area)
-{
-    /* sanity check that we didn't trap inside the debugger...
-     * if we did, we're definitely hosed now! */
-    if (save_area[GDB_X86_64_RSP_REG] >= (lvaddr_t)&gdb_stack &&
-        save_area[GDB_X86_64_RSP_REG] <= (lvaddr_t)gdb_stack_top) {
-        panic("Nested exception within GDB stub");
-    }
-
-    /* were we in user mode at the time of the trap? */
-    if ((save_area[GDB_X86_64_CS_REG] & 0x3) != 0) {
-        printk(LOG_NOTE,
-               "Entered from user-mode (at least, CPL in CS is non-zero)\n");
-
-    /* while we're checking the stack pointer, sanity check that it's
-     * within the normal kernel stack region */
-    } else if (save_area[GDB_X86_64_RSP_REG] < (lvaddr_t)&k1om_kernel_stack ||
-              save_area[GDB_X86_64_RSP_REG] > (lvaddr_t)&k1om_kernel_stack +
-               K1OM_KERNEL_STACK_SIZE) {
-        printk(LOG_WARN, "BIG FAT WARNING: kernel stack pointer (0x%lx) is "
-               "invalid!\n", save_area[GDB_X86_64_RSP_REG]);
-        printk(LOG_WARN, "Boldly attempting to continue into GDB anyway...\n");
-    }
-
-    char buffer[64];
-    int signal = exception_to_signal(vector);
-
-#if 0 // this is broken, because the register data needs to be in LE byte order
-    // T <signal> RSP:<stack ptr>; RIP:<inst ptr>;
-    int r = snprintf(buffer, sizeof(buffer), "T%02hhx%x:%lx;%x:%lx;", signal,
-                     RSP_REG, save_area[RSP_REG],
-                     RIP_REG, save_area[RIP_REG]);
-#else
-    int r = snprintf(buffer, sizeof(buffer), "S%02hhx", signal);
-#endif
-    assert(r < sizeof(buffer));
-    if (r >= sizeof(buffer)) {
-        // ensure termination in the case of overflow
-        buffer[sizeof(buffer) - 1] = '\0';
-    }
-
-    gdb_arch_registers = save_area;
-    gdb_stub_entry(signal, buffer);
-}
-
-/** \brief Get the value of a single register in the frame.
- * \param regnum register number (as defined by the #gdb_register_nums enum)
- * \param value pointer to location in which to return current value
- * \return Zero on success, nonzero on failure (invalid regnum).
- */
-int gdb_arch_get_register(int regnum, uintptr_t *value)
-{
-    if (regnum < 0 || regnum >= GDB_X86_64_NUM_REGS) {
-        return -1;
-    }
-
-    *value = gdb_arch_registers[regnum];
-    return 0;
-}
-
-/** \brief Set the value of a single register in the frame.
- * \param regnum register number (as defined by the #gdb_register_nums enum)
- * \param value new value
- * \return Zero on success, nonzero on failure (invalid regnum).
- */
-int gdb_arch_set_register(int regnum, uintptr_t value)
-{
-    if (regnum < 0 || regnum >= GDB_X86_64_NUM_REGS) {
-        return -1;
-    }
-
-    gdb_arch_registers[regnum] = value;
-    return 0;
-}
-
-/** \brief Resume execution.
- *
- * Resumes execution with the CPU state stored in the #gdb_arch_registers frame.
- */
-void gdb_resume(void) __attribute__((noreturn));
-
-__asm__ (
-    ".global gdb_resume                 \n"
-    "gdb_resume:                        \n"
-    /* load address (in PIC manner) of register frame */
-    "movq gdb_arch_registers(%rip), %r15\n"
-    /* setup stack for iretq below */
-    "movq (19*8)(%r15), %rax            \n" // SS
-    "pushq %rax                         \n"
-    "movq (7*8)(%r15), %rax             \n" // RSP
-    "pushq %rax                         \n"
-    "movq (17*8)(%r15), %rax            \n" // EFLAGS
-    "pushq %rax                         \n"
-    "movq (18*8)(%r15), %rax            \n" // CS
-    "pushq %rax                         \n"
-    "movq (16*8)(%r15), %rax            \n" // EIP
-    "pushq %rax                         \n"
-    /* load remaining registers directly */
-    "movq (0*8)(%r15), %rax             \n"
-    "movq (1*8)(%r15), %rbx             \n"
-    "movq (2*8)(%r15), %rcx             \n"
-    "movq (3*8)(%r15), %rdx             \n"
-    "movq (4*8)(%r15), %rsi             \n"
-    "movq (5*8)(%r15), %rdi             \n"
-    "movq (6*8)(%r15), %rbp             \n"
-    "movq (8*8)(%r15), %r8              \n"
-    "movq (9*8)(%r15), %r9              \n"
-    "movq (10*8)(%r15), %r10            \n"
-    "movq (11*8)(%r15), %r11            \n"
-    "movq (12*8)(%r15), %r12            \n"
-    "movq (13*8)(%r15), %r13            \n"
-    "movq (14*8)(%r15), %r14            \n"
-    "movq (15*8)(%r15), %r15            \n"
-    /* return! */
-    "iretq                              \n"
-);
-
-/** \brief Resume program execution.
- * \param addr Address to resume at, or 0 to continue at last address.
- */
-void gdb_arch_continue(lvaddr_t addr)
-{
-    if (addr != 0) {
-        gdb_arch_registers[GDB_X86_64_RIP_REG] = addr;
-    }
-
-    /* clear the trace bit */
-    gdb_arch_registers[GDB_X86_64_EFLAGS_REG] &= ~0x100; // XXX
-
-    gdb_resume(); /* doesn't return */
-}
-
-/** \brief Single-step program execution.
- * \param addr Address to resume at, or 0 to continue at last address.
- */
-void gdb_arch_single_step(lvaddr_t addr)
-{
-    if (addr != 0) {
-        gdb_arch_registers[GDB_X86_64_RIP_REG] = addr;
-    }
-
-    /* set the trace bit for single-step */
-    gdb_arch_registers[GDB_X86_64_EFLAGS_REG] |= 0x100; // XXX
-
-    gdb_resume(); /* doesn't return */
-}
-
-/** \brief Ensures that the page containing addr is mapped.
- * \return Zero on success, negative on failure.
- */
-static int ensure_mapping(lvaddr_t addr)
-{
-    static lpaddr_t lastaddr;
-
-    /* check if address is in kernel image */
-    if (addr >= (lvaddr_t)&_start_kernel && addr < (lvaddr_t)&_end_kernel) {
-        return 0;
-    }
-
-    /* if address is outside "physical" memory region, fail the access */
-    if (addr < K1OM_PADDR_SPACE_LIMIT) {
-        return -1;
-    }
-
-    /* we now know we have a valid "physical memory" region address */
-    lpaddr_t paddr = mem_to_local_phys(addr);
-    paddr -= paddr & X86_64_MEM_PAGE_MASK; // page-align
-
-    /* quick and dirty optimisation: if this address is on the same page as
-     * the last time we were called, return immediately */
-    if (lastaddr == paddr && lastaddr != 0) {
-        return 0;
-    }
-
-    int r = paging_k1om_map_memory(paddr, X86_64_MEM_PAGE_SIZE);
-    if (r < 0) {
-        return r;
-    }
-
-    lastaddr = paddr;
-    return 0;
-}
-
-/** \brief Writes a byte to an arbitrary address in kernel memory.
- * \return Zero on success, nonzero on error (invalid address)
- */
-int gdb_arch_write_byte(uint8_t *addr, uint8_t val)
-{
-    int r = ensure_mapping((lvaddr_t)addr);
-    if (r < 0) {
-        return r;
-    }
-
-    *addr = val;
-    return 0;
-}
-
-/** \brief Reads a byte from an arbitrary address in kernel memory.
- * \return Zero on success, nonzero on error (invalid address)
- */
-int gdb_arch_read_byte(uint8_t *addr, uint8_t *val)
-{
-    int r = ensure_mapping((lvaddr_t)addr);
-    if (r < 0) {
-        return r;
-    }
-
-    *val = *addr;
-    return 0;
-}
index d384b8b..852091c 100644 (file)
 #include <target/x86/barrelfish_kpi/coredata_target.h>
 #include <arch/x86/timing.h>
 #include <arch/x86/startup_x86.h>
+#include <arch/x86/start_aps.h>
 #include <arch/x86/ipi_notify.h>
 #include <barrelfish_kpi/cpu_arch.h>
 #include <target/k1om/barrelfish_kpi/cpu_target.h>
 #include <barrelfish_kpi/asm_inlines_arch.h>
-#include <linux_host.h>
+
+#include <coreboot.h>
+#include <kcb.h>
 
 #include <xeon_phi.h>
 #include <xeon_phi/xeon_phi.h>
@@ -49,6 +52,8 @@
 #include <dev/ia32_dev.h>
 #include <dev/amd64_dev.h>
 
+#include <linux_host.h>
+
 /**
  * Used to store the address of global struct passed during boot across kernel
  * relocations.
@@ -78,108 +83,107 @@ static struct task_state_segment tss __attribute__ ((aligned (4)));
  * both kernel- and user-space and the only Task State Segment (TSS).
  */
 union segment_descriptor gdt[] __attribute__ ((aligned (4))) = {
-    [NULL_SEL] = {  // Null segment
-            .raw = 0
-        },
+    [NULL_SEL] = {   // Null segment
+        .raw = 0
+    },
     [KCODE_SEL] = {   // Kernel code segment
-            .d = {
-                .lo_limit = 0xffff,
-                .lo_base = 0,
-                .type = 0xa,
-                .system_desc = 1,
-                .privilege_level = SEL_KPL,
-                .present = 1,
-                .hi_limit = 0xf,
-                .available = 0,
-                .long_mode = 1,
-                .operation_size = 0,
-                .granularity = 1,
-                .hi_base = 0
-            }
-        },
-    [KSTACK_SEL] = {  // Kernel stack segment
-            .d = {
-                .lo_limit = 0xffff,
-                .lo_base = 0,
-                .type = 2,
-                .system_desc = 1,
-                .privilege_level = SEL_KPL,
-                .present = 1,
-                .hi_limit = 0xf,
-                .available = 0,
-                .long_mode = 1,
-                .operation_size = 0,
-                .granularity = 1,
-                .hi_base = 0
-            }
-        },
-    [USTACK_SEL] = {  // User stack segment
-            .d = {
-                .lo_limit = 0xffff,
-                .lo_base = 0,
-                .type = 2,
-                .system_desc = 1,
-                .privilege_level = SEL_UPL,
-                .present = 1,
-                .hi_limit = 0xf,
-                .available = 0,
-                .long_mode = 1,
-                .operation_size = 0,
-                .granularity = 1,
-                .hi_base = 0
-            }
-        },
-    [UCODE_SEL] = {  // User code segment
-            .d = {
-                .lo_limit = 0xffff,
-                .lo_base = 0,
-                .type = 0xa,
-                .system_desc = 1,
-                .privilege_level = SEL_UPL,
-                .present = 1,
-                .hi_limit = 0xf,
-                .available = 0,
-                .long_mode = 1,
-                .operation_size = 0,
-                .granularity = 1,
-                .hi_base = 0
-            }
-        },
-    [TSS_LO_SEL] = {  // Global Task State Segment (TSS), lower 8 bytes
-            .sys_lo = {
-                .lo_limit = sizeof(tss) & 0xffff,
-                .type = SDT_SYSTSS,
-                .privilege_level = SEL_KPL,
-                .present = 1,
-                .hi_limit = (sizeof(tss) >> 16) & 0xf,
-                .available = 0,
-                .granularity = 0,
-            }
-        },
-    [TSS_HI_SEL] = {  // Global Task State Segment (TSS), upper 8 bytes
-            .sys_hi = {
-                .base = 0
-            }
-        },
-    [LDT_LO_SEL] = {  // Local descriptor table (LDT), lower 8 bytes
-            .sys_lo = {
-                .lo_limit = 0,  // # 4k pages (since granularity = 1)
-                .lo_base = 0,  // changed by context switch path when doing lldt
-                .type = 2,  // LDT
-                .privilege_level = SEL_UPL,
-                .present = 1,
-                .hi_limit = 0,
-                .available = 0,
-                .granularity = 1,
-                .hi_base = 0
-            }
-        },
-    [LDT_HI_SEL ] = {  // Local descriptor table (LDT), upper 8 bytes
-            .sys_hi = {
-                .base = 0
-            // changed by context switch path when doing lldt
-                }
-        },
+        .d = {
+            .lo_limit = 0xffff,
+            .lo_base = 0,
+            .type = 0xa,
+            .system_desc = 1,
+            .privilege_level = SEL_KPL,
+            .present = 1,
+            .hi_limit = 0xf,
+            .available = 0,
+            .long_mode = 1,
+            .operation_size = 0,
+            .granularity = 1,
+            .hi_base = 0
+        }
+    },
+    [KSTACK_SEL] = {   // Kernel stack segment
+        .d = {
+            .lo_limit = 0xffff,
+            .lo_base = 0,
+            .type = 2,
+            .system_desc = 1,
+            .privilege_level = SEL_KPL,
+            .present = 1,
+            .hi_limit = 0xf,
+            .available = 0,
+            .long_mode = 1,
+            .operation_size = 0,
+            .granularity = 1,
+            .hi_base = 0
+        }
+    },
+    [USTACK_SEL] = {   // User stack segment
+        .d = {
+            .lo_limit = 0xffff,
+            .lo_base = 0,
+            .type = 2,
+            .system_desc = 1,
+            .privilege_level = SEL_UPL,
+            .present = 1,
+            .hi_limit = 0xf,
+            .available = 0,
+            .long_mode = 1,
+            .operation_size = 0,
+            .granularity = 1,
+            .hi_base = 0
+        }
+    },
+    [UCODE_SEL] = {   // User code segment
+        .d = {
+            .lo_limit = 0xffff,
+            .lo_base = 0,
+            .type = 0xa,
+            .system_desc = 1,
+            .privilege_level = SEL_UPL,
+            .present = 1,
+            .hi_limit = 0xf,
+            .available = 0,
+            .long_mode = 1,
+            .operation_size = 0,
+            .granularity = 1,
+            .hi_base = 0
+        }
+    },
+    [TSS_LO_SEL] = {   // Global Task State Segment (TSS), lower 8 bytes
+        .sys_lo = {
+            .lo_limit = sizeof(tss) & 0xffff,
+            .type = SDT_SYSTSS,
+            .privilege_level = SEL_KPL,
+            .present = 1,
+            .hi_limit = (sizeof(tss) >> 16) & 0xf,
+            .available = 0,
+            .granularity = 0,
+        }
+    },
+    [TSS_HI_SEL] = {   // Global Task State Segment (TSS), upper 8 bytes
+        .sys_hi = {
+            .base = 0
+        }
+    },
+    [LDT_LO_SEL] = {    // Local descriptor table (LDT), lower 8 bytes
+        .sys_lo = {
+            .lo_limit = 0, // # 4k pages (since granularity = 1)
+            .lo_base = 0, // changed by context switch path when doing lldt
+            .type = 2, // LDT
+            .privilege_level = SEL_UPL,
+            .present = 1,
+            .hi_limit = 0,
+            .available = 0,
+            .granularity = 1,
+            .hi_base = 0
+        }
+    },
+    [LDT_HI_SEL] = {    // Local descriptor table (LDT), upper 8 bytes
+        .sys_hi = {
+            .base = 0 // changed by context switch path when doing lldt
+        }
+    },
 };
 
 union segment_descriptor *ldt_descriptor = &gdt[LDT_LO_SEL];
@@ -188,23 +192,30 @@ union segment_descriptor *ldt_descriptor = &gdt[LDT_LO_SEL];
  * Bootup PML4, used to map both low (identity-mapped) memory and relocated
  * memory at the same time.
  */
-static union x86_64_pdir_entry boot_pml4[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE)));
+static union x86_64_pdir_entry boot_pml4[PTABLE_SIZE]
+__attribute__ ((aligned(BASE_PAGE_SIZE)));
 
 /**
  * Bootup low-map PDPT and hi-map PDPT.
  */
-static union x86_64_pdir_entry
-                boot_pdpt[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE))),
-                boot_pdpt_hi[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE)));
+static union x86_64_pdir_entry boot_pdpt[PTABLE_SIZE]
+__attribute__ ((aligned(BASE_PAGE_SIZE))),
+    boot_pdpt_hi[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE)));
 
 /**
  * Bootup low-map PDIR, hi-map PDIR, and 1GB PDIR.
  */
-static union x86_64_ptable_entry
-                boot_pdir[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE))),
-                boot_pdir_hi[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE))),
-                boot_pdir_1GB[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE))),
-                boot_pdir_mmio[PTABLE_SIZE] __attribute__ ((aligned(BASE_PAGE_SIZE)));
+static union x86_64_ptable_entry boot_pdir[PTABLE_SIZE]
+__attribute__ ((aligned(BASE_PAGE_SIZE)));
+
+static union x86_64_ptable_entry boot_pdir_hi[PTABLE_SIZE]
+__attribute__ ((aligned(BASE_PAGE_SIZE)));
+
+static union x86_64_ptable_entry boot_pdir_1GB[PTABLE_SIZE]
+__attribute__ ((aligned(BASE_PAGE_SIZE)));
+
+static union x86_64_ptable_entry boot_pdir_mmio[PTABLE_SIZE]
+__attribute__ ((aligned(BASE_PAGE_SIZE)));
 
 /**
  * This flag is set to true once the IDT is initialized and exceptions can be
@@ -225,67 +236,55 @@ bool idt_initialized = false;
  * \param base  Start address of kernel image in physical address space.
  * \param size  Size of kernel image.
  */
-static void paging_init(lpaddr_t base,
-                        size_t size)
+static void paging_init(lpaddr_t base, size_t size)
 {
     lvaddr_t vbase = local_phys_to_mem(base);
 
     // Align base to kernel page size
-    if (base & X86_64_MEM_PAGE_MASK) {
+    if(base & X86_64_MEM_PAGE_MASK) {
         size += base & X86_64_MEM_PAGE_MASK;
         base -= base & X86_64_MEM_PAGE_MASK;
     }
 
     // Align vbase to kernel page size
-    if (vbase & X86_64_MEM_PAGE_MASK) {
+    if(vbase & X86_64_MEM_PAGE_MASK) {
         vbase -= vbase & X86_64_MEM_PAGE_MASK;
     }
 
     // Align size to kernel page size
-    if (size & X86_64_MEM_PAGE_MASK) {
+    if(size & X86_64_MEM_PAGE_MASK) {
         size += X86_64_MEM_PAGE_SIZE - (size & X86_64_MEM_PAGE_MASK);
     }
 
     // XXX: Cannot currently map more than one table of pages
     assert(size <= X86_64_MEM_PAGE_SIZE * X86_64_PTABLE_SIZE);
-    /*     assert(size <= MEM_PAGE_SIZE); */
+/*     assert(size <= MEM_PAGE_SIZE); */
 
-    for (size_t i = 0; i < size; i += X86_64_MEM_PAGE_SIZE, base +=
-    X86_64_MEM_PAGE_SIZE, vbase += X86_64_MEM_PAGE_SIZE) {
+    for(size_t i = 0; i < size; i += X86_64_MEM_PAGE_SIZE,
+            base += X86_64_MEM_PAGE_SIZE, vbase += X86_64_MEM_PAGE_SIZE) {
         // No kernel image above 4 GByte
-        assert(base < ((lpaddr_t )4 << 30));
+        assert(base < ((lpaddr_t)4 << 30));
 
         // Identity-map the kernel's physical region, so we don't lose ground
-        paging_k1om_map_table(&boot_pml4[X86_64_PML4_BASE(base)],
-                              (lpaddr_t) boot_pdpt);
-        paging_k1om_map_table(&boot_pdpt[X86_64_PDPT_BASE(base)],
-                              (lpaddr_t) boot_pdir);
-        paging_k1om_map_large(&boot_pdir[X86_64_PDIR_BASE(base)],
-                              base,
-                              PTABLE_PRESENT | PTABLE_READ_WRITE
-                              | PTABLE_USER_SUPERVISOR);
+        paging_x86_64_map_table(&boot_pml4[X86_64_PML4_BASE(base)], (lpaddr_t)boot_pdpt);
+        paging_x86_64_map_table(&boot_pdpt[X86_64_PDPT_BASE(base)], (lpaddr_t)boot_pdir);
+        paging_x86_64_map_large(&boot_pdir[X86_64_PDIR_BASE(base)], base, PTABLE_PRESENT
+                                | PTABLE_READ_WRITE | PTABLE_USER_SUPERVISOR);
 
         // Alias the same region at MEMORY_OFFSET
-        paging_k1om_map_table(&boot_pml4[X86_64_PML4_BASE(vbase)],
-                              (lpaddr_t) boot_pdpt_hi);
-        paging_k1om_map_table(&boot_pdpt_hi[X86_64_PDPT_BASE(vbase)],
-                              (lpaddr_t) boot_pdir_hi);
-        paging_k1om_map_large(&boot_pdir_hi[X86_64_PDIR_BASE(vbase)],
-                              base,
-                              PTABLE_PRESENT | PTABLE_READ_WRITE
-                              | PTABLE_USER_SUPERVISOR);
-
+        paging_x86_64_map_table(&boot_pml4[X86_64_PML4_BASE(vbase)], (lpaddr_t)boot_pdpt_hi);
+        paging_x86_64_map_table(&boot_pdpt_hi[X86_64_PDPT_BASE(vbase)], (lpaddr_t)boot_pdir_hi);
+        paging_x86_64_map_large(&boot_pdir_hi[X86_64_PDIR_BASE(vbase)], base, PTABLE_PRESENT
+                                | PTABLE_READ_WRITE | PTABLE_USER_SUPERVISOR);
     }
 
     // Identity-map the first 1G of physical memory for bootloader data
-    paging_k1om_map_table(&boot_pml4[0], (lpaddr_t) boot_pdpt);
-    paging_k1om_map_table(&boot_pdpt[0], (lpaddr_t) boot_pdir_1GB);
+    paging_x86_64_map_table(&boot_pml4[0], (lpaddr_t)boot_pdpt);
+    paging_x86_64_map_table(&boot_pdpt[0], (lpaddr_t)boot_pdir_1GB);
     for (int i = 0; i < X86_64_PTABLE_SIZE; i++) {
-        paging_k1om_map_large(&boot_pdir_1GB[X86_64_PDIR_BASE(X86_64_MEM_PAGE_SIZE
-                                              * i)],
-                              X86_64_MEM_PAGE_SIZE * i,
-                              PTABLE_PRESENT | PTABLE_READ_WRITE
-                              | PTABLE_USER_SUPERVISOR);
+        paging_x86_64_map_large(&boot_pdir_1GB[X86_64_PDIR_BASE(X86_64_MEM_PAGE_SIZE * i)],
+                                X86_64_MEM_PAGE_SIZE * i, PTABLE_PRESENT
+                                | PTABLE_READ_WRITE | PTABLE_USER_SUPERVISOR);
     }
 
     /*
@@ -294,18 +293,18 @@ static void paging_init(lpaddr_t base,
      *
      * PML4[0], PDIR[32]
      */
-    paging_k1om_map_table(&boot_pml4[X86_64_PML4_BASE(local_phys_to_mem(XEON_PHI_SBOX_BASE))],
+    paging_x86_64_map_table(&boot_pml4[X86_64_PML4_BASE(local_phys_to_mem(XEON_PHI_SBOX_BASE))],
                           (lpaddr_t) boot_pdpt_hi);
-    paging_k1om_map_table(&boot_pdpt_hi[X86_64_PDPT_BASE(local_phys_to_mem(XEON_PHI_SBOX_BASE))],
+    paging_x86_64_map_table(&boot_pdpt_hi[X86_64_PDPT_BASE(local_phys_to_mem(XEON_PHI_SBOX_BASE))],
                           (lpaddr_t) boot_pdir_mmio);
 
-    paging_k1om_map_large(&boot_pdir_mmio[X86_64_PDIR_BASE(local_phys_to_mem(XEON_PHI_SBOX_BASE))],
+    paging_x86_64_map_large(&boot_pdir_mmio[X86_64_PDIR_BASE(local_phys_to_mem(XEON_PHI_SBOX_BASE))],
                           XEON_PHI_SBOX_BASE,
                           PTABLE_PRESENT | PTABLE_READ_WRITE | PTABLE_USER_SUPERVISOR
                           | PTABLE_CACHE_DISABLED);
 
     // Activate new page tables
-    paging_k1om_context_switch((lpaddr_t) boot_pml4);
+    paging_x86_64_context_switch((lpaddr_t)boot_pml4);
 }
 
 /**
@@ -318,10 +317,10 @@ static void paging_init(lpaddr_t base,
  */
 static void gdt_reset(void)
 {
-    lvaddr_t ptss = (lvaddr_t) &tss;
-    struct region_descriptor region = {
+    lvaddr_t                     ptss = (lvaddr_t)&tss;
+    struct region_descriptor    region = {
         .rd_limit = sizeof(gdt),
-        .rd_base = (uint64_t) &gdt
+        .rd_base = (uint64_t)&gdt
     };
 
     // Load default GDT
@@ -329,22 +328,22 @@ static void gdt_reset(void)
 
     // Reload segments
     __asm volatile("mov %[null], %%ds      \n\t"
-                    "mov %[null], %%es      \n\t"
-                    "mov %[ss], %%ss        \n\t"
-                    "mov %[null], %%gs      \n\t"
-                    "mov %[null], %%fs      \n\t"
-                    "pushq %[cs]            \n\t"          // new CS
-                    "lea 1f(%%rip), %%rax   \n\t"// jumps to after lret
-                    "pushq %%rax            \n\t"// new IP
-                    "lretq                  \n\t"// fake return
-                    "1:                     \n\t"// we'll continue here
-                    : /* No Output */
-                    :
-                    [null] "r" (0),
-                    [ss] "r" (GSEL(KSTACK_SEL, SEL_KPL)),
-                    [cs] "i" (GSEL(KCODE_SEL, SEL_KPL))
-                    : "rax"
-    );
+                   "mov %[null], %%es      \n\t"
+                   "mov %[ss], %%ss        \n\t"
+                   "mov %[null], %%gs      \n\t"
+                   "mov %[null], %%fs      \n\t"
+                   "pushq %[cs]            \n\t"          // new CS
+                   "lea 1f(%%rip), %%rax   \n\t"          // jumps to after lret
+                   "pushq %%rax            \n\t"          // new IP
+                   "lretq                  \n\t"          // fake return
+                   "1:                     \n\t"          // we'll continue here
+                   : /* No Output */
+                   :
+                   [null] "r" (0),
+                   [ss] "r" (GSEL(KSTACK_SEL, SEL_KPL)),
+                   [cs] "i" (GSEL(KCODE_SEL, SEL_KPL))
+                   : "rax"
+                   );
 
     // Complete setup of TSS descriptor (by inserting base address of TSS)
     gdt[TSS_LO_SEL].sys_lo.lo_base = ptss & 0xffffff;
@@ -367,15 +366,14 @@ static void gdt_reset(void)
  *
  * \param offset        Offset to add to the stack pointer.
  */
-
 static inline void __attribute__ ((always_inline))
 relocate_stack(lvaddr_t offset)
 {
     __asm volatile("add %[stack], %%rsp\n\t"
-                    : /* No output */
-                    : [stack] "er" (offset)
-                    : "rsp"
-    );
+                   : /* No output */
+                   : [stack] "er" (offset)
+                   : "rsp"
+                   );
 }
 
 /**
@@ -391,16 +389,16 @@ static inline void enable_fast_syscalls(void)
     // Segment selector bases for both kernel- and user-space for fast
     // system calls
     ia32_star_t star = ia32_star_rd(NULL);
-    star = ia32_star_call_insert(star, GSEL(KCODE_SEL, SEL_KPL));
-    star = ia32_star_ret_insert(star, GSEL(KSTACK_SEL, SEL_UPL));
+    star = ia32_star_call_insert(star, GSEL(KCODE_SEL,  SEL_KPL));
+    star = ia32_star_ret_insert( star, GSEL(KSTACK_SEL, SEL_UPL));
     ia32_star_wr(NULL, star);
 
     // Set ia32_lstar MSR to point to kernel-space system call multiplexer
-    ia32_lstar_wr(NULL, (lvaddr_t) syscall_entry);
+    ia32_lstar_wr(NULL, (lvaddr_t)syscall_entry);
 
     // Set IA32_FMASK MSR for our OSes EFLAGS mask
     // We mask out everything (including interrupts).
-    ia32_fmask_v_wrf(NULL, ~(RFLAGS_ALWAYS1));
+    ia32_fmask_v_wrf(NULL, ~(RFLAGS_ALWAYS1) );
 
     // Enable fast system calls
     ia32_efer_sce_wrf(NULL, 1);
@@ -412,13 +410,13 @@ static inline void enable_tlb_flush_filter(void)
 
     // Must read "AuthenticAMD"
     cpuid(0, &eax, &ebx, &ecx, &edx);
-    if (ebx != 0x68747541 || ecx != 0x444d4163 || edx != 0x69746e65) {
+    if(ebx != 0x68747541 || ecx != 0x444d4163 || edx != 0x69746e65) {
         return;
     }
 
     // Is at least family 0fh?
     cpuid(1, &eax, &ebx, &ecx, &edx);
-    if (((eax >> 8) & 0xf) != 0xf) {
+    if(((eax >> 8) & 0xf) != 0xf) {
         return;
     }
 
@@ -426,20 +424,6 @@ static inline void enable_tlb_flush_filter(void)
     ia32_amd_hwcr_ffdis_wrf(NULL, 1);
 }
 
-static inline void enable_monitor_mwait(void)
-{
-    uint32_t eax, ebx, ecx, edx;
-
-    cpuid(1, &eax, &ebx, &ecx, &edx);
-
-    if (ecx & (1 << 3)) {
-        cpuid(5, &eax, &ebx, &ecx, &edx);
-        debug(SUBSYS_STARTUP, "MONITOR/MWAIT supported: "
-              "min size %u bytes, max %u bytes. %s %s\n",
-              eax, ebx, (ecx & 2) ? "IBE" : "", (ecx & 1) ? "EMX" : "");
-    }
-}
-
 /**
  * \brief Continue kernel initialization in kernel address space.
  *
@@ -451,11 +435,10 @@ static inline void enable_monitor_mwait(void)
  * calls kernel_startup(), which should not return (if it does, this function
  * halts the kernel).
  */
-static void __attribute__ ((noreturn, noinline))
-text_init(void)
+static void  __attribute__ ((noreturn, noinline)) text_init(void)
 {
     // Reset global and locks to point to the memory in the pristine image
-    global = (struct global*) addr_global;
+    global = (struct global*)addr_global;
 
     // re-initialize the console with the relocated address
     serial_console_init(local_phys_to_mem(XEON_PHI_SBOX_BASE));
@@ -463,22 +446,25 @@ text_init(void)
      * Reset paging once more to use relocated data structures and map in
      * whole of kernel and available physical memory. Map out low memory.
      */
-    paging_k1om_reset();
+    paging_x86_64_reset();
 
     // Relocate global to "memory"
-    global = (struct global*) local_phys_to_mem((lpaddr_t) global);
+    global = (struct global*)local_phys_to_mem((lpaddr_t)global);
 
     // Relocate glbl_core_data to "memory"
-    glbl_core_data =
-        (struct x86_core_data *) local_phys_to_mem((lpaddr_t) glbl_core_data);
+    glbl_core_data = (struct x86_core_data *)
+        local_phys_to_mem((lpaddr_t)glbl_core_data);
 
     /*
      * We know how much memory we have based on the card model
      */
-    if (paging_k1om_map_memory(0, K1OM_PHYSICAL_MEMORY_SIZE) != 0) {
+    if (paging_x86_64_map_memory(0, K1OM_PHYSICAL_MEMORY_SIZE) != 0) {
         panic("error while mapping physical memory!");
     }
 
+    kcb_current = (struct kcb *)
+        local_phys_to_mem((lpaddr_t) kcb_current);
+
     /*
      * Also reset the global descriptor table (GDT), so we get
      * segmentation again and can catch interrupts/exceptions (the IDT
@@ -523,15 +509,6 @@ text_init(void)
     // do not remove/change this printf: needed by regression harness
     printf("Barrelfish CPU driver starting on k1om apic_id %u\n", apic_id);
 
-    /*
-     * there is no such thing as a PIC on the xeon phi
-     * XXX: verify!
-     if (apic_is_bsp()) {
-     printf("apic_is_bsp\n");
-     // Initialize classic (8259A) PIC
-     pic_init();
-     }
-     */
 
     // Initialize local APIC timer
     if (kernel_ticks_enabled) {
@@ -569,14 +546,6 @@ text_init(void)
     // AMD64: Check if TLB flush filter is enabled
     enable_tlb_flush_filter();
 
-    // Enable global pages
-    // there are no global page tables, enabling this will result in a GP
-    // amd64_cr4_pge_wrf(NULL, 1);
-
-    // Check/Enable MONITOR/MWAIT opcodes
-    // there is no monitor/mwait
-    //enable_monitor_mwait();
-
     // Call main kernel startup function -- this should never return
     kernel_startup();
 
@@ -698,10 +667,9 @@ void arch_init(uint64_t magic,
     struct Elf64_Shdr *rela, *symtab;
     struct x86_coredata_elf *elf;
     uint32_t multiboot_flags;
-
     if (mb != NULL) { /* Multiboot info was passed */
         multiboot_flags = mb->flags;
-        elf = (struct x86_coredata_elf *) &mb->syms.elf;
+        elf = (struct x86_coredata_elf *)&mb->syms.elf;
 
         // We need the ELF section header table for relocation
         if (!(multiboot_flags & MULTIBOOT_INFO_FLAG_HAS_ELF_SYMS)) {
@@ -709,9 +677,7 @@ void arch_init(uint64_t magic,
                   "header information -- Relocation impossible!");
         }
 
-        /*
-         * Determine where free RAM starts
-         */
+        // Determine where free RAM starts
         glbl_core_data->start_free_ram =
             ROUND_UP(max(multiboot_end_addr(mb), (uintptr_t)&_end_kernel),
                      BASE_PAGE_SIZE);
@@ -726,18 +692,23 @@ void arch_init(uint64_t magic,
         glbl_core_data->mmap_length = mb->mmap_length;
         glbl_core_data->mmap_addr = mb->mmap_addr;
         glbl_core_data->xeon_phi_id = mb->xeon_phi_id;
+        
+        extern struct kcb bspkcb;
+        memset(&bspkcb, 0, sizeof(bspkcb));
+        kcb_current = &bspkcb;
     } else {
         /* No multiboot info, use the core_data struct */
-        struct x86_core_data *core_data = (struct x86_core_data*) (dest
-                        - BASE_PAGE_SIZE);
+        struct x86_core_data *core_data =
+            (struct x86_core_data*)(dest - BASE_PAGE_SIZE);
         multiboot_flags = core_data->multiboot_flags;
         elf = &core_data->elf;
         glbl_core_data = core_data;
-        core_data->cmdline = (lpaddr_t) &core_data->kernel_cmdline;
+        core_data->cmdline = (lpaddr_t)&core_data->kernel_cmdline;
         my_core_id = core_data->dst_core_id;
 
-        if (core_data->module_end > 4ul * (1ul << 20)) {
-            panic("The cpu module is outside the initial 4MB mapping."
+        kcb_current = (struct kcb*) glbl_core_data->kcb;
+        if (core_data->module_end > 4ul * (1ul << 30)) {
+            panic("The cpu module is outside the initial 4GB mapping."
                   " Either move the module or increase initial mapping.");
         }
     }
@@ -748,37 +719,32 @@ void arch_init(uint64_t magic,
     }
 
     // Find relocation section
-    rela = elf64_find_section_header_type((struct Elf64_Shdr *) (lpaddr_t) elf->addr,
-                                          elf->num,
-                                          SHT_RELA);
+    rela = elf64_find_section_header_type((struct Elf64_Shdr *)
+                                          (lpaddr_t)elf->addr,
+                                          elf->num, SHT_RELA);
     if (rela == NULL) {
         panic("Kernel image does not include relocation section!");
     }
 
     // Find symbol table section
-    symtab = elf64_find_section_header_type((struct Elf64_Shdr *) (lpaddr_t) elf
-                                                            ->addr,
-                                            elf->num,
-                                            SHT_DYNSYM);
+    symtab = elf64_find_section_header_type((struct Elf64_Shdr *)
+                                            (lpaddr_t)elf->addr,
+                                            elf->num, SHT_DYNSYM);
     if (symtab == NULL) {
         panic("Kernel image does not include symbol table!");
     }
 
     // Alias kernel on top of memory, keep low memory
-    paging_init((lpaddr_t) &_start_kernel, SIZE_KERNEL_IMAGE);
+    paging_init((lpaddr_t)&_start_kernel, SIZE_KERNEL_IMAGE);
 
     // Relocate kernel image for top of memory
-    elf64_relocate(
-    K1OM_MEMORY_OFFSET + (lvaddr_t) &_start_kernel,
-                   (lvaddr_t) &_start_kernel, (struct Elf64_Rela *) (rela->sh_addr
-                                   - K1OM_START_KERNEL_PHYS
-                                                                     + &_start_kernel),
-                   rela->sh_size, (struct Elf64_Sym *) (symtab->sh_addr
-                                   - K1OM_START_KERNEL_PHYS
-                                                        + &_start_kernel),
+    elf64_relocate(K1OM_MEMORY_OFFSET + (lvaddr_t)&_start_kernel,
+                   (lvaddr_t)&_start_kernel,
+                   (struct Elf64_Rela *)(rela->sh_addr - K1OM_START_KERNEL_PHYS + &_start_kernel),
+                   rela->sh_size,
+                   (struct Elf64_Sym *)(symtab->sh_addr - K1OM_START_KERNEL_PHYS + &_start_kernel),
                    symtab->sh_size,
-                   K1OM_START_KERNEL_PHYS,
-                   &_start_kernel);
+                   K1OM_START_KERNEL_PHYS, &_start_kernel);
     /*** Aliased kernel available now -- low memory still mapped ***/
 
     // Relocate stack to aliased location
diff --git a/kernel/arch/k1om/irq.c b/kernel/arch/k1om/irq.c
deleted file mode 100644 (file)
index e719347..0000000
+++ /dev/null
@@ -1,1124 +0,0 @@
-/**
- * \file
- * \brief x86-64 interrupt/exception handling utility functions
- */
-
-/*
- * Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Universitaetstr. 6, CH-8092 Zurich. Attn: Systems Group.
- */
-
-/*********************************************************************
- *
- * Copyright (C) 2003-2004,  Karlsruhe University
- *
- * File path:     glue/v4-amd64/hwirq.h
- * Description:   Macros to define interrupt handler stubs for AMD64
- *
- * 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 THE 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 THE 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.
- *
- * $Id: hwirq.h,v 1.3 2006/10/19 22:57:35 ud3 Exp $
- *
- ********************************************************************/
-
-#include <kernel.h>
-#include <stdio.h>
-#include <string.h>
-#include <irq.h>
-#include <exec.h>
-#include <gdb_stub.h>
-#include <arch_gdb_stub.h>
-#include <x86.h>
-#include <dispatch.h>
-#include <wakeup.h>
-#include <arch/x86/perfmon.h>
-#include <arch/x86/barrelfish_kpi/perfmon.h>
-#include <arch/x86/pic.h>
-#include <arch/x86/apic.h>
-#include <barrelfish_kpi/dispatcher_shared_target.h>
-#include <asmoffsets.h>
-#include <trace/trace.h>
-#include <trace_definitions/trace_defs.h>
-#include <arch/x86/timing.h>
-#include <arch/x86/syscall.h>
-#include <arch/x86/ipi_notify.h>
-#include <barrelfish_kpi/cpu_arch.h>
-#include <kcb.h>
-#include <mdb/mdb_tree.h>
-
-#include <dev/ia32_dev.h>
-
-#ifdef FPU_LAZY_CONTEXT_SWITCH
-#  include <fpu.h>
-#endif
-
-
-/**
- * \brief Define IRQ handler number 'num'.
- *
- * This defines an interrupt handler for vector #num. The way this is done is
- * quite tricky: A block of assembly is emitted, with a label pointing to
- * the beginning of that block. The label is made known as a symbol by
- * having a C function _declaration_ directly in front of the block. The
- * symbol has to be defined extern, so it is global, but its ELF visibility
- * is set "hidden", so that the symbol does not end up in the GOT. This is
- * very important for keeping the code position-independent.
- *
- * The NOERR/ERR variants depend on whether the hardware delivers an error code.
- */
-#define HW_EXCEPTION_NOERR(num)                                         \
-    void __attribute__ ((visibility ("hidden"))) hwexc_##num(void);     \
-    __asm (                                                             \
-           "\t.text                                        \n\t"        \
-           "\t.type hwexc_"#num",@function                 \n\t"        \
-           "hwexc_"#num":                                  \n\t"        \
-           "pushq $0                /* dummy error code */ \n\t"        \
-           "pushq $"#num"           /* vector number */    \n\t"        \
-           "jmp    hwexc_common     /* common stuff */     \n\t"        \
-                                                                        )
-
-#define HW_EXCEPTION_ERR(num)                                           \
-    void __attribute__ ((visibility ("hidden"))) hwexc_##num(void);     \
-    __asm (                                                             \
-           "\t.text                                        \n\t"        \
-           "\t.type hwexc_"#num",@function                 \n\t"        \
-           "hwexc_"#num":                                  \n\t"        \
-           "pushq $"#num"           /* vector number */    \n\t"        \
-           "jmp    hwexc_common     /* common stuff */     \n\t"        \
-                                                                        )
-
-#define XHW_IRQ(num)                                                    \
-    void __attribute__ ((visibility ("hidden"))) hwirq_##num(void);     \
-    __asm (                                                             \
-           "\t.text                                        \n\t"        \
-           "\t.type hwirq_"#num",@function                 \n\t"        \
-           "hwirq_"#num":                                  \n\t"        \
-           "pushq $"#num"           /* vector number */    \n\t"        \
-           "jmp    hwirq_common     /* common stuff */     \n\t"        \
-                                                                        )
-/// Noop wrapper for HW_IRQ to deal with CPP stringification problems
-#define HW_IRQ(num) XHW_IRQ(num)
-
-#define STR(x) #x
-#define XTR(x) STR(x)
-
-__asm (
-    ".text                                              \n\t"
-    "   .type hwexc_common ,@function                   \n\t"
-    "hwexc_common:                                      \n\t"
-    "testb $3, 24(%rsp) /* if CS.CPL == 0 */            \n\t"
-    "jz kernel_fault                                    \n\t"
-
-    /* User exception: save full state and return to the user.
-     * This path could be optimized by only saving the non-volatile
-     * registers (since the kernel's C path will maintain them), and
-     * having the user-mode code save them if needed. Since the
-     * current user code always does need them, we just save the full
-     * set here. */
-
-    /* decide where to save the state, the options are:
-     *    pagefault and enabled -> enabled save area
-     *    pagefault while disabled or any other trap -> trap save area
-     */
-    "pushq %rcx                                         \n\t"
-    "movq dcb_current(%rip), %rcx /* rcx = dcb_current */       \n\t"
-    "movq "XTR(OFFSETOF_DCB_DISP)"(%rcx), %rcx /* rcx = dcb_current->disp */\n\t"
-    "cmpq $14, 8(%rsp)       /* is pagefault? */        \n\t"
-    "jne save_trap                                      \n\t"
-    "cmpl $0, "XTR(OFFSETOF_DISP_DISABLED)"(%rcx) /* disp->disabled ? */\n\t"
-    "jne save_trap                                      \n\t"
-    "pushq %rbx                                         \n\t"
-    "movq 4*8(%rsp), %rbx     /* rbx = faulting IP */   \n\t"
-    "cmpq "XTR(OFFSETOF_DISP_X86_64_CRIT_PC_LOW)"(%rcx), %rbx /* crit_pc_low <= rip? */\n\t"
-    "jae disabled_test                                  \n\t"
-    "\nsave_enabled:                                    \n\t"
-    "popq %rbx                                          \n\t"
-    "addq $"XTR(OFFSETOF_DISP_X86_64_ENABLED_AREA)", %rcx /* rcx = enabled_save_area */\n\t"
-    "jmp do_save                                        \n\t"
-    "\ndisabled_test:                                   \n\t"
-    "cmpq "XTR(OFFSETOF_DISP_X86_64_CRIT_PC_HIGH)"(%rcx), %rbx /* crit_pc_high > rip? */\n\t"
-    "jae save_enabled                                   \n\t"
-    "popq %rbx                                          \n\t"
-    "\nsave_trap:                                       \n\t"
-    "addq $"XTR(OFFSETOF_DISP_X86_64_TRAP_AREA)", %rcx /* trap_save_area */\n\t"
-
-    /* save to the save area. at this point, rcx = save area ptr,
-     * rsp+8 = exception num, rsp+16 = CPU-stacked error and registers */
-    "\ndo_save:                                         \n\t"
-    "movq %rax,  0*8(%rcx)                              \n\t"
-    "popq %rax                    /* original rcx */    \n\t"
-    "movq %rbx,  1*8(%rcx)                              \n\t"
-    "movq %rax,  2*8(%rcx)                              \n\t"
-    "movq %rdx,  3*8(%rcx)                              \n\t"
-    "movq %rsi,  4*8(%rcx)                              \n\t"
-    "movq %rdi,  5*8(%rcx)                              \n\t"
-    "movq %rbp,  6*8(%rcx)                              \n\t"
-    "movq %r8,   8*8(%rcx)                              \n\t"
-    "movq %r9,   9*8(%rcx)                              \n\t"
-    "movq %r10, 10*8(%rcx)                              \n\t"
-    "movq %r11, 11*8(%rcx)                              \n\t"
-    "movq %r12, 12*8(%rcx)                              \n\t"
-    "movq %r13, 13*8(%rcx)                              \n\t"
-    "movq %r14, 14*8(%rcx)                              \n\t"
-    "movq %r15, 15*8(%rcx)                              \n\t"
-    "mov %fs, "XTR(OFFSETOF_FS_REG)"(%rcx)              \n\t"
-    "mov %gs, "XTR(OFFSETOF_GS_REG)"(%rcx)              \n\t"
-    "popq %rdi                    /* vector number */   \n\t"
-    "popq %rsi                    /* error code */      \n\t"
-    "movq %rsp, %rdx              /* CPU save area */   \n\t"
-    "callq generic_handle_user_exception                \n\t"
-    "iretq                                              \n\t"
-
-    /* a kernel fault means something bad happened, so we stack
-     * everything for the debugger to use, in the GDB frame format */
-    "\nkernel_fault:                                    \n\t"
-    "pushq 6*8(%rsp) /* SS */                           \n\t"
-    "pushq 4*8(%rsp) /* CS */                           \n\t"
-    "pushq 7*8(%rsp) /* EFLAGS */                       \n\t"
-    "pushq 5*8(%rsp) /* RIP */                          \n\t"
-    /* TODO: extend frame size and save FS/GS so we can resume afterwards */
-    "pushq %r15                                         \n\t"
-    "pushq %r14                                         \n\t"
-    "pushq %r13                                         \n\t"
-    "pushq %r12                                         \n\t"
-    "pushq %r11                                         \n\t"
-    "pushq %r10                                         \n\t"
-    "pushq %r9                                          \n\t"
-    "pushq %r8                                          \n\t"
-    "pushq 17*8(%rsp) /* RSP */                         \n\t"
-    "pushq %rbp                                         \n\t"
-    "pushq %rdi                                         \n\t"
-    "pushq %rsi                                         \n\t"
-    "pushq %rdx                                         \n\t"
-    "pushq %rcx                                         \n\t"
-    "pushq %rbx                                         \n\t"
-    "pushq %rax                                         \n\t"
-    "movq 20*8(%rsp), %rdi  /* vector number */         \n\t"
-    "movq 21*8(%rsp), %rsi  /* error code   */          \n\t"
-    "movq %rsp, %rdx       /* save area ptr*/           \n\t"
-    "jmp generic_handle_kernel_exception                \n\t"
-
-
-    /* (Device) interrupt. */
-    "   .type hwirq_common ,@function                   \n\t"
-    "hwirq_common:                                      \n\t"
-    /* If it happened in kernel_mode, simply make userspace runnable.
-     * This is a special case, since interrupts are normally disabled when
-     * entering the kernel. However, they are enabled when there is nothing
-     * to do, and the kernel goes to sleep using wait_for_interrupts() */
-    "testb $3, 16(%rsp) /* if CS.CPL == 0 */            \n\t"
-    "jz call_handle_irq                                 \n\t"
-
-    /* Happened in user mode.
-     * we need to save everything to the dispatcher. */
-    /* decide where to save the state, either enabled or disabled save areas */
-    "pushq %rdx                                         \n\t"
-    "movq dcb_current(%rip), %rdx /* rdx = dcb_current */       \n\t"
-    "movq "XTR(OFFSETOF_DCB_DISP)"(%rdx), %rdx /* rdx = dcb_current->disp */\n\t"
-    "cmpl $0, "XTR(OFFSETOF_DISP_DISABLED)"(%rdx) /* disp->disabled ? */\n\t"
-    "jne irq_save_disabled                              \n\t"
-    "pushq %rbx                                         \n\t"
-    "movq 24(%rsp), %rbx     /* rbx = faulting IP */    \n\t"
-    "cmpq "XTR(OFFSETOF_DISP_X86_64_CRIT_PC_LOW)"(%rdx), %rbx /* crit_pc_low <= rip? */\n\t"
-    "jae irq_disabled_test                              \n\t"
-    "\nirq_save_enabled:                                \n\t"
-    "popq %rbx                                          \n\t"
-    "addq $"XTR(OFFSETOF_DISP_X86_64_ENABLED_AREA)", %rdx /* rdx = enabled_save_area */\n\t"
-    "jmp irq_do_save                                    \n\t"
-    "\nirq_disabled_test:                               \n\t"
-    "cmpq "XTR(OFFSETOF_DISP_X86_64_CRIT_PC_HIGH)"(%rdx), %rbx /* crit_pc_high > rip? */\n\t"
-    "jae irq_save_enabled                               \n\t"
-    "popq %rbx                                          \n\t"
-    "\nirq_save_disabled:                               \n\t"
-    "addq $"XTR(OFFSETOF_DISP_X86_64_DISABLED_AREA)", %rdx /* disabled_save_area */\n\t"
-
-    /* save to the save area. at this point, rdx = save area ptr,
-     * rsp+8 = vector number, rsp+16 = CPU-stacked regisers */
-    "\nirq_do_save:                                     \n\t"
-    "movq %rax,  0*8(%rdx)                              \n\t"
-    "movq %rbx,  1*8(%rdx)                              \n\t"
-    "movq %rcx,  2*8(%rdx)                              \n\t"
-    "popq %rax                    /* original rdx */    \n\t"
-    "movq %rax,  3*8(%rdx)                              \n\t"
-    "movq %rsi,  4*8(%rdx)                              \n\t"
-    "movq %rdi,  5*8(%rdx)                              \n\t"
-    "movq %rbp,  6*8(%rdx)                              \n\t"
-    "movq %r8,   8*8(%rdx)                              \n\t"
-    "movq %r9,   9*8(%rdx)                              \n\t"
-    "movq %r10, 10*8(%rdx)                              \n\t"
-    "movq %r11, 11*8(%rdx)                              \n\t"
-    "movq %r12, 12*8(%rdx)                              \n\t"
-    "movq %r13, 13*8(%rdx)                              \n\t"
-    "movq %r14, 14*8(%rdx)                              \n\t"
-    "movq %r15, 15*8(%rdx)                              \n\t"
-    "mov %fs, "XTR(OFFSETOF_FS_REG)"(%rdx)              \n\t"
-    "mov %gs, "XTR(OFFSETOF_GS_REG)"(%rdx)              \n\t"
-    "popq %rdi                    /* vector number */   \n\t"
-    "movq %rsp, %rsi              /* CPU save area */   \n\t"
-    "jmp generic_handle_irq /* NB: rdx = disp save ptr*/\n\t"
-
-    "\ncall_handle_irq:                                 \n\t"
-    "popq %rdi                                          \n\t"
-    "callq handle_irq                                   \n\t"
-);
-
-// CPU exceptions
-HW_EXCEPTION_NOERR(0);
-HW_EXCEPTION_NOERR(1);
-HW_EXCEPTION_NOERR(2);
-HW_EXCEPTION_NOERR(3);
-HW_EXCEPTION_NOERR(4);
-HW_EXCEPTION_NOERR(5);
-HW_EXCEPTION_NOERR(6);
-HW_EXCEPTION_NOERR(7);
-HW_EXCEPTION_ERR(8);
-HW_EXCEPTION_NOERR(9);
-HW_EXCEPTION_ERR(10);
-HW_EXCEPTION_ERR(11);
-HW_EXCEPTION_ERR(12);
-HW_EXCEPTION_ERR(13);
-HW_EXCEPTION_ERR(14);
-HW_EXCEPTION_NOERR(16);
-HW_EXCEPTION_ERR(17);
-HW_EXCEPTION_NOERR(18);
-HW_EXCEPTION_NOERR(19);
-
-// Classic PIC interrupts
-HW_IRQ(32);
-HW_IRQ(33);
-HW_IRQ(34);
-HW_IRQ(35);
-HW_IRQ(36);
-HW_IRQ(37);
-HW_IRQ(38);
-HW_IRQ(39);
-HW_IRQ(40);
-HW_IRQ(41);
-HW_IRQ(42);
-HW_IRQ(43);
-HW_IRQ(44);
-HW_IRQ(45);
-HW_IRQ(46);
-HW_IRQ(47);
-
-// Generic interrupts
-HW_IRQ(48);
-HW_IRQ(49);
-HW_IRQ(50);
-HW_IRQ(51);
-HW_IRQ(52);
-HW_IRQ(53);
-HW_IRQ(54);
-HW_IRQ(55);
-HW_IRQ(56);
-HW_IRQ(57);
-HW_IRQ(58);
-HW_IRQ(59);
-HW_IRQ(60);
-HW_IRQ(61);
-
-// Trace IPIs
-HW_IRQ(62);
-HW_IRQ(63);
-
-// Local APIC interrupts
-HW_IRQ(248);
-HW_IRQ(249);
-HW_IRQ(250);
-HW_IRQ(251);
-HW_IRQ(252);
-HW_IRQ(253);
-HW_IRQ(254);
-
-// Reserved as "unhandled exception" handler
-HW_EXCEPTION_NOERR(666);
-
-#define ERR_PF_PRESENT          (1 << 0)
-#define ERR_PF_READ_WRITE       (1 << 1)
-#define ERR_PF_USER_SUPERVISOR  (1 << 2)
-#define ERR_PF_RESERVED         (1 << 3)
-#define ERR_PF_INSTRUCTION      (1 << 4)
-
-/// Number of (reserved) hardware exceptions
-#define NEXCEPTIONS             32
-
-/// Size of hardware IRQ dispatch table == #NIDT - #NEXCEPTIONS exceptions
-#define NDISPATCH               (NIDT - NEXCEPTIONS)
-
-/**
- * \brief Interrupt Descriptor Table (IDT) for processor this kernel is running
- * on.
- */
-static struct gate_descriptor idt[NIDT] __attribute__ ((aligned (16)));
-
-static int timer_fired = 0;
-
-#if CONFIG_TRACE && NETWORK_STACK_TRACE
-#define TRACE_ETHERSRV_MODE 1
-#endif // CONFIG_TRACE && NETWORK_STACK_TRACE
-
-
-/**
- * \brief Send interrupt notification to user-space listener.
- *
- * Sends an interrupt notification IDC to a local endpoint that
- * listens for IRQ notifications.
- *
- * \param irq   IRQ# to send in notification.
- */
-static uint32_t pkt_interrupt_count = 0;
-static void send_user_interrupt(int irq)
-{
-    assert(irq >= 0 && irq < NDISPATCH);
-    struct kcb *k = kcb_current;
-    do {
-        if (k->irq_dispatch[irq].cap.type == ObjType_EndPoint) {
-            break;
-        }
-        k = k->next;
-    } while (k && k != kcb_current);
-    // if k == NULL we don't need to switch as we only have a single kcb
-    if (k) {
-        switch_kcb(k);
-    }
-    // from here: kcb_current is the kcb for which the interrupt was intended
-    struct capability *cap = &kcb_current->irq_dispatch[irq].cap;
-
-    // Return on null cap (unhandled interrupt)
-    if(cap->type == ObjType_Null) {
-        printk(LOG_WARN, "unhandled IRQ %d\n", irq);
-        return;
-    } else if (cap->type > ObjType_Num) {
-        // XXX: HACK: this doesn't fix the root cause of having weird entries
-        // in kcb_current->irq_dispatch[], but it allows us to test the system
-        // more reliably for now. -SG
-        // Also complain to SG if this gets checked in to the main tree!
-        printk(LOG_WARN, "receiver type > %d, %d, assume unhandled\n", ObjType_Num, cap->type);
-        return;
-    }
-
-    if (irq == 0) {
-        ++pkt_interrupt_count;
-#if NETWORK_STACK_TRACE
-    trace_event(TRACE_SUBSYS_NNET, TRACE_EVENT_NNET_UIRQ, pkt_interrupt_count);
-#endif // NETWORK_STACK_TRACE
-
-    }
-//    printf("Interrupt %d\n", irq);
-    // Otherwise, cap needs to be an endpoint
-    assert(cap->type == ObjType_EndPoint);
-
-    // send empty message as notification
-    errval_t err = lmp_deliver_notification(cap);
-    if (err_is_fail(err)) {
-        if (err_no(err) == SYS_ERR_LMP_BUF_OVERFLOW) {
-            struct dispatcher_shared_generic *disp =
-                get_dispatcher_shared_generic(cap->u.endpoint.listener->disp);
-            printk(LOG_DEBUG, "%.*s: IRQ message buffer overflow on IRQ %d\n",
-                   DISP_NAME_LEN, disp->name, irq);
-        } else {
-            printk(LOG_ERR, "Unexpected error delivering IRQ\n");
-        }
-    }
-
-#ifdef SCHEDULER_RR
-    /* XXX: run the handler dispatcher immediately
-     * we shouldn't do this (we should let the scheduler decide), but because
-     * our default scheduler is braindead, this is a quick hack to make sure
-     * that mostly-sane things happen
-     */
-    dispatch(cap->u.endpoint.listener);
-#else
-    dispatch(schedule());
-#endif
-}
-
-errval_t irq_table_alloc(int *outvec)
-{
-    assert(outvec);
-    // XXX: this is O(#kcb*NDISPATCH)
-    int i;
-    for (i = 0; i < NDISPATCH; i++) {
-        struct kcb *k = kcb_current;
-        bool found_free = true;
-        do {
-            if (kcb_current->irq_dispatch[i].cap.type == ObjType_EndPoint) {
-                found_free = false;
-                break;
-            }
-            k=k->next?k->next:k;
-        } while(k != kcb_current);
-        if (found_free) {
-            break;
-        }
-    }
-    if (i == NDISPATCH) {
-        *outvec = -1;
-        return SYS_ERR_IRQ_NO_FREE_VECTOR;
-    } else {
-        *outvec = i;
-        return SYS_ERR_OK;
-    }
-}
-
-errval_t irq_table_set(unsigned int nidt, capaddr_t endpoint)
-{
-    errval_t err;
-    struct cte *recv;
-
-    err = caps_lookup_slot(&dcb_current->cspace.cap, endpoint,
-                           CPTR_BITS, &recv, CAPRIGHTS_WRITE);
-    if (err_is_fail(err)) {
-        return err_push(err, SYS_ERR_IRQ_LOOKUP);
-    }
-
-    assert(recv != NULL);
-
-    // Return w/error if cap is not an endpoint
-    if(recv->cap.type != ObjType_EndPoint) {
-        return SYS_ERR_IRQ_NOT_ENDPOINT;
-    }
-
-    // Return w/error if no listener on endpoint
-    if(recv->cap.u.endpoint.listener == NULL) {
-        return SYS_ERR_IRQ_NO_LISTENER;
-    }
-
-    if(nidt < NDISPATCH) {
-        // check that we don't overwrite someone else's handler
-        if (kcb_current->irq_dispatch[nidt].cap.type != ObjType_Null) {
-            printf("kernel: installing new handler for IRQ %d\n", nidt);
-        }
-        err = caps_copy_to_cte(&kcb_current->irq_dispatch[nidt], recv, false, 0, 0);
-        return err;
-    } else {
-        return SYS_ERR_IRQ_INVALID;
-    }
-}
-
-errval_t irq_table_delete(unsigned int nidt)
-{
-    if(nidt < NDISPATCH) {
-        kcb_current->irq_dispatch[nidt].cap.type = ObjType_Null;
-        return SYS_ERR_OK;
-    } else {
-        return SYS_ERR_IRQ_INVALID;
-    }
-}
-
-errval_t irq_table_notify_domains(struct kcb *kcb)
-{
-    uintptr_t msg[] = { 1 };
-    for (int i = 0; i < NDISPATCH; i++) {
-        if (kcb->irq_dispatch[i].cap.type == ObjType_EndPoint) {
-            struct capability *cap = &kcb->irq_dispatch[i].cap;
-            // 1 word message as notification
-            errval_t err = lmp_deliver_payload(cap, NULL, msg, 1, false);
-            if (err_is_fail(err)) {
-                if (err_no(err) == SYS_ERR_LMP_BUF_OVERFLOW) {
-                    struct dispatcher_shared_generic *disp =
-                        get_dispatcher_shared_generic(cap->u.endpoint.listener->disp);
-                    printk(LOG_DEBUG, "%.*s: IRQ message buffer overflow\n",
-                            DISP_NAME_LEN, disp->name);
-                } else {
-                    printk(LOG_ERR, "Unexpected error delivering IRQ\n");
-                }
-            }
-        }
-        kcb->irq_dispatch[i].cap.type = ObjType_Null;
-    }
-    return SYS_ERR_OK;
-}
-
-/**
- * \brief Handles kernel exceptions
- *
- * \param vec   Vector number of exception
- * \param error Error code from CPU, or 0 for an exception without an error code
- * \param gdb_save_frame Pointer to save area for registers stacked by trap handler
- */
-static __attribute__ ((used,noreturn))
-    void generic_handle_kernel_exception(uint64_t vec, uint64_t error,
-                                         uintptr_t *gdb_save_frame)
-{
-    lvaddr_t fault_address;
-    char *descr;
-
-    if (vec == 666) {
-        panic("unhandled kernel exception (vector 666)");
-    }
-
-    assert(vec < NEXCEPTIONS);
-
-    printk(LOG_PANIC, "exception %d (error code 0x%lx): ", (int)vec, error);
-
-    if (vec == ia32_vec_pf) {
-        printf("%s page fault due to %s%s, while in %s mode%s\n",
-               error & ERR_PF_READ_WRITE ? "write" : "read",
-               error & ERR_PF_PRESENT ? "access violation" : "page not present",
-               error & ERR_PF_RESERVED ? ", reserved bits set in page table"
-               : "",
-               error & ERR_PF_USER_SUPERVISOR ? "user" : "supervisor",
-               error & ERR_PF_INSTRUCTION ? ", by instruction fetch" : "");
-
-        __asm volatile("mov %%cr2, %[fault_address]"
-                       : [fault_address] "=r" (fault_address));
-        printf("Address that caused the fault: 0x%lx\n", fault_address);
-
-    } else if ((descr = ia32_exc_vec_describe(vec))) {
-        printf("%s\n", descr);
-    } else {
-        printf("unhandled exception!\n");
-    }
-
-    // Print faulting instruction pointer
-    uintptr_t rip = gdb_save_frame[GDB_X86_64_RIP_REG];
-    printf("Faulting instruction pointer (or next instruction): 0x%lx\n", rip);
-    printf("  => i.e. unrelocated kernel address 0x%lx\n",
-           rip - (uintptr_t)&_start_kernel + K1OM_START_KERNEL_PHYS);
-
-    printf("Registers:\n");
-    printf(" rax: 0x%016lx  r8 : 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RAX_REG],
-           gdb_save_frame[GDB_X86_64_R8_REG]);
-    printf(" rbx: 0x%016lx  r9 : 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RBX_REG],
-           gdb_save_frame[GDB_X86_64_R9_REG]);
-    printf(" rcx: 0x%016lx  r10: 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RCX_REG],
-           gdb_save_frame[GDB_X86_64_R10_REG]);
-    printf(" rdx: 0x%016lx  r11: 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RDX_REG],
-           gdb_save_frame[GDB_X86_64_R11_REG]);
-    printf(" rsp: 0x%016lx  r12: 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RSP_REG],
-           gdb_save_frame[GDB_X86_64_R12_REG]);
-    printf(" rdi: 0x%016lx  r13: 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RDI_REG],
-           gdb_save_frame[GDB_X86_64_R13_REG]);
-    printf(" rsi: 0x%016lx  r14: 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RSI_REG],
-           gdb_save_frame[GDB_X86_64_R14_REG]);
-    printf(" rip: 0x%016lx  r15: 0x%016lx\n",
-           gdb_save_frame[GDB_X86_64_RIP_REG],
-           gdb_save_frame[GDB_X86_64_R15_REG]);
-
-    // Print the top 10 stack words
-    printf("Top o' stack:\n");
-    for(int i = 0; i < 10; i++) {
-        unsigned long *p = (unsigned long *)gdb_save_frame[GDB_X86_64_RSP_REG] + i;
-        printf(" %d \t 0x%016lx (%lu)\n", i, *p, *p);
-    }
-
-    // Drop to the debugger
-    gdb_handle_exception(vec, gdb_save_frame);
-    panic("gdb_handle_exception returned");
-}
-
-/**
- * \brief copies CPU-stacked registers to a dispatcher save area
- */
-static void copy_cpu_frame_to_dispatcher(
-    uintptr_t * NONNULL COUNT(X86_SAVE_AREA_SIZE) cpu_save_area,
-    struct registers_x86_64 *disp_save_area)
-{
-    // sanity checks
-    assert((cpu_save_area[X86_SAVE_EFLAGS] & USER_EFLAGS) == USER_EFLAGS);
-
-    disp_save_area->rsp = cpu_save_area[X86_SAVE_RSP];
-    disp_save_area->eflags = cpu_save_area[X86_SAVE_EFLAGS];
-    disp_save_area->rip = cpu_save_area[X86_SAVE_RIP];
-}
-
-/**
- * \brief Handles user-mode exceptions
- *
- * \param vec   Vector number of exception
- * \param error Error code from CPU, or 0 for an exception without an error code
- * \param cpu_save_area  Pointer to save area for registers stacked by CPU
- * \param disp_save_area Pointer to save area in dispatcher
- */
-static __attribute__ ((used))
-    void generic_handle_user_exception(int vec, uint64_t error,
-                uintptr_t * NONNULL COUNT(X86_SAVE_AREA_SIZE) cpu_save_area,
-                struct registers_x86_64 *disp_save_area)
-{
-    assert(dcb_current->disp_cte.cap.type == ObjType_Frame);
-    dispatcher_handle_t handle = dcb_current->disp;
-    struct dispatcher_shared_generic *disp =
-        get_dispatcher_shared_generic(handle);
-    uint64_t rip = cpu_save_area[X86_SAVE_RIP];
-    uint64_t rsp = cpu_save_area[X86_SAVE_RSP];
-    lvaddr_t fault_address, handler = 0, param = 0;
-
-    assert(vec < NEXCEPTIONS);
-    assert((cpu_save_area[X86_SAVE_CS] & 0x3) != 0); // CS.CPL > 0
-
-    copy_cpu_frame_to_dispatcher(cpu_save_area, disp_save_area);
-
-    bool disabled = dispatcher_is_disabled_ip(handle, rip);
-    dcb_current->disabled = disabled;
-
-    if (disabled) {
-        dcb_current->faults_taken++;
-    }
-
-    // Store FPU state if it's used
-    // Do this for every trap when the current domain used the FPU
-    // Do it for FPU not available traps in any case (to save the last FPU user)
-    // XXX: Need to reset fpu_dcb when that DCB is deleted
-    if(fpu_dcb != NULL &&
-       (fpu_dcb == dcb_current || vec == IDT_NM)) {
-        struct dispatcher_shared_generic *dst =
-            get_dispatcher_shared_generic(fpu_dcb->disp);
-
-        // Turn FPU trap off temporarily for saving its state
-        bool trap = fpu_trap_get();
-        fpu_trap_off();
-
-        if(fpu_dcb->disabled) {
-            fpu_save(dispatcher_get_disabled_fpu_save_area(fpu_dcb->disp));
-           dst->fpu_used = 1;
-        } else {
-            assert(!fpu_dcb->disabled);
-            fpu_save(dispatcher_get_enabled_fpu_save_area(fpu_dcb->disp));
-           dst->fpu_used = 2;
-        }
-
-        if(trap) {
-            fpu_trap_on();
-        }
-    }
-
-    if (vec == IDT_PF) { // Page fault
-        // Get fault address
-        __asm volatile("mov %%cr2, %[fault_address]"
-                       : [fault_address] "=r" (fault_address));
-
-        printk(LOG_WARN, "user page fault%s in '%.*s': addr %lx IP %lx SP %lx "
-                         "error 0x%lx\n",
-               disabled ? " WHILE DISABLED" : "", DISP_NAME_LEN,
-               disp->name, fault_address, rip, rsp, error);
-
-        /* sanity-check that the trap handler saved in the right place */
-        assert((disabled && disp_save_area == dispatcher_get_trap_save_area(handle))
-               || (!disabled && disp_save_area == dispatcher_get_enabled_save_area(handle)));
-        if (disabled) {
-            handler = disp->dispatcher_pagefault_disabled;
-        } else {
-            handler = disp->dispatcher_pagefault;
-        }
-        param = fault_address;
-    } else if (vec == IDT_NMI) {
-        printk(LOG_WARN, "NMI - ignoring\n");
-        dispatch(dcb_current);
-    } else if (vec == IDT_NM) {     // device not available (FPU) exception
-        debug(SUBSYS_DISPATCH, "FPU trap in %.*s at 0x%" PRIxPTR "\n",
-              DISP_NAME_LEN, disp->name, rip);
-        assert(!dcb_current->is_vm_guest);
-
-        /* Intel system programming part 1: 2.3.1, 2.5, 11, 12.5.1
-         * clear the TS flag (flag that says, that the FPU is not available)
-         */
-        clts();
-
-        // Remember FPU-using DCB
-        fpu_dcb = dcb_current;
-
-        // Wipe FPU for protection and to initialize it in case we trapped while
-        // disabled
-        fpu_init();
-
-        if(disabled) {
-            // Initialize FPU (done earlier) and ignore trap
-            dispatch(dcb_current);
-        } else {
-            // defer trap to user-space
-            // FPUs are switched eagerly while disabled, there should be no trap
-            assert(disp_save_area == dispatcher_get_trap_save_area(handle));
-            handler = disp->dispatcher_trap;
-            param = vec;
-        }
-    } else if (vec == IDT_MF) {
-        uint16_t fpu_status;
-
-        __asm volatile("fnstsw %0" : "=a" (fpu_status));
-
-        printk(LOG_WARN, "FPU error%s in '%.*s': IP %" PRIxPTR " FPU status %x\n",
-               disabled ? " WHILE DISABLED" : "", DISP_NAME_LEN,
-               disp->name, rip, fpu_status);
-
-        handler = disp->dispatcher_trap;
-        param = vec;
-    } else if (vec == IDT_MC) {
-        // TODO: provide more useful information about the cause
-        panic("machine check exception while in user mode");
-    } else { // All other traps
-        printk(LOG_WARN, "user trap #%d%s in '%.*s': IP %lx, error %lx\n",
-               vec, disabled ? " WHILE DISABLED" : "",
-               DISP_NAME_LEN, disp->name, rip, error);
-        assert(disp_save_area == dispatcher_get_trap_save_area(handle));
-        if (disabled) {
-            if (vec == IDT_DB) { // debug exception: just continue
-                resume(dispatcher_get_trap_save_area(handle));
-            } else {
-                // can't handle a trap while disabled: nowhere safe to deliver it
-                scheduler_remove(dcb_current);
-                dispatch(schedule());
-            }
-        } else {
-            handler = disp->dispatcher_trap;
-            param = vec;
-        }
-    }
-
-    // Make unrunnable if it has taken too many faults
-    if (dcb_current->faults_taken > 2) {
-        printk(LOG_WARN, "generic_handle_user_exception: too many faults, "
-               "making domain unrunnable\n");
-        dcb_current->faults_taken = 0; // just in case it gets restarted
-        scheduler_remove(dcb_current);
-        dispatch(schedule());
-    }
-
-    /* resume user to save area */
-    disp->disabled = 1;
-    if(handler == 0) {
-        printk(LOG_WARN, "no suitable handler for this type of fault, "
-               "making domain unrunnable\n");
-        scheduler_remove(dcb_current);
-        dispatch(schedule());
-    } else {
-        cpu_save_area[X86_SAVE_RIP] = handler;
-        cpu_save_area[X86_SAVE_EFLAGS] = USER_EFLAGS;
-    }
-
-    /* XXX: get GCC to load up the argument registers before returning */
-    register uintptr_t arg0 __asm ("%rdi") = disp->udisp;
-    register uintptr_t arg1 __asm ("%rsi") = param;
-    register uintptr_t arg2 __asm ("%rdx") = error;
-    register uintptr_t arg3 __asm ("%rcx") = rip;
-    __asm volatile("" :: "r" (arg0), "r" (arg1), "r" (arg2), "r" (arg3));
-}
-
-static void
-update_kernel_now(void)
-{
-    uint64_t tsc_now = rdtsc();
-    #ifdef CONFIG_ONESHOT_TIMER
-    uint64_t ticks = tsc_now - tsc_lasttime;
-    kernel_now += ticks / timing_get_tsc_per_ms();
-    #else // !CONFIG_ONESHOT_TIMER
-    // maintain compatibility with old behaviour. Not sure if it is
-    // actually needed. -AKK
-    //
-    // Ignore timeslice if it happens too closely (less than half
-    // of the TSC ticks that are supposed to pass) to the last.
-    // In that case we have just synced timers and see a spurious
-    // APIC timer interrupt.
-    if(tsc_now - tsc_lasttime >
-       (kernel_timeslice * timing_get_tsc_per_ms()) / 2) {
-        kernel_now += kernel_timeslice;
-    }
-    #endif // CONFIG_ONESHOT_TIMER
-    tsc_lasttime = tsc_now;
-}
-
-/// Handle an IRQ that arrived, either while in user or kernel mode (HLT)
-static __attribute__ ((used)) void handle_irq(int vector)
-{
-    int irq = vector - NEXCEPTIONS;
-    debug(SUBSYS_DISPATCH, "IRQ vector %d (irq %d) while %s\n", vector, irq,
-          dcb_current ? (dcb_current->disabled ? "disabled": "enabled") : "in kernel");
-
-
-    // if we were in wait_for_interrupt(), unmask timer before running userspace
-    if (dcb_current == NULL && kernel_ticks_enabled) {
-        apic_unmask_timer();
-    }
-
-#if TRACE_ETHERSRV_MODE
-    trace_event(TRACE_SUBSYS_NNET, TRACE_EVENT_NNET_IRQ, vector);
-#endif // TRACE_ETHERSRV_MODE
-
-    // APIC timer interrupt: handle in kernel and reschedule
-    if (vector == APIC_TIMER_INTERRUPT_VECTOR) {
-        // count time slices
-        timer_fired ++;
-
-        // switch kcb every other timeslice
-        if (!kcb_sched_suspended && timer_fired % 2 == 0 && kcb_current->next) {
-            //printk(LOG_NOTE, "switching from kcb(%p) to kcb(%p)\n", kcb_current, kcb_current->next);
-            switch_kcb(kcb_current->next);
-        }
-
-        apic_eoi();
-           assert(kernel_ticks_enabled);
-           update_kernel_now();
-        trace_event(TRACE_SUBSYS_KERNEL, TRACE_EVENT_KERNEL_TIMER, kernel_now);
-        wakeup_check(kernel_now+kcb_current->kernel_off);
-    } else if (vector == APIC_PERFORMANCE_INTERRUPT_VECTOR) {
-        // Handle performance counter overflow
-        // Reset counters
-        perfmon_measure_reset();
-        if(dcb_current!=NULL) {
-            // Get faulting instruction pointer
-            struct registers_x86_64 *disp_save_area = dcb_current->disabled ?
-                dispatcher_get_disabled_save_area(dcb_current->disp) :
-                dispatcher_get_enabled_save_area(dcb_current->disp);
-            struct dispatcher_shared_generic *disp =
-                get_dispatcher_shared_generic(dcb_current->disp);
-
-            // Setup data structure for LMP transfer to user level handler
-            struct perfmon_overflow_data data = {
-                .ip = disp_save_area->rip
-            };
-            strncpy(data.name, disp->name, PERFMON_DISP_NAME_LEN);
-
-            // Call overflow handler represented by endpoint
-            extern struct capability perfmon_callback_ep;
-            size_t payload_len = sizeof(struct perfmon_overflow_data)/ sizeof(uintptr_t)+1;
-               errval_t err = lmp_deliver_payload(&perfmon_callback_ep,
-                             NULL,
-                             (uintptr_t*) &data,
-                             payload_len,
-                             false);
-
-            // Make sure delivery was okay. SYS_ERR_LMP_BUF_OVERFLOW is okay for now
-            assert(err_is_ok(err) || err_no(err)==SYS_ERR_LMP_BUF_OVERFLOW);
-        } else {
-            // This should never happen, as interrupts are disabled in kernel
-            printf("Performance counter overflow interrupt from "
-                   "apic in kernel level\n");
-        }
-        apic_eoi();
-    } else if (vector == APIC_ERROR_INTERRUPT_VECTOR) {
-        printk(LOG_ERR, "APIC error interrupt fired!\n");
-        xapic_esr_t esr = apic_get_esr();
-        char str[256];
-        xapic_esr_prtval(str, 256, esr);
-        printf("%s\n", str);
-        apic_eoi();
-    } else if (vector == APIC_INTER_CORE_VECTOR) {
-        apic_eoi();
-        ipi_handle_notify();
-    } else if (vector == APIC_INTER_HALT_VECTOR) {
-        apic_eoi();
-        // Update kernel_off for all KCBs
-        struct kcb *k = kcb_current;
-        do{
-            k->kernel_off = kernel_now;
-            k = k->next;
-        } while(k && k!=kcb_current);
-        // Stop the core
-        halt();
-    } else if (vector == APIC_SPURIOUS_INTERRUPT_VECTOR) {
-        // ignore
-        printk(LOG_DEBUG, "spurious interrupt\n");
-    }
-
-#if 0
- else if (irq >= 0 && irq <= 15) { // classic PIC device interrupt
-     printk(LOG_NOTE, "got interrupt %d!\n", irq);
-
-        apic_eoi();
-
-        // only handle PIC interrupts on the BSP core
-        if (apic_is_bsp()) {
-            if (pic_have_interrupt(irq)) {
-                pic_eoi(irq);
-                send_user_interrupt(irq);
-            } else { // no interrupt pending, check for a different one (!)
-                irq = pic_pending_interrupt();
-                if (irq == -1) { // really nothing pending
-                    printk(LOG_NOTE, "spurious interrupt (IRQ %d)\n", irq);
-                } else { // why does this happen?! -AB
-                    printk(LOG_NOTE, "IRQ %d reported on wrong vector (%d)\n",
-                           irq, vector - NEXCEPTIONS);
-                    pic_eoi(irq);
-                    send_user_interrupt(irq);
-                }
-            }
-        }
-    }
-#endif
-    else { // APIC device interrupt (or IPI)
-        //printk(LOG_NOTE, "interrupt %d vector %d!\n", irq, vector);
-        apic_eoi();
-        send_user_interrupt(irq);
-    }
-
-    // reschedule (because the runnable processes may have changed) and dispatch
-    /* FIXME: the round-robin scheduler doesn't do the best thing here:
-     * it always picks the next task, but we only really want to do that on
-     * a timer tick
-     */
-    dispatch(schedule());
-    panic("dispatch() returned");
-}
-
-/**
- * \brief Handles device interrupts that arrive while in user mode
- *
- * \param vector    Vector number
- * \param cpu_save_area  Pointer to save area for registers stacked by CPU
- * \param disp_save_area Pointer to save area in dispatcher
- */
-static __attribute__ ((used, noreturn)) void
-generic_handle_irq(int vector,
-                   uintptr_t * NONNULL COUNT(X86_SAVE_AREA_SIZE) cpu_save_area,
-                   struct registers_x86_64 *disp_save_area)
-{
-    assert(dcb_current->disp_cte.cap.type == ObjType_Frame);
-    dispatcher_handle_t handle = dcb_current->disp;
-    uint64_t rip = cpu_save_area[X86_SAVE_RIP];
-    assert(vector < NIDT && vector >= NEXCEPTIONS);
-
-    // Copy CPU-saved registers to dispatcher save area
-    copy_cpu_frame_to_dispatcher(cpu_save_area, disp_save_area);
-
-    /* sanity-check that the trap handler saved in the right place,
-     * and update disabled flag in DCB */
-    if (disp_save_area == dispatcher_get_disabled_save_area(handle)) {
-        assert(dispatcher_is_disabled_ip(handle, rip));
-        dcb_current->disabled = true;
-    } else {
-        assert(disp_save_area == dispatcher_get_enabled_save_area(handle));
-        assert(!dispatcher_is_disabled_ip(handle, rip));
-        dcb_current->disabled = false;
-    }
-
-    handle_irq(vector);
-    resume(disp_save_area);
-}
-
-/* Utility function for code below; initialises a gate_descriptor */
-static void setgd(struct gate_descriptor *gd, void (* handler)(void),
-                  int ist, int type, int dpl, int selector)
-{
-    memset(gd, 0, sizeof(struct gate_descriptor));
-    gd->gd_looffset = (uintptr_t)handler & ((1UL << 16) - 1);
-    gd->gd_hioffset = (uintptr_t)handler >> 16;
-    gd->gd_selector = selector;
-    gd->gd_ist = ist;
-    gd->gd_type = type;
-    gd->gd_dpl = dpl;
-    gd->gd_p = 1;
-}
-
-/**
- * \brief Sets up the default IDT for current CPU.
- */
-void setup_default_idt(void)
-{
-    struct region_descriptor region = {         // set default IDT
-        .rd_limit = NIDT * sizeof(idt[0]) - 1,
-        .rd_base = (uint64_t)&idt
-    };
-    int i;
-
-    // reset IDT
-    memset((void *)&idt, 0, NIDT * sizeof(idt[0]));
-
-    // initialize IDT with default generic handlers
-    for (i = 0; i < NIDT; i++)
-        setgd(&idt[i], hwexc_666, 0, SDT_SYSIGT, SEL_KPL,
-              GSEL(KCODE_SEL, SEL_KPL));
-
-    /* Setup exception handlers */
-    setgd(&idt[0], hwexc_0, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[1], hwexc_1, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[2], hwexc_2, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[3], hwexc_3, 0, SDT_SYSIGT, SEL_UPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[4], hwexc_4, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[5], hwexc_5, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[6], hwexc_6, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[7], hwexc_7, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[8], hwexc_8, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[9], hwexc_9, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[10], hwexc_10, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[11], hwexc_11, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[12], hwexc_12, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[13], hwexc_13, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[14], hwexc_14, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    // Interrupt 15 is undefined
-    setgd(&idt[16], hwexc_16, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[17], hwexc_17, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[18], hwexc_18, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[19], hwexc_19, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    // Interrupts 20 - 31 are reserved
-
-    /* Setup classic PIC interrupt handlers */
-    setgd(&idt[32], hwirq_32, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[33], hwirq_33, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[34], hwirq_34, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[35], hwirq_35, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[36], hwirq_36, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[37], hwirq_37, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[38], hwirq_38, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[39], hwirq_39, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[40], hwirq_40, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[41], hwirq_41, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[42], hwirq_42, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[43], hwirq_43, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[44], hwirq_44, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[45], hwirq_45, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[46], hwirq_46, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[47], hwirq_47, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-
-    // Setup generic interrupt handlers
-    setgd(&idt[48], hwirq_48, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[49], hwirq_49, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[50], hwirq_50, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[50], hwirq_50, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[51], hwirq_51, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[52], hwirq_52, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[53], hwirq_53, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[54], hwirq_54, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[55], hwirq_55, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[56], hwirq_56, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[57], hwirq_57, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[58], hwirq_58, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[59], hwirq_59, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[60], hwirq_60, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[61], hwirq_61, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-
-    // XXX Interrupts used for TRACE IPIs
-    setgd(&idt[62], hwirq_62, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[63], hwirq_63, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-
-    // Setup local APIC interrupt handlers
-    setgd(&idt[248], hwirq_248, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[249], hwirq_249, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[250], hwirq_250, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[251], hwirq_251, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[252], hwirq_252, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[253], hwirq_253, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-    setgd(&idt[254], hwirq_254, 0, SDT_SYSIGT, SEL_KPL, GSEL(KCODE_SEL, SEL_KPL));
-
-    /* Load IDT register */
-    __asm volatile("lidt %0" :: "m" (region));
-}
diff --git a/kernel/arch/k1om/kernel.S b/kernel/arch/k1om/kernel.S
deleted file mode 100644 (file)
index 3350497..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * \file
- * \brief Bootstrap the kernel.
- */
-
-/*
- * Copyright (c) 2007, 2008, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <multiboot.h>
-#include <target/k1om/offsets_target.h>
-
-/* The flags for the Multiboot header */
-#define MB_FLAGS (MULTIBOOT_HEADER_FLAG_MODS_PGALIGNED | MULTIBOOT_HEADER_FLAG_NEED_MEMINFO)
-
-          .text
-          .globl    start, halt
-          /* Multiboot header, 4-byte aligned */
-          .align    4
-          .long     MULTIBOOT_HEADER_MAGIC               /* magic */
-          .long     MB_FLAGS                             /* flags */
-          .long     -(MULTIBOOT_HEADER_MAGIC + MB_FLAGS) /* checksum */
-
-start:
-          /* Initialize the stack pointer */
-          lea       (k1om_kernel_stack + K1OM_KERNEL_STACK_SIZE)(%rip), %rsp
-
-          /* Reset EFLAGS */
-          pushq     $0
-          popf
-
-          /* Enter architecture-specific init -- this should never return */
-          movl      %eax, %edi          /* Multiboot magic value */
-          movl      %ebx, %esi          /* Pointer to multiboot info struct */
-          call      arch_init
-
-          /* Halt -- this should never be reached */
-halt:     hlt
-          jmp       halt
index 92b09d3..d639461 100644 (file)
@@ -18,7 +18,7 @@ OUTPUT_ARCH(k1om)
 _start_kernel = K1OM_START_KERNEL_PHYS;
 
 
-ENTRY(kernel_start)
+ENTRY(start)
 SECTIONS
 {
  /* Be careful parts of head_64.S assume startup_32 is at
@@ -105,7 +105,7 @@ OUTPUT_ARCH(k1om)
 
 _start_kernel = K1OM_START_KERNEL_PHYS;
 
-ENTRY(kernel_start)
+ENTRY(start)
 SECTIONS {
         . = ALIGN(4k);
         .text K1OM_START_KERNEL_PHYS : ALIGN(4k)
diff --git a/kernel/arch/k1om/microbenchmarks.c b/kernel/arch/k1om/microbenchmarks.c
deleted file mode 100644 (file)
index 78e94cf..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-/**
- * \file
- * \brief Architecture-specific microbenchmarks.
- */
-
-/*
- * Copyright (c) 2007, 2008, 2009, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <kernel.h>
-#include <microbenchmarks.h>
-#include <apic.h>
-#include <x86.h>
-
-// address space switch (mov to cr3)
-static int asswitch_func(struct microbench *mb)
-{
-    uint64_t start, end;
-    uint64_t asvalue;
-    
-    // Put the cr3 value in the asvalue register for now
-    __asm__ __volatile__("mov %%cr3, %0" : "=r" (asvalue));
-    
-    start = rdtscp();
-    for (int i = 0; i < MICROBENCH_ITERATIONS; i++) {
-        __asm__ __volatile__(
-            "mov %0, %%cr3"
-            :
-            : "r" (asvalue));
-    }
-    end = rdtscp();
-    
-    mb->result = end - start;
-    
-    return 0;
-}
-
-static int wrmsr_func(struct microbench *mb)
-{
-    uint64_t start, end;
-    
-    start = rdtscp();
-    for (int i = 0; i < MICROBENCH_ITERATIONS; i++) {
-        wrmsr(MSR_IA32_FSBASE, 0);
-    }
-    end = rdtscp();
-
-    mb->result = end - start;
-    
-    return 0;
-}
-
-struct microbench arch_benchmarks[] = {
-    {
-        .name = "wrmsr",
-        .run_func = wrmsr_func
-    },
-    {
-        .name = "address space switch (mov to cr3)",
-        .run_func = asswitch_func
-    }
-};
-
-size_t arch_benchmarks_size = sizeof(arch_benchmarks) / sizeof(struct microbench);
diff --git a/kernel/arch/k1om/page_mappings_arch.c b/kernel/arch/k1om/page_mappings_arch.c
deleted file mode 100644 (file)
index 4468ec4..0000000
+++ /dev/null
@@ -1,465 +0,0 @@
-/**
- * \file
- * \brief
- */
-
-/*
- * Copyright (c) 2010, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <kernel.h>
-#include <dispatch.h>
-#include <target/k1om/paging_kernel_target.h>
-#include <target/k1om/offsets_target.h>
-#include <paging_kernel_arch.h>
-#include <mdb/mdb_tree.h>
-#include <string.h>
-#include <barrelfish_kpi/init.h>
-#include <cap_predicates.h>
-
-static inline struct cte *cte_for_cap(struct capability *cap)
-{
-    return (struct cte *) (cap - offsetof(struct cte, cap));
-}
-
-/// Map within a x86_64 non leaf ptable
-static errval_t x86_64_non_ptable(struct capability *dest, cslot_t slot,
-                                  struct capability *src, uintptr_t flags,
-                                  uintptr_t offset, size_t pte_count)
-{
-    if (slot >= X86_64_PTABLE_SIZE) { // Within pagetable
-        return SYS_ERR_VNODE_SLOT_INVALID;
-    }
-
-    if (type_is_vnode(src->type) && pte_count != 1) { // only allow single ptable mappings
-        printf("src type and count mismatch\n");
-        return SYS_ERR_VM_MAP_SIZE;
-    }
-
-    if (slot + pte_count > X86_64_PTABLE_SIZE) { // mapping size ok
-        printf("mapping size invalid (%zd)\n", pte_count);
-        return SYS_ERR_VM_MAP_SIZE;
-    }
-
-    size_t page_size = 0;
-    switch (dest->type) {
-        case ObjType_VNode_x86_64_pml4:
-            if (src->type != ObjType_VNode_x86_64_pdpt) { // Right mapping
-                printf("src type invalid\n");
-                return SYS_ERR_WRONG_MAPPING;
-            }
-            if(slot >= X86_64_PML4_BASE(K1OM_MEMORY_OFFSET)) { // Kernel mapped here
-                return SYS_ERR_VNODE_SLOT_RESERVED;
-            }
-            break;
-        case ObjType_VNode_x86_64_pdpt:
-            // TODO: huge page support, set page_size to HUGE_PAGE_SIZE
-            if (src->type != ObjType_VNode_x86_64_pdir) { // Right mapping
-                printf("src type invalid\n");
-                return SYS_ERR_WRONG_MAPPING;
-            }
-            break;
-        case ObjType_VNode_x86_64_pdir:
-            // TODO: superpage support, set page_size to SUPER_PAGE_SIZE
-            if (src->type != ObjType_VNode_x86_64_ptable) { // Right mapping
-                printf("src type invalid\n");
-                return SYS_ERR_WRONG_MAPPING;
-            }
-            break;
-        default:
-            printf("dest type invalid\n");
-            return SYS_ERR_DEST_TYPE_INVALID;
-    }
-
-    // Convert destination base address
-    genpaddr_t dest_gp   = get_address(dest);
-    lpaddr_t dest_lp     = gen_phys_to_local_phys(dest_gp);
-    lvaddr_t dest_lv     = local_phys_to_mem(dest_lp);
-    // Convert source base address
-    genpaddr_t src_gp   = get_address(src);
-    lpaddr_t src_lp     = gen_phys_to_local_phys(src_gp);
-
-    // set metadata
-    struct cte *src_cte = cte_for_cap(src);
-    src_cte->mapping_info.pte = dest_lp + slot * sizeof(union x86_64_ptable_entry);
-    src_cte->mapping_info.pte_count = pte_count;
-    src_cte->mapping_info.offset = offset;
-
-    cslot_t last_slot = slot + pte_count;
-    for (; slot < last_slot; slot++, offset += page_size) {
-        // Destination
-        union x86_64_pdir_entry *entry = (union x86_64_pdir_entry *)dest_lv + slot;
-
-        if (X86_64_IS_PRESENT(entry)) {
-            // cleanup mapping info
-            // TODO: cleanup already mapped pages
-            memset(&src_cte->mapping_info, 0, sizeof(struct mapping_info));
-            return SYS_ERR_VNODE_SLOT_INUSE;
-        }
-
-        paging_k1om_map_table(entry, src_lp + offset);
-    }
-
-    return SYS_ERR_OK;
-}
-
-/// Map within a x86_64 ptable
-static errval_t x86_64_ptable(struct capability *dest, cslot_t slot,
-                              struct capability *src, uintptr_t mflags,
-                              uintptr_t offset, size_t pte_count)
-{
-    if (slot >= X86_64_PTABLE_SIZE) { // Within pagetable
-        return SYS_ERR_VNODE_SLOT_INVALID;
-    }
-
-    if (slot + pte_count > X86_64_PTABLE_SIZE) { // mapping size ok
-        printf("mapping size invalid (%zd)\n", pte_count);
-        return SYS_ERR_VM_MAP_SIZE;
-    }
-
-    if (src->type != ObjType_Frame &&
-        src->type != ObjType_DevFrame) { // Right mapping
-        printf("src type invalid\n");
-        return SYS_ERR_WRONG_MAPPING;
-    }
-
-    // check offset within frame
-    genpaddr_t off = offset;
-    if (off + pte_count * X86_64_BASE_PAGE_SIZE > get_size(src)) {
-        printf("frame offset invalid\n");
-        return SYS_ERR_FRAME_OFFSET_INVALID;
-    }
-
-
-    /* Calculate page access protection flags */
-    // Get frame cap rights
-    paging_x86_64_flags_t flags =
-        paging_k1om_cap_to_page_flags(src->rights);
-    // Mask with provided access rights mask
-    flags = paging_k1om_mask_attrs(flags, X86_64_PTABLE_ACCESS(mflags));
-    // Add additional arch-specific flags
-    flags |= X86_64_PTABLE_FLAGS(mflags);
-    // Unconditionally mark the page present
-    flags |= X86_64_PTABLE_PRESENT;
-
-    // Convert destination base address
-    genpaddr_t dest_gp   = get_address(dest);
-    lpaddr_t dest_lp     = gen_phys_to_local_phys(dest_gp);
-    lvaddr_t dest_lv     = local_phys_to_mem(dest_lp);
-    // Convert source base address
-    genpaddr_t src_gp   = get_address(src);
-    lpaddr_t src_lp     = gen_phys_to_local_phys(src_gp);
-    // Set metadata
-    struct cte *src_cte = cte_for_cap(src);
-    src_cte->mapping_info.pte = dest_lp + slot * sizeof(union x86_64_ptable_entry);
-    src_cte->mapping_info.pte_count = pte_count;
-    src_cte->mapping_info.offset = offset;
-
-    cslot_t last_slot = slot + pte_count;
-    for (; slot < last_slot; slot++, offset += X86_64_BASE_PAGE_SIZE) {
-        union x86_64_ptable_entry *entry =
-            (union x86_64_ptable_entry *)dest_lv + slot;
-
-        /* FIXME: Flush TLB if the page is already present
-         * in the meantime, since we don't do this, we just fail to avoid
-         * ever reusing a VA mapping */
-        if (X86_64_IS_PRESENT(entry)) {
-            // TODO: cleanup already mapped pages
-            memset(&src_cte->mapping_info, 0, sizeof(struct mapping_info));
-            debug(LOG_WARN, "Trying to remap an already-present page is NYI, but "
-                  "this is most likely a user-space bug!\n");
-            return SYS_ERR_VNODE_SLOT_INUSE;
-        }
-
-        // Carry out the page mapping
-        paging_k1om_map(entry, src_lp + offset, flags);
-    }
-
-    return SYS_ERR_OK;
-}
-
-typedef errval_t (*mapping_handler_t)(struct capability *dest_cap,
-                                      cslot_t dest_slot,
-                                      struct capability *src_cap,
-                                      uintptr_t flags, uintptr_t offset,
-                                      size_t pte_count);
-
-/// Dispatcher table for the type of mapping to create
-static mapping_handler_t handler[ObjType_Num] = {
-    [ObjType_VNode_x86_64_pml4]   = x86_64_non_ptable,
-    [ObjType_VNode_x86_64_pdpt]   = x86_64_non_ptable,
-    [ObjType_VNode_x86_64_pdir]   = x86_64_non_ptable,
-    [ObjType_VNode_x86_64_ptable] = x86_64_ptable,
-};
-
-
-#define DIAGNOSTIC_ON_ERROR 1
-#define RETURN_ON_ERROR 1
-
-/// Create page mappings
-errval_t caps_copy_to_vnode(struct cte *dest_vnode_cte, cslot_t dest_slot,
-                            struct cte *src_cte, uintptr_t flags,
-                            uintptr_t offset, uintptr_t pte_count)
-{
-    assert(type_is_vnode(dest_vnode_cte->cap.type));
-
-    struct capability *src_cap  = &src_cte->cap;
-    struct capability *dest_cap = &dest_vnode_cte->cap;
-    mapping_handler_t handler_func = handler[dest_cap->type];
-
-    assert(handler_func != NULL);
-
-#if 0
-    genpaddr_t paddr = get_address(&src_cte->cap) + offset;
-    genvaddr_t vaddr;
-    compile_vaddr(dest_vnode_cte, dest_slot, &vaddr);
-    printf("mapping 0x%"PRIxGENPADDR" to 0x%"PRIxGENVADDR"\n", paddr, vaddr);
-#endif
-
-    if (src_cte->mapping_info.pte) {
-        // this cap is already mapped
-#if DIAGNOSTIC_ON_ERROR
-        printf("caps_copy_to_vnode: this copy is already mapped @pte 0x%lx (paddr = 0x%"PRIxGENPADDR")\n", src_cte->mapping_info.pte, get_address(src_cap));
-#endif
-#if RETURN_ON_ERROR
-        return SYS_ERR_VM_ALREADY_MAPPED;
-#endif
-    }
-
-    cslot_t last_slot = dest_slot + pte_count;
-
-    if (last_slot > X86_64_PTABLE_SIZE) {
-        // requested map overlaps leaf page table
-#if DIAGNOSTIC_ON_ERROR
-        printf("caps_copy_to_vnode: requested mapping spans multiple leaf page tables\n");
-#endif
-#if RETURN_ON_ERROR
-        return SYS_ERR_VM_RETRY_SINGLE;
-#endif
-    }
-
-    errval_t r = handler_func(dest_cap, dest_slot, src_cap, flags, offset, pte_count);
-    if (err_is_fail(r)) {
-        printf("caps_copy_to_vnode: handler func returned %ld\n", r);
-    }
-#if 0
-    else {
-        printf("mapping_info.pte       = 0x%lx\n", src_cte->mapping_info.pte);
-        printf("mapping_info.offset    = 0x%lx\n", src_cte->mapping_info.offset);
-        printf("mapping_info.pte_count = %zu\n", src_cte->mapping_info.pte_count);
-    }
-#endif
-    return r;
-}
-
-static inline void read_pt_entry(struct capability *pgtable, size_t slot,
-                                 genpaddr_t *mapped_addr, lpaddr_t *pte,
-                                 void **entry)
-{
-    assert(type_is_vnode(pgtable->type));
-
-    genpaddr_t paddr;
-    lpaddr_t pte_;
-    void *entry_;
-
-    genpaddr_t gp = get_address(pgtable);
-    lpaddr_t lp = gen_phys_to_local_phys(gp);
-    lvaddr_t lv = local_phys_to_mem(lp);
-
-    // get paddr
-    switch (pgtable->type) {
-    case ObjType_VNode_x86_64_pml4:
-    case ObjType_VNode_x86_64_pdpt:
-    case ObjType_VNode_x86_64_pdir: {
-        union x86_64_pdir_entry *e =
-            (union x86_64_pdir_entry *)lv + slot;
-        paddr = (lpaddr_t)e->d.base_addr << BASE_PAGE_BITS;
-        entry_ = e;
-        pte_ = lp + slot * sizeof(union x86_64_pdir_entry);
-        break;
-    }
-    case ObjType_VNode_x86_64_ptable: {
-        union x86_64_ptable_entry *e =
-            (union x86_64_ptable_entry *)lv + slot;
-        paddr = (lpaddr_t)e->base.base_addr << BASE_PAGE_BITS;
-        entry_ = e;
-        pte_ = lp + slot * sizeof(union x86_64_ptable_entry);
-        break;
-    }
-    default:
-        assert(!"Should not get here");
-    }
-
-    if (mapped_addr) {
-        *mapped_addr = paddr;
-    }
-    if (pte) {
-        *pte = pte_;
-    }
-    if (entry) {
-        *entry = entry_;
-    }
-}
-
-static inline void clear_pt_entry(lvaddr_t pte) {
-    ((union x86_64_pdir_entry *)pte)->raw = 0;
-}
-
-static inline lvaddr_t get_leaf_ptable_for_vaddr(genvaddr_t vaddr)
-{
-    lvaddr_t root_pt = local_phys_to_mem(dcb_current->vspace);
-
-    // get pdpt
-    union x86_64_pdir_entry *pdpt = (union x86_64_pdir_entry *)root_pt + X86_64_PML4_BASE(vaddr);
-    if (!pdpt->raw) { return 0; }
-    genpaddr_t pdpt_gp = pdpt->d.base_addr << BASE_PAGE_BITS;
-    lvaddr_t pdpt_lv = local_phys_to_mem(gen_phys_to_local_phys(pdpt_gp));
-    // get pdir
-    union x86_64_pdir_entry *pdir = (union x86_64_pdir_entry *)pdpt_lv + X86_64_PDPT_BASE(vaddr);
-    if (!pdir->raw) { return 0; }
-    genpaddr_t pdir_gp = pdir->d.base_addr << BASE_PAGE_BITS;
-    lvaddr_t pdir_lv = local_phys_to_mem(gen_phys_to_local_phys(pdir_gp));
-    // get ptable
-    union x86_64_ptable_entry *ptable = (union x86_64_ptable_entry *)pdir_lv + X86_64_PDIR_BASE(vaddr);
-    if (!ptable->raw) { return 0; }
-    genpaddr_t ptable_gp = ptable->base.base_addr << BASE_PAGE_BITS;
-    lvaddr_t ptable_lv = local_phys_to_mem(gen_phys_to_local_phys(ptable_gp));
-
-    return ptable_lv;
-}
-
-size_t do_unmap(lvaddr_t pt, cslot_t slot, size_t num_pages)
-{
-    // iterate over affected leaf ptables
-    size_t unmapped_pages = 0;
-    union x86_64_ptable_entry *ptentry = (union x86_64_ptable_entry *)pt + slot;
-    for (int i = 0; i < num_pages; i++) {
-        ptentry++->raw = 0;
-        unmapped_pages++;
-    }
-    return unmapped_pages;
-}
-
-errval_t page_mappings_unmap(struct capability *pgtable, struct cte *mapping,
-                             size_t slot, size_t num_pages)
-{
-    assert(type_is_vnode(pgtable->type));
-    errval_t err;
-    debug(SUBSYS_PAGING, "page_mappings_unmap(%zd pages)\n", num_pages);
-
-    // get page table entry data
-    genpaddr_t paddr;
-
-    read_pt_entry(pgtable, slot, &paddr, NULL, NULL);
-    lvaddr_t pt = local_phys_to_mem(gen_phys_to_local_phys(get_address(pgtable)));
-
-    // get virtual address of first page
-    // TODO: error checking
-    genvaddr_t vaddr;
-    struct cte *leaf_pt = cte_for_cap(pgtable);
-    err = compile_vaddr(leaf_pt, slot, &vaddr);
-    if (err_is_fail(err)) {
-        if (err_no(err) == SYS_ERR_VNODE_NOT_INSTALLED) {
-            debug(SUBSYS_PAGING, "couldn't reconstruct virtual address\n");
-        }
-        else {
-            return err;
-        }
-    }
-
-    if (num_pages != mapping->mapping_info.pte_count) {
-        // want to unmap a different amount of pages than was mapped
-        return SYS_ERR_VM_MAP_SIZE;
-    }
-
-    do_unmap(pt, slot, num_pages);
-
-    // flush TLB for unmapped pages if we got a valid virtual address
-    // TODO: heuristic that decides if selective or full flush is more
-    //       efficient?
-    if (num_pages > 1 && err_is_ok(err)) {
-        do_full_tlb_flush();
-    } else {
-        do_one_tlb_flush(vaddr);
-    }
-
-    // update mapping info
-    memset(&mapping->mapping_info, 0, sizeof(struct mapping_info));
-
-    return SYS_ERR_OK;
-}
-
-errval_t page_mappings_modify_flags(struct capability *frame, size_t offset,
-                                    size_t pages, size_t mflags)
-{
-    struct cte *mapping = cte_for_cap(frame);
-    struct mapping_info *info = &mapping->mapping_info;
-
-    /* Calculate page access protection flags */
-    // Get frame cap rights
-    paging_x86_64_flags_t flags =
-        paging_k1om_cap_to_page_flags(frame->rights);
-    // Mask with provided access rights mask
-    flags = paging_k1om_mask_attrs(flags, X86_64_PTABLE_ACCESS(mflags));
-    // Add additional arch-specific flags
-    flags |= X86_64_PTABLE_FLAGS(mflags);
-    // Unconditionally mark the page present
-    flags |= X86_64_PTABLE_PRESENT;
-
-    /* Calculate location of page table entries we need to modify */
-    lvaddr_t base = local_phys_to_mem(info->pte) + offset;
-
-    for (int i = 0; i < pages; i++) {
-        union x86_64_ptable_entry *entry =
-            (union x86_64_ptable_entry *)base + i;
-        paging_k1om_modify_flags(entry, flags);
-    }
-
-    /* flush affected TLB entries and return */
-    return paging_tlb_flush_range(mapping, pages);
-}
-
-void paging_dump_tables(struct dcb *dispatcher)
-{
-    lvaddr_t root_pt = local_phys_to_mem(dispatcher->vspace);
-
-    // loop over pdpts
-    for (int pdpt_index = 0; pdpt_index < X86_64_PTABLE_SIZE-1; pdpt_index++) {
-
-        union x86_64_pdir_entry *pdpt = (union x86_64_pdir_entry *)root_pt + pdpt_index;
-        if (!pdpt->raw) { continue; }
-        genpaddr_t pdpt_gp = pdpt->d.base_addr << BASE_PAGE_BITS;
-        lvaddr_t pdpt_lv = local_phys_to_mem(gen_phys_to_local_phys(pdpt_gp));
-
-        for (int pdir_index = 0; pdir_index < X86_64_PTABLE_SIZE; pdir_index++) {
-            // get pdir
-            union x86_64_pdir_entry *pdir = (union x86_64_pdir_entry *)pdpt_lv + pdir_index;
-            if (!pdir->raw) { continue; }
-            genpaddr_t pdir_gp = pdir->d.base_addr << BASE_PAGE_BITS;
-            lvaddr_t pdir_lv = local_phys_to_mem(gen_phys_to_local_phys(pdir_gp));
-
-            for (int ptable_index = 0; ptable_index < X86_64_PTABLE_SIZE; ptable_index++) {
-                // get ptable
-                union x86_64_ptable_entry *ptable = (union x86_64_ptable_entry *)pdir_lv + ptable_index;
-                if (!ptable->raw) { continue; }
-                genpaddr_t ptable_gp = ptable->base.base_addr << BASE_PAGE_BITS;
-                lvaddr_t ptable_lv = local_phys_to_mem(gen_phys_to_local_phys(ptable_gp));
-
-                for (int entry = 0; entry < X86_64_PTABLE_SIZE; entry++) {
-                    union x86_64_ptable_entry *e =
-                        (union x86_64_ptable_entry *)ptable_lv + entry;
-                    genpaddr_t paddr = (genpaddr_t)e->base.base_addr << BASE_PAGE_BITS;
-                    if (!paddr) {
-                        continue;
-                    }
-                    printf("%d.%d.%d.%d: 0x%"PRIxGENPADDR"\n", pdpt_index, pdir_index, ptable_index, entry, paddr);
-                }
-            }
-        }
-    }
-}
diff --git a/kernel/arch/k1om/paging.c b/kernel/arch/k1om/paging.c
deleted file mode 100644 (file)
index fb53962..0000000
+++ /dev/null
@@ -1,210 +0,0 @@
-/**
- * \file
- * \brief x86-64 kernel page-table setup
- */
-
-/*
- * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <kernel.h>
-#include <paging_kernel_arch.h>
-
-#include <xeon_phi.h>
-/*
- * Table requirements for various address spaces.
- */
-#define MEM_PDPT_SIZE           X86_64_PDPT_ENTRIES(K1OM_PADDR_SPACE_LIMIT)
-#define MEM_PDIR_SIZE           X86_64_PDIR_ENTRIES(K1OM_PADDR_SPACE_LIMIT)
-
-/*
- * Page attribute bitmaps for various address spaces.
- *
- * Note: The K1OM architecture does not understand the global bit
- */
-#define MEM_PAGE_BITMAP                                 \
-    (X86_64_PTABLE_PRESENT | X86_64_PTABLE_READ_WRITE | X86_64_PTABLE_USER_SUPERVISOR)
-#define DEVICE_PAGE_BITMAP                                      \
-    (X86_64_PTABLE_PRESENT | X86_64_PTABLE_READ_WRITE | X86_64_PTABLE_USER_SUPERVISOR |        \
-     X86_64_PTABLE_CACHE_DISABLED )
-
-/**
- * Kernel page map level 4 table.
- */
-static union x86_64_pdir_entry pml4[X86_64_PTABLE_SIZE] __attribute__((aligned(X86_64_BASE_PAGE_SIZE)));
-
-/**
- * Page directory pointer table for physical memory address space.
- */
-static union x86_64_pdir_entry mem_pdpt[MEM_PDPT_SIZE][X86_64_PTABLE_SIZE] __attribute__((aligned(X86_64_BASE_PAGE_SIZE)));
-
-/**
- * Page directory for physical memory address space.
- */
-static union x86_64_ptable_entry mem_pdir[MEM_PDPT_SIZE][MEM_PDIR_SIZE][X86_64_PTABLE_SIZE] __attribute__((aligned(X86_64_BASE_PAGE_SIZE)));
-
-static inline void
-mapit(union x86_64_pdir_entry *pml4_base,
-      union x86_64_pdir_entry *pdpt_base,
-      union x86_64_ptable_entry *pdir_base,
-      lpaddr_t addr,
-      uint64_t bitmap)
-{
-    if (!X86_64_IS_PRESENT(pml4_base)) {
-        paging_k1om_map_table(pml4_base, mem_to_local_phys((lvaddr_t) pdpt_base));
-    }
-
-    if (!X86_64_IS_PRESENT(pdpt_base)) {
-        paging_k1om_map_table(pdpt_base, mem_to_local_phys((lvaddr_t) pdir_base));
-    }
-
-    if (!X86_64_IS_PRESENT(pdir_base)) {
-        debug(SUBSYS_PAGING, "mapped!\n");
-        paging_k1om_map_large(pdir_base, addr, bitmap);
-    } else {
-//remap the page anyway, this is important for the memory latency benchmark
-        debug(SUBSYS_PAGING, "already existing! remapping");
-        paging_k1om_map_large(pdir_base, addr, bitmap);
-    }
-}
-
-/**
- * \brief Map a region of physical memory into physical memory address space.
- *
- * Maps the region of physical memory, based at base and sized size bytes
- * to the same-sized virtual memory region. All pages are flagged according to
- * bitmap. This function automatically fills the needed page directory entries
- * in the page hierarchy rooted at pml4. base and size will be made
- * page-aligned by this function.
- *
- * \param base          Physical base address of memory region
- * \param size          Size in bytes of memory region
- * \param bitmap        Bitmap of flags for page tables/directories
- *
- * \return 0 on success, -1 on error (out of range)
- */
-static int
-paging_map_mem(lpaddr_t base,
-               size_t size,
-               uint64_t bitmap)
-{
-    lvaddr_t vaddr, vbase = local_phys_to_mem(base);
-    lpaddr_t addr;
-
-    // Align given physical base address
-    if (base & X86_64_MEM_PAGE_MASK) {
-        base -= base & X86_64_MEM_PAGE_MASK;
-    }
-
-    paging_align(&vbase, &base, &size, X86_64_MEM_PAGE_SIZE);
-
-    // Is mapped region out of range?
-    assert(base + size <= (lpaddr_t)K1OM_PADDR_SPACE_LIMIT);
-    if (base + size > (lpaddr_t) K1OM_PADDR_SPACE_LIMIT) {
-        return -1;
-    }
-
-    // Map pages, tables and directories
-    for (vaddr = vbase, addr = base; vaddr < vbase + size; vaddr +=
-    X86_64_MEM_PAGE_SIZE, addr += X86_64_MEM_PAGE_SIZE) {
-        union x86_64_pdir_entry *pml4_base = &pml4[X86_64_PML4_BASE(vaddr)];
-
-        union x86_64_pdir_entry *pdpt_base =
-                &mem_pdpt[X86_64_PML4_BASE(addr)][X86_64_PDPT_BASE(vaddr)];
-
-        union x86_64_ptable_entry *pdir_base =
-                &mem_pdir[X86_64_PML4_BASE(addr)][X86_64_PDPT_BASE(addr)][X86_64_PDIR_BASE(
-                        vaddr)];
-
-        debug(SUBSYS_PAGING,
-              "Mapping 2M page: vaddr = 0x%"PRIxLVADDR"x, addr = 0x%lx, " "PML4_BASE = %lu, PDPT_BASE = %lu, PDIR_BASE = %lu -- ",
-              vaddr, addr, X86_64_PML4_BASE(vaddr), X86_64_PDPT_BASE(vaddr),
-              X86_64_PDIR_BASE(vaddr));
-
-        mapit(pml4_base, pdpt_base, pdir_base, addr, bitmap);
-    }
-    // XXX FIXME: get rid of this TBL flush code, or move it elsewhere
-    // uint64_t cr3;
-    // __asm__ __volatile__("mov %%cr3,%0" : "=a" (cr3) : );
-    // __asm__ __volatile__("mov %0,%%cr3" :  : "a" (cr3));
-
-    return 0;
-}
-
-lvaddr_t
-paging_k1om_map_device(lpaddr_t base,
-                       size_t size)
-{
-    if (paging_map_mem(base, size, DEVICE_PAGE_BITMAP) == 0) {
-        return local_phys_to_mem(base);
-    } else {
-        return 0;
-    }
-}
-
-int
-paging_k1om_map_memory(lpaddr_t base,
-                       size_t size)
-{
-    return paging_map_mem(base, size, MEM_PAGE_BITMAP);
-}
-
-/**
- * \brief Reset kernel paging.
- *
- * This function resets the page maps for kernel and memory-space. It clears out
- * all other mappings. Use this only at system bootup!
- */
-void
-paging_k1om_reset(void)
-{
-    // Map kernel image so we don't lose ground
-    if (paging_k1om_map_memory(mem_to_local_phys((lvaddr_t) &_start_kernel),
-                               SIZE_KERNEL_IMAGE)
-        != 0) {
-        panic("error while mapping physical memory!");
-    }
-
-    // Map an initial amount of memory
-    if (paging_k1om_map_memory(0, K1OM_KERNEL_INIT_MEMORY) != 0) {
-        panic("error while mapping physical memory!");
-    }
-
-    if (paging_k1om_map_memory(XEON_PHI_SBOX_BASE, XEON_PHI_SBOX_SIZE) != 0) {
-        panic("error while mapping physical memory!");
-    }
-
-    // Switch to new page layout
-    paging_k1om_context_switch(mem_to_local_phys((lvaddr_t) pml4));
-}
-
-/**
- * \brief Make a "good" PML4 table out of a page table.
- *
- * A "good" PML4 table is one that has all physical address space and
- * the kernel mapped in. This function modifies the passed PML4, based
- * at physical address 'base' accordingly. It does this by taking out
- * the corresponding entries of the kernel's pristine PML4 table.
- *
- * \param base  Physical base address of PML4 table to make "good".
- */
-void
-paging_k1om_make_good_pml4(lpaddr_t base)
-{
-    union x86_64_pdir_entry *newpml4 = (union x86_64_pdir_entry *) local_phys_to_mem(
-            base);
-    int i;
-
-    // XXX: Disabled till vaddr_t is figured out
-    debug(SUBSYS_PAGING, "Is now a PML4: table = 0x%"PRIxLPADDR"\n", base);
-
-    // Map memory
-    for (i = X86_64_PML4_BASE(K1OM_MEMORY_OFFSET); i < X86_64_PTABLE_SIZE; i++) {
-        newpml4[i] = pml4[i];
-    }
-}
index ef641f8..a923134 100644 (file)
@@ -31,6 +31,7 @@
 #include <kputchar.h>
 #include <startup.h>
 #include <arch/x86/startup_x86.h>
+#include <arch/x86/start_aps.h>
 
 #include <xeon_phi.h>
 #include <xeon_phi/xeon_phi.h>
 #define INIT_PAGE_BITMAP        X86_64_PTABLE_PRESENT
 
 /// Pointer to bootinfo structure for init
-static struct bootinfo *bootinfo = (struct bootinfo *) BOOTINFO_BASE;
+static struct bootinfo *bootinfo = (struct bootinfo *)BOOTINFO_BASE;
 
 static struct spawn_state spawn_state;
 
 /**
  * Page map level 4 table for init user address space.
  */
-static union x86_64_pdir_entry *init_pml4;  //[PTABLE_SIZE]
+static union x86_64_pdir_entry *init_pml4; //[PTABLE_SIZE]
 
 /**
  * Page directory pointer table for init user address space.
  */
-static union x86_64_pdir_entry *init_pdpt;  //[INIT_PDPT_SIZE][PTABLE_SIZE]
+static union x86_64_pdir_entry *init_pdpt; //[INIT_PDPT_SIZE][PTABLE_SIZE]
 
 /**
  * Page directory for init user address space.
  */
-static union x86_64_pdir_entry *init_pdir;  //[INIT_PDPT_SIZE][INIT_PDIR_SIZE][PTABLE_SIZE]
+static union x86_64_pdir_entry *init_pdir; //[INIT_PDPT_SIZE][INIT_PDIR_SIZE][PTABLE_SIZE]
 
 /**
  * Page tables for init user address space.
  */
-static union x86_64_ptable_entry *init_ptable;  //[INIT_PDPT_SIZE][INIT_PDIR_SIZE][INIT_PTABLE_SIZE][PTABLE_SIZE]
+static union x86_64_ptable_entry *init_ptable; //[INIT_PDPT_SIZE][INIT_PDIR_SIZE][INIT_PTABLE_SIZE][PTABLE_SIZE]
 
 /**
  * \brief Convert elf flags to page flags
@@ -106,9 +107,7 @@ static uint64_t paging_elf_to_page_flags(uint32_t flags)
  * \param size  Size of program segment in bytes.
  * \param flags ELF64 access control flags of program segment.
  */
-errval_t startup_map_init(lvaddr_t vbase,
-                          lpaddr_t base,
-                          size_t size,
+errval_t startup_map_init(lvaddr_t vbase, lpaddr_t base, size_t size,
                           uint32_t flags)
 {
     lvaddr_t vaddr;
@@ -118,32 +117,26 @@ errval_t startup_map_init(lvaddr_t vbase,
     assert(vbase + size - K1OM_INIT_VBASE < K1OM_INIT_SPACE_LIMIT);
 
     // Map pages
-    for (vaddr = vbase; vaddr < vbase + size; vaddr += BASE_PAGE_SIZE, base +=
-                    BASE_PAGE_SIZE) {
+    for(vaddr = vbase; vaddr < vbase + size;
+        vaddr += BASE_PAGE_SIZE, base += BASE_PAGE_SIZE) {
         lvaddr_t baddr = vaddr - K1OM_INIT_VBASE;
-        union x86_64_ptable_entry *ptable_base =
-                        &init_ptable[X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE
-                                     * X86_64_PTABLE_SIZE * X86_64_PTABLE_SIZE
-                                     + X86_64_PDPT_BASE(baddr) * X86_64_PTABLE_SIZE
-                                       * X86_64_PTABLE_SIZE
-                                     + X86_64_PDIR_BASE(baddr) * X86_64_PTABLE_SIZE
-                                     + X86_64_PTABLE_BASE(vaddr)];
-
-        debug(SUBSYS_PAGING,
-              "Mapping 4K page: vaddr = 0x%lx, base = 0x%lx, "
+        union x86_64_ptable_entry *ptable_base = &init_ptable[
+                    X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE *
+                    X86_64_PTABLE_SIZE * X86_64_PTABLE_SIZE +
+                    X86_64_PDPT_BASE(baddr) * X86_64_PTABLE_SIZE *
+                    X86_64_PTABLE_SIZE + X86_64_PDIR_BASE(baddr) *
+                    X86_64_PTABLE_SIZE + X86_64_PTABLE_BASE(vaddr)];
+
+        debug(SUBSYS_PAGING, "Mapping 4K page: vaddr = 0x%lx, base = 0x%lx, "
               "PML4_BASE = %lu, PDPT_BASE = %lu, PDIR_BASE = %lu, "
-              "PTABLE_BASE = %lu -- ",
-              vaddr,
-              base,
-              X86_64_PML4_BASE(baddr),
-              X86_64_PDPT_BASE(baddr),
-              X86_64_PDIR_BASE(baddr),
+              "PTABLE_BASE = %lu -- ", vaddr, base, X86_64_PML4_BASE(baddr),
+              X86_64_PDPT_BASE(baddr), X86_64_PDIR_BASE(baddr),
               X86_64_PTABLE_BASE(baddr));
 
-        if (!X86_64_IS_PRESENT(ptable_base)) {
+        if(!X86_64_IS_PRESENT(ptable_base)) {
             debug(SUBSYS_PAGING, "mapped!\n");
-            paging_k1om_map(ptable_base, base,
-            INIT_PAGE_BITMAP | paging_elf_to_page_flags(flags));
+            paging_x86_64_map(ptable_base, base,
+                              INIT_PAGE_BITMAP | paging_elf_to_page_flags(flags));
         } else {
             debug(SUBSYS_PAGING, "already existing!\n");
         }
@@ -158,278 +151,33 @@ static void create_phys_caps(lpaddr_t init_alloc_addr)
     errval_t err;
 
     // map first meg of RAM, which contains lots of crazy BIOS tables
-    err = create_caps_to_cnode(0,
-                               K1OM_START_KERNEL_PHYS,
-                               RegionType_PlatformData,
-                               &spawn_state,
-                               bootinfo);
+    err = create_caps_to_cnode(0, K1OM_START_KERNEL_PHYS, 
+                               RegionType_PlatformData, &spawn_state, bootinfo);
     assert(err_is_ok(err));
 
     char *mmap_addr = MBADDR_ASSTRING(glbl_core_data->mmap_addr);
     lpaddr_t last_end_addr = 0;
 
-#define PRINT_REGIONS(map, map_length) do {\
-        for(char * printcur = map; printcur < map + map_length;) {\
-            struct multiboot_mmap * printcurmmap = (struct multiboot_mmap * SAFE)TC(printcur);\
-            printf("\t0x%08lx - 0x%08lx Type: %d Length: 0x%lx\n", printcurmmap->base_addr, printcurmmap->base_addr + printcurmmap->length, printcurmmap->type, printcurmmap->length);\
-            printcur += printcurmmap->size;\
-        }\
-    } while (0)
-
-    printf("Raw MMAP from BIOS\n");
-    PRINT_REGIONS(mmap_addr, glbl_core_data->mmap_length);
-
-    // normalize memory regions
-char *clean_mmap_addr = MBADDR_ASSTRING(init_alloc_addr); // FIXME: Hack!!!! TODO: properly get memory form somewhere
-        assert(glbl_core_data->mmap_length < BOOTINFO_SIZE);
-    uint32_t clean_mmap_length = glbl_core_data->mmap_length;
-
-    memcpy(clean_mmap_addr, mmap_addr, glbl_core_data->mmap_length);
-
-    // first of all, sort regions by base address
-    // yes, it's a bubble sort, but the dataset is small and usually in the right order
-    bool swapped;
-    do {
-        swapped = false;
-
-        for (char * cur = clean_mmap_addr; cur < clean_mmap_addr + clean_mmap_length;
-                        ) {
-            struct multiboot_mmap * curmmap = (struct multiboot_mmap * SAFE)TC(cur);
-            if (cur + curmmap->size >= clean_mmap_addr + clean_mmap_length)
-                break;  // do not try to move this check into the forloop as entries do not have to be the same length
-
-            struct multiboot_mmap * nextmmap = (struct multiboot_mmap * SAFE)TC(cur
-                            + curmmap->size);
-
-            if (nextmmap->base_addr < curmmap->base_addr || (nextmmap->base_addr
-                            == curmmap->base_addr
-                                                             && nextmmap->length > curmmap
-                                                                             ->length)) {
-                // swap
-                assert(curmmap->size == 20);  // FIXME: The multiboot specification does not require this size
-                assert(nextmmap->size == 20);
-
-                struct multiboot_mmap tmp;
-                tmp = *curmmap;
-                *curmmap = *nextmmap;
-                *nextmmap = tmp;
-
-                swapped = true;
-            }
-
-            cur += curmmap->size + 4;
-        }
-    } while (swapped);
-
-    printf("Sorted MMAP\n");
-    PRINT_REGIONS(clean_mmap_addr, clean_mmap_length);
-
-#if 0
-    // now merge consecutive memory regions of the same or lower type
-    for(char * cur = clean_mmap_addr; cur < clean_mmap_addr + clean_mmap_length;) {
-        struct multiboot_mmap * curmmap = (struct multiboot_mmap * SAFE)TC(cur);
-        if (cur + curmmap->size + 4 >= clean_mmap_addr + clean_mmap_length)
-        break;  // do not try to move this check into the forloop as entries do not have to be the same length
-
-        struct multiboot_mmap * nextmmap = (struct multiboot_mmap * SAFE)TC(cur + curmmap->size);
-
-        /* On some machines (brie1) the IOAPIC region is only 1kB.
-         * Currently we're not able to map regions that are <4kB so we
-         * make sure that every region (if there is no problematic overlap)
-         * is at least BASE_PAGE_SIZEd (==4kB) here.
-         */
-        if ((curmmap->length < BASE_PAGE_SIZE) && (curmmap->base_addr + BASE_PAGE_SIZE <= nextmmap->base_addr)) {
-            curmmap->length = BASE_PAGE_SIZE;
-        }
-
-#define DISCARD_NEXT_MMAP do {\
-    uint32_t discardsize = nextmmap->size + 4;\
-    memmove(cur + curmmap->size + 4, cur + curmmap->size + 4 + discardsize, clean_mmap_length - (cur - clean_mmap_addr) - curmmap->size - 4 - discardsize);\
-    clean_mmap_length -= discardsize;\
-    } while (0)
-
-#define BUBBLE_NEXT_MMAP do {\
-    for (char * bubblecur = cur + curmmap->size + 4; bubblecur < clean_mmap_addr + clean_mmap_length;){\
-        struct multiboot_mmap * bubblecur_mmap = (struct multiboot_mmap * SAFE)TC(bubblecur);\
-        if (bubblecur + bubblecur_mmap->size + 4 >= clean_mmap_addr + clean_mmap_length)\
-            break;\
-        struct multiboot_mmap * bubblenext_mmap = (struct multiboot_mmap * SAFE)TC(bubblecur + bubblecur_mmap->size + 4);\
-        if (bubblenext_mmap->base_addr < bubblecur_mmap->base_addr || (bubblecur_mmap->base_addr == bubblenext_mmap->base_addr && bubblenext_mmap->length > bubblecur_mmap->length)) {\
-            struct multiboot_mmap bubbletmp; bubbletmp = *bubblecur_mmap; *bubblecur_mmap = *bubblenext_mmap; *bubblenext_mmap = bubbletmp;\
-        } else break;\
-    }} while(0)
-
-        bool reduced = false;
-        do {
-            reduced = false;
-
-            if (curmmap->base_addr == nextmmap->base_addr) {
-                // regions start at the same location
-                if (curmmap->length == nextmmap->length) {
-                    // trivial case. They are the same. Choose higher type and discard next
-                    curmmap->type = max(curmmap->type, nextmmap->type);
-
-                    DISCARD_NEXT_MMAP;
-
-                    reduced = true;
-                    continue;
-                } else {
-                    // next region is smaller (we sorted that way)
-                    if (nextmmap->type <= curmmap->type) {
-                        // next regions type is the same or smaller. discard
-                        DISCARD_NEXT_MMAP;
-
-                        reduced = true;
-                        continue;
-                    } else {
-                        // next regions type is higher, so it gets priority
-                        // change type of current region and shrink next
-                        uint32_t tmptype = curmmap->type;
-                        uint64_t newlength = curmmap->length - nextmmap->length;
-                        curmmap->type = nextmmap->type;
-                        curmmap->length = nextmmap->length;
-                        nextmmap->type = tmptype;
-                        nextmmap->base_addr += nextmmap->length;
-                        nextmmap->length = newlength;
-
-                        // now we need to bubble next to the right place to restore order
-                        BUBBLE_NEXT_MMAP;
-
-                        reduced = true;
-                        continue;
-                    }
-                }
-            }
+    char *clean_mmap_addr;
+    uint32_t clean_mmap_length;
+    cleanup_bios_regions(mmap_addr, &clean_mmap_addr, &clean_mmap_length);
 
-            // regions overlap
-            if (nextmmap->base_addr > curmmap->base_addr && nextmmap->base_addr < curmmap->base_addr + curmmap->length) {
-                // same type
-                if (curmmap->type == nextmmap->type) {
-                    // simple. just extend if necessary and discard next
-                    if (nextmmap->base_addr + nextmmap->length > curmmap->base_addr + curmmap->length)
-                    curmmap->length = (nextmmap->base_addr + nextmmap->length) - curmmap->base_addr;
-
-                    DISCARD_NEXT_MMAP;
-
-                    reduced = true;
-                    continue;
-                } else {
-                    // type is not the same
-                    if (nextmmap->base_addr + nextmmap->length < curmmap->base_addr + curmmap->length) {
-                        // there is a chunk at the end. create a new region
-                        struct multiboot_mmap tmpmmap;
-                        tmpmmap.size = 20;
-                        tmpmmap.base_addr = nextmmap->base_addr + nextmmap->length;
-                        tmpmmap.length = (curmmap->base_addr + curmmap->length) - (nextmmap->base_addr + nextmmap->length);
-                        tmpmmap.type = curmmap->type;
-
-                        // move everything to make room
-                        assert(clean_mmap_length + tmpmmap.length + 4 < BOOTINFO_SIZE);
-                        memmove(cur + curmmap->size + 4 + tmpmmap.size + 4, cur + curmmap->size + 4, clean_mmap_length - ((cur - clean_mmap_addr) + curmmap->size + 4));
-                        clean_mmap_length += tmpmmap.size + 4;
-
-                        // insert new
-                        *nextmmap = tmpmmap;
-
-                        // restore order
-                        BUBBLE_NEXT_MMAP;
-
-                        reduced = true;
-                    }
-
-                    // after the previous step, the next region either ends
-                    // at the same location as the current or is longer
-                    uint64_t overlap = (curmmap->base_addr + curmmap->length) - nextmmap->base_addr;
-
-                    if (curmmap-> type > nextmmap->type) {
-                        // current has priority, shrink next and extend current
-                        nextmmap->length -= overlap;
-                        nextmmap->base_addr += overlap;
-                        curmmap->length += overlap;
-
-                        if (nextmmap->length == 0)
-                        DISCARD_NEXT_MMAP;
-
-                        reduced = true;
-                        continue;
-                    } else {
-                        // next has priority, shrink current and extend next
-                        nextmmap->length += overlap;
-                        nextmmap->base_addr -= overlap;
-                        curmmap->length -= overlap;
-
-                        reduced = true;
-                        continue;
-                    }
-                }
-            }
-        }while (reduced);
-
-        cur += curmmap->size;
-
-#undef DISCARD_NEXT_MMAP
-#undef BUBBLE_NEXT_MMAP
-    }
-#endif
-    printf("Preprocessed MMAP\n");
-    PRINT_REGIONS(clean_mmap_addr, clean_mmap_length);
-
-    // we can only map pages. Therefore page align regions
-    for (char * cur = clean_mmap_addr; cur < clean_mmap_addr + clean_mmap_length;) {
-        struct multiboot_mmap * curmmap = (struct multiboot_mmap * SAFE)TC(cur);
-        if (cur + curmmap->size + 4 >= clean_mmap_addr + clean_mmap_length)
-            break;  // do not try to move this check into the forloop as entries do not have to be the same length
-
-        struct multiboot_mmap * nextmmap = (struct multiboot_mmap * SAFE)TC(cur
-                        + curmmap->size);
-
-        if (nextmmap->base_addr & BASE_PAGE_MASK) {
-            uint64_t offset =
-                            nextmmap->base_addr - ((nextmmap->base_addr
-                                            >> BASE_PAGE_BITS)
-                                                   << BASE_PAGE_BITS);
-
-            // round in favour of higher type
-            if (curmmap->type > nextmmap->type) {
-                curmmap->length += BASE_PAGE_SIZE - offset;
-                nextmmap->base_addr += BASE_PAGE_SIZE - offset;
-                nextmmap->length -= BASE_PAGE_SIZE - offset;
-            } else {
-                curmmap->length -= offset;
-                nextmmap->base_addr -= offset;
-                nextmmap->length += offset;
-            }
-        }
-
-        cur += curmmap->size;
-    }
-
-    printf("Pagealigned MMAP\n");
-    PRINT_REGIONS(clean_mmap_addr, clean_mmap_length);
-
-#undef PRINT_REGIONS
-
-    for (char *m = clean_mmap_addr; m < clean_mmap_addr + clean_mmap_length;) {
+    for(char *m = clean_mmap_addr; m < clean_mmap_addr + clean_mmap_length;) {
         struct multiboot_mmap *mmap = (struct multiboot_mmap * SAFE)TC(m);
 
-        debug(SUBSYS_STARTUP,
-              "MMAP %lx--%lx Type %u\n",
-              mmap->base_addr,
-              mmap->base_addr + mmap->length,
+        debug(SUBSYS_STARTUP, "MMAP %lx--%lx Type %u\n",
+              mmap->base_addr, mmap->base_addr + mmap->length,
               mmap->type);
 
-        if (last_end_addr >= init_alloc_addr && mmap->base_addr > last_end_addr) {
+        if (last_end_addr >= init_alloc_addr
+            && mmap->base_addr > last_end_addr) {
             /* we have a gap between regions. add this as a physaddr range */
-            debug(SUBSYS_STARTUP,
-                  "physical address range %lx--%lx\n",
-                  last_end_addr,
-                  mmap->base_addr);
+            debug(SUBSYS_STARTUP, "physical address range %lx--%lx\n",
+                  last_end_addr, mmap->base_addr);
 
             err = create_caps_to_cnode(last_end_addr,
                                        mmap->base_addr - last_end_addr,
-                                       RegionType_PhyAddr,
-                                       &spawn_state,
-                                       bootinfo);
+                                       RegionType_PhyAddr, &spawn_state, bootinfo);
             assert(err_is_ok(err));
         }
 
@@ -443,11 +191,11 @@ char *clean_mmap_addr = MBADDR_ASSTRING(init_alloc_addr); // FIXME: Hack!!!! TOD
                     base_addr = local_phys_to_gen_phys(init_alloc_addr);
                 }
                 debug(SUBSYS_STARTUP, "RAM %lx--%lx\n", base_addr, end_addr);
-                err = create_caps_to_cnode(base_addr,
-                                           end_addr - base_addr,
-                                           RegionType_Empty,
-                                           &spawn_state,
-                                           bootinfo);
+                err = create_caps_to_cnode(base_addr, end_addr - base_addr,
+                                           RegionType_Empty, &spawn_state, bootinfo);
+                if (err_no(err) == SYS_ERR_SLOTS_IN_USE) {
+                    printk(LOG_WARN, "not able to create RAM caps for all physical memory in the system, CNode full\n");
+                }
                 assert(err_is_ok(err));
             }
         } else if (mmap->base_addr > local_phys_to_gen_phys(init_alloc_addr)) {
@@ -456,16 +204,11 @@ char *clean_mmap_addr = MBADDR_ASSTRING(init_alloc_addr); // FIXME: Hack!!!! TOD
              * 3, and things like the IOAPIC tend to show up as type 2 or 4,
              * so we map all these regions as platform data
              */
-            debug(SUBSYS_STARTUP,
-                  "platform %lx--%lx\n",
-                  mmap->base_addr,
+            debug(SUBSYS_STARTUP, "platform %lx--%lx\n", mmap->base_addr,
                   mmap->base_addr + mmap->length);
             assert(mmap->base_addr > local_phys_to_gen_phys(init_alloc_addr));
-            err = create_caps_to_cnode(mmap->base_addr,
-                                       mmap->length,
-                                       RegionType_PlatformData,
-                                       &spawn_state,
-                                       bootinfo);
+            err = create_caps_to_cnode(mmap->base_addr, mmap->length,
+                                       RegionType_PlatformData, &spawn_state, bootinfo);
             assert(err_is_ok(err));
         }
 
@@ -474,31 +217,6 @@ char *clean_mmap_addr = MBADDR_ASSTRING(init_alloc_addr); // FIXME: Hack!!!! TOD
     }
 
     assert(last_end_addr != 0);
-
-    if (last_end_addr < K1OM_PADDR_SPACE_SIZE) {
-        /*
-         * FIXME: adding the full range results in too many caps to add
-         * to the cnode (and we can't handle such big caps in user-space
-         * yet anyway) so instead we limit it to something much smaller
-         */
-        size_t size = K1OM_PADDR_SPACE_SIZE - last_end_addr;
-        const lpaddr_t phys_region_limit = 1UL << 32;  // PCI implementation limit
-        if (last_end_addr > phys_region_limit) {
-            size = 0;  // end of RAM is already too high!
-        } else if (last_end_addr + size > phys_region_limit) {
-            size = phys_region_limit - last_end_addr;
-        }
-        debug(SUBSYS_STARTUP,
-              "end physical address range %lx--%lx\n",
-              last_end_addr,
-              last_end_addr + size);
-        err = create_caps_to_cnode(last_end_addr,
-                                   size,
-                                   RegionType_PhyAddr,
-                                   &spawn_state,
-                                   bootinfo);
-        assert(err_is_ok(err));
-    }
 }
 
 #define NEEDED_KERNEL_SPACE \
@@ -508,105 +226,96 @@ char *clean_mmap_addr = MBADDR_ASSTRING(init_alloc_addr); // FIXME: Hack!!!! TOD
 
 #define OBJSPERPAGE_CTE         (1UL << (BASE_PAGE_BITS - OBJBITS_CTE))
 
-static void init_page_tables(struct spawn_state *st,
-                             alloc_phys_func alloc_phys)
+static void init_page_tables(struct spawn_state *st, alloc_phys_func alloc_phys)
 {
     /* Allocate memory for init's page tables */
-    init_pml4 = (void *) local_phys_to_mem(alloc_phys(X86_64_PTABLE_SIZE
-                    * sizeof(union x86_64_pdir_entry)));
-    init_pdpt = (void *) local_phys_to_mem(alloc_phys(X86_64_PTABLE_SIZE
-                    * INIT_PDPT_SIZE * sizeof(union x86_64_pdir_entry)));
-    init_pdir = (void *) local_phys_to_mem(alloc_phys(X86_64_PTABLE_SIZE
-                    * INIT_PDPT_SIZE * INIT_PDIR_SIZE
-                    * sizeof(union x86_64_pdir_entry)));
-    init_ptable = (void *) local_phys_to_mem(alloc_phys(X86_64_PTABLE_SIZE
-                    * INIT_PDPT_SIZE * INIT_PDIR_SIZE * INIT_PTABLE_SIZE
-                    * sizeof(union x86_64_ptable_entry)));
+    init_pml4 = (void *)local_phys_to_mem(
+                alloc_phys(X86_64_PTABLE_SIZE * sizeof(union x86_64_pdir_entry)));
+    init_pdpt = (void *)local_phys_to_mem(
+                alloc_phys(X86_64_PTABLE_SIZE * INIT_PDPT_SIZE
+                           * sizeof(union x86_64_pdir_entry)));
+    init_pdir = (void *)local_phys_to_mem(
+                alloc_phys(X86_64_PTABLE_SIZE * INIT_PDPT_SIZE * INIT_PDIR_SIZE
+                           * sizeof(union x86_64_pdir_entry)));
+    init_ptable = (void *)local_phys_to_mem(
+                alloc_phys(X86_64_PTABLE_SIZE * INIT_PDPT_SIZE * INIT_PDIR_SIZE
+                           * INIT_PTABLE_SIZE * sizeof(union x86_64_ptable_entry)));
 
     /* Page table setup */
     /* Initialize init page tables */
-    for (size_t i = 0; i < INIT_PDPT_SIZE; i++) {
-        paging_k1om_clear_pdir(&init_pdpt[i]);
-        for (size_t j = 0; j < INIT_PDIR_SIZE; j++) {
-            paging_k1om_clear_pdir(&init_pdir[i * PTABLE_SIZE + j]);
-            for (size_t k = 0; k < INIT_PTABLE_SIZE; k++) {
-                paging_k1om_clear_ptable(&init_ptable[i * PTABLE_SIZE * PTABLE_SIZE
-                                + j * PTABLE_SIZE + k]);
+    for(size_t i = 0; i < INIT_PDPT_SIZE; i++) {
+        paging_x86_64_clear_pdir(&init_pdpt[i]);
+        for(size_t j = 0; j < INIT_PDIR_SIZE; j++) {
+            paging_x86_64_clear_pdir(&init_pdir[i * PTABLE_SIZE + j]);
+            for(size_t k = 0; k < INIT_PTABLE_SIZE; k++) {
+                paging_x86_64_clear_ptable(
+                &init_ptable[i * PTABLE_SIZE * PTABLE_SIZE + j * PTABLE_SIZE + k]);
             }
         }
     }
     /* Map pagetables into pageCN */
-    int pagecn_pagemap = 0;
+    int     pagecn_pagemap = 0;
     // Map PML4 (slot 0 in pagecn)
-    caps_create_new(ObjType_VNode_x86_64_pml4,
-                    mem_to_local_phys((lvaddr_t) init_pml4),
-                    BASE_PAGE_BITS,
-                    0,
+    caps_create_new(ObjType_VNode_x86_64_pml4, mem_to_local_phys((lvaddr_t)init_pml4),
+                    BASE_PAGE_BITS, 0,
                     caps_locate_slot(CNODE(st->pagecn), pagecn_pagemap++));
     // Map PDPT into successive slots in pagecn
-    for (size_t i = 0; i < INIT_PDPT_SIZE; i++) {
+    for(size_t i = 0; i < INIT_PDPT_SIZE; i++) {
         caps_create_new(ObjType_VNode_x86_64_pdpt,
-                        mem_to_local_phys((lvaddr_t) init_pdpt) + i * BASE_PAGE_SIZE,
-                        BASE_PAGE_BITS,
-                        0,
+                        mem_to_local_phys((lvaddr_t)init_pdpt) + i * BASE_PAGE_SIZE,
+                        BASE_PAGE_BITS, 0,
                         caps_locate_slot(CNODE(st->pagecn), pagecn_pagemap++));
     }
     // Map PDIR into successive slots in pagecn
-    for (size_t i = 0; i < INIT_PDIR_SIZE; i++) {
+    for(size_t i = 0; i < INIT_PDIR_SIZE; i++) {
         caps_create_new(ObjType_VNode_x86_64_pdir,
-                        mem_to_local_phys((lvaddr_t) init_pdir) + i * BASE_PAGE_SIZE,
-                        BASE_PAGE_BITS,
-                        0,
+                        mem_to_local_phys((lvaddr_t)init_pdir) + i * BASE_PAGE_SIZE,
+                        BASE_PAGE_BITS, 0,
                         caps_locate_slot(CNODE(st->pagecn), pagecn_pagemap++));
     }
     // Map page tables into successive slots in pagecn
-    for (size_t i = 0; i < INIT_PTABLE_SIZE; i++) {
+    for(size_t i = 0; i < INIT_PTABLE_SIZE; i++) {
         caps_create_new(ObjType_VNode_x86_64_ptable,
-                        mem_to_local_phys((lvaddr_t) init_ptable) + i
-                                        * BASE_PAGE_SIZE,
-                        BASE_PAGE_BITS,
-                        0,
+                        mem_to_local_phys((lvaddr_t)init_ptable) + i * BASE_PAGE_SIZE,
+                        BASE_PAGE_BITS, 0,
                         caps_locate_slot(CNODE(st->pagecn), pagecn_pagemap++));
     }
     // Connect all page tables to page directories.
     // init's memory manager expects page tables within the pagecn to
     // already be connected to the corresponding directories. To avoid
     // unneccessary special cases, we connect them here.
-    for (lvaddr_t vaddr = K1OM_INIT_VBASE; vaddr < K1OM_INIT_SPACE_LIMIT; vaddr +=
-                    BASE_PAGE_SIZE) {
+    for(lvaddr_t vaddr = K1OM_INIT_VBASE; vaddr < K1OM_INIT_SPACE_LIMIT;
+        vaddr += BASE_PAGE_SIZE) {
         lvaddr_t baddr = vaddr - K1OM_INIT_VBASE;
         union x86_64_pdir_entry *pml4_base, *pdpt_base, *pdir_base;
         union x86_64_ptable_entry *ptable_base;
         pml4_base = &init_pml4[X86_64_PML4_BASE(vaddr)];
-        pdpt_base = &init_pdpt[X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE
-                        + X86_64_PDPT_BASE(vaddr)];
-        pdir_base = &init_pdir[X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE
-                               * X86_64_PTABLE_SIZE
+        pdpt_base = &init_pdpt[X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE +
+                               X86_64_PDPT_BASE(vaddr)];
+        pdir_base = &init_pdir[X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE *
+                               X86_64_PTABLE_SIZE
                                + X86_64_PDPT_BASE(baddr) * X86_64_PTABLE_SIZE
                                + X86_64_PDIR_BASE(vaddr)];
-        ptable_base = &init_ptable[X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE
-                                   * X86_64_PTABLE_SIZE * X86_64_PTABLE_SIZE
-                                   + X86_64_PDPT_BASE(baddr) * X86_64_PTABLE_SIZE
-                                     * X86_64_PTABLE_SIZE
-                                   + X86_64_PDIR_BASE(baddr) * X86_64_PTABLE_SIZE
-                                   + X86_64_PTABLE_BASE(vaddr)];
-
-        paging_k1om_map_table(pml4_base, mem_to_local_phys((lvaddr_t) pdpt_base));
-        paging_k1om_map_table(pdpt_base, mem_to_local_phys((lvaddr_t) pdir_base));
-        paging_k1om_map_table(pdir_base, mem_to_local_phys((lvaddr_t) ptable_base));
+        ptable_base = &init_ptable[X86_64_PML4_BASE(baddr) * X86_64_PTABLE_SIZE *
+                                   X86_64_PTABLE_SIZE * X86_64_PTABLE_SIZE +
+                                   X86_64_PDPT_BASE(baddr) * X86_64_PTABLE_SIZE *
+                                   X86_64_PTABLE_SIZE + X86_64_PDIR_BASE(baddr) *
+                                   X86_64_PTABLE_SIZE + X86_64_PTABLE_BASE(vaddr)];
+
+        paging_x86_64_map_table(pml4_base, mem_to_local_phys((lvaddr_t)pdpt_base));
+        paging_x86_64_map_table(pdpt_base, mem_to_local_phys((lvaddr_t)pdir_base));
+        paging_x86_64_map_table(pdir_base, mem_to_local_phys((lvaddr_t)ptable_base));
     }
 
     /* Initialize and switch to init's PML4 */
-    paging_k1om_make_good_pml4(mem_to_local_phys((lvaddr_t) init_pml4));
-    paging_k1om_context_switch(mem_to_local_phys((lvaddr_t) init_pml4));
+    paging_x86_64_make_good_pml4(mem_to_local_phys((lvaddr_t)init_pml4));
+    paging_x86_64_context_switch(mem_to_local_phys((lvaddr_t)init_pml4));
 
     /***** VSpace available now *****/
 }
 
-static struct dcb *spawn_init_common(struct spawn_state *st,
-                                     const char *name,
-                                     int argc,
-                                     const char *argv[],
+static struct dcb *spawn_init_common(struct spawn_state *st, const char *name,
+                                     int argc, const char *argv[],
                                      lpaddr_t bootinfo_phys,
                                      alloc_phys_func alloc_phys)
 {
@@ -615,68 +324,63 @@ static struct dcb *spawn_init_common(struct spawn_state *st,
     /* Perform arch-independent spawn */
     lvaddr_t paramaddr;
     struct dcb *init_dcb = spawn_module(st, name, argc, argv, bootinfo_phys,
-    ARGS_BASE,
-                                        alloc_phys, &paramaddr);
+                                        ARGS_BASE, alloc_phys, &paramaddr);
 
     /* Init page tables */
     init_page_tables(st, alloc_phys);
 
     /* Map cmdline args R/W into VSpace at ARGS_BASE */
-    paging_k1om_map_table(&init_pml4[X86_64_PML4_BASE(ARGS_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_pdpt));
-    paging_k1om_map_table(&init_pdpt[X86_64_PDPT_BASE(ARGS_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_pdir));
-    paging_k1om_map_table(&init_pdir[X86_64_PDIR_BASE(ARGS_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_ptable));
+    paging_x86_64_map_table(&init_pml4[X86_64_PML4_BASE(ARGS_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_pdpt));
+    paging_x86_64_map_table(&init_pdpt[X86_64_PDPT_BASE(ARGS_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_pdir));
+    paging_x86_64_map_table(&init_pdir[X86_64_PDIR_BASE(ARGS_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_ptable));
     for (int i = 0; i < ARGS_SIZE / BASE_PAGE_SIZE; i++) {
-        paging_k1om_map(&init_ptable[X86_64_PTABLE_BASE(ARGS_BASE) + i],
-                        st->args_page + i * BASE_PAGE_SIZE,
-                        INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R | PF_W));
+        paging_x86_64_map(&init_ptable[X86_64_PTABLE_BASE(ARGS_BASE) + i],
+                          st->args_page + i * BASE_PAGE_SIZE,
+                          INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R|PF_W));
     }
 
     /* Map dispatcher frame R/W into VSpace */
-    paging_k1om_map_table(&init_pml4[X86_64_PML4_BASE(DISPATCHER_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_pdpt));
-    paging_k1om_map_table(&init_pdpt[X86_64_PDPT_BASE(DISPATCHER_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_pdir));
-    paging_k1om_map_table(&init_pdir[X86_64_PDIR_BASE(DISPATCHER_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_ptable));
+    paging_x86_64_map_table(&init_pml4[X86_64_PML4_BASE(DISPATCHER_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_pdpt));
+    paging_x86_64_map_table(&init_pdpt[X86_64_PDPT_BASE(DISPATCHER_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_pdir));
+    paging_x86_64_map_table(&init_pdir[X86_64_PDIR_BASE(DISPATCHER_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_ptable));
     for (int i = 0; i < (1 << DISPATCHER_FRAME_BITS) / BASE_PAGE_SIZE; i++) {
-        paging_k1om_map(&init_ptable[X86_64_PTABLE_BASE(DISPATCHER_BASE) + i],
-                        mem_to_local_phys(init_dcb->disp) + i * BASE_PAGE_SIZE,
-                        INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R | PF_W));
+        paging_x86_64_map(&init_ptable[X86_64_PTABLE_BASE(DISPATCHER_BASE) + i],
+                          mem_to_local_phys(init_dcb->disp) + i * BASE_PAGE_SIZE,
+                          INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R|PF_W));
     }
 
     struct dispatcher_shared_generic *init_disp =
-                    get_dispatcher_shared_generic(init_dcb->disp);
+        get_dispatcher_shared_generic(init_dcb->disp);
     struct dispatcher_shared_x86_64 *init_disp_x86_64 =
-                    get_dispatcher_shared_x86_64(init_dcb->disp);
+        get_dispatcher_shared_x86_64(init_dcb->disp);
 
     registers_set_param(&init_disp_x86_64->enabled_save_area, paramaddr);
 
     // Map IO cap in task cnode
     struct cte *iocap = caps_locate_slot(CNODE(st->taskcn), TASKCN_SLOT_SYSMEM);
-    err = caps_create_new(ObjType_DevFrame,
-                          XEON_PHI_SYSMEM_BASE,
-                          XEON_PHI_SYSMEM_SIZE_BITS,
-                          XEON_PHI_SYSMEM_SIZE_BITS,
+    err = caps_create_new(ObjType_DevFrame, XEON_PHI_SYSMEM_BASE, 
+                          XEON_PHI_SYSMEM_SIZE_BITS, XEON_PHI_SYSMEM_SIZE_BITS,
                           iocap);
     /*
      * XXX: there is no IO on the xeon phi, we use this slot to put in the
      *      capability to the host memory, as this can be seen as IO
      */
     struct cte *mmiocap = caps_locate_slot(CNODE(st->taskcn), TASKCN_SLOT_IO);
-    err = caps_create_new(ObjType_DevFrame,
-                          XEON_PHI_SBOX_BASE,
-                          XEON_PHI_SBOX_SIZE_BITS,
-                          XEON_PHI_SBOX_SIZE_BITS,
+    err = caps_create_new(ObjType_DevFrame, XEON_PHI_SBOX_BASE, 
+                          XEON_PHI_SBOX_SIZE_BITS, XEON_PHI_SBOX_SIZE_BITS,
                           mmiocap);
 
     assert(err_is_ok(err));
 
     /* Set fields in DCB */
     // Set Vspace
-    init_dcb->vspace = mem_to_local_phys((lvaddr_t) init_pml4);
+    init_dcb->vspace = mem_to_local_phys((lvaddr_t)init_pml4);
 
     // init dispatcher
     init_disp->disabled = true;
@@ -695,8 +399,7 @@ static struct dcb *spawn_init_common(struct spawn_state *st,
     return init_dcb;
 }
 
-struct dcb *spawn_bsp_init(const char *name,
-                           alloc_phys_func alloc_phys)
+struct dcb *spawn_bsp_init(const char *name, alloc_phys_func alloc_phys)
 {
     errval_t err;
 
@@ -705,30 +408,24 @@ struct dcb *spawn_bsp_init(const char *name,
 
     /* Allocate bootinfo */
     lpaddr_t bootinfo_phys = alloc_phys(BOOTINFO_SIZE);
-    memset((void *) local_phys_to_mem(bootinfo_phys), 0, BOOTINFO_SIZE);
+    memset((void *)local_phys_to_mem(bootinfo_phys), 0, BOOTINFO_SIZE);
 
     /* Construct cmdline args */
     char bootinfochar[16];
     snprintf(bootinfochar, sizeof(bootinfochar), "%lu", BOOTINFO_BASE);
-    const char *argv[] = {
-        "init",
-        bootinfochar
-    };
-
-    struct dcb *init_dcb = spawn_init_common(&spawn_state,
-                                             name,
-                                             ARRAY_LENGTH(argv),
-                                             argv,
-                                             bootinfo_phys,
-                                             alloc_phys);
+    const char *argv[] = { "init", bootinfochar };
+
+    struct dcb *init_dcb = spawn_init_common(&spawn_state, name,
+                                             ARRAY_LENGTH(argv), argv,
+                                             bootinfo_phys, alloc_phys);
 
     /* Map bootinfo R/W into VSpace at vaddr BOOTINFO_BASE */
-    paging_k1om_map_table(&init_pml4[0], mem_to_local_phys((lvaddr_t) init_pdpt));
-    paging_k1om_map_table(&init_pdpt[0], mem_to_local_phys((lvaddr_t) init_pdir));
-    paging_k1om_map_table(&init_pdir[1], mem_to_local_phys((lvaddr_t) init_ptable));
+    paging_x86_64_map_table(&init_pml4[0], mem_to_local_phys((lvaddr_t)init_pdpt));
+    paging_x86_64_map_table(&init_pdpt[0], mem_to_local_phys((lvaddr_t)init_pdir));
+    paging_x86_64_map_table(&init_pdir[1], mem_to_local_phys((lvaddr_t)init_ptable));
     for (int i = 0; i < BOOTINFO_SIZE / BASE_PAGE_SIZE; i++) {
-        paging_k1om_map(&init_ptable[i], bootinfo_phys + i * BASE_PAGE_SIZE,
-        INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R | PF_W));
+        paging_x86_64_map(&init_ptable[i], bootinfo_phys + i * BASE_PAGE_SIZE,
+                   INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R|PF_W));
     }
 
     /* Load init ELF64 binary from multiboot */
@@ -737,19 +434,16 @@ struct dcb *spawn_bsp_init(const char *name,
         panic("Could not find init module!");
     }
     lvaddr_t init_ep;
-    err = elf_load(EM_K1OM,
-                   startup_alloc_init,
-                   &spawn_state,
+    err = elf_load(EM_K1OM, startup_alloc_init, &spawn_state,
                    local_phys_to_mem(module->mod_start),
-                   MULTIBOOT_MODULE_SIZE(*module),
-                   &init_ep);
+                   MULTIBOOT_MODULE_SIZE(*module), &init_ep);
     if (err_is_fail(err)) {
         //err_print_calltrace(err);
         panic("ELF load of init module failed!");
     }
 
     struct dispatcher_shared_x86_64 *init_disp_x86_64 =
-                    get_dispatcher_shared_x86_64(init_dcb->disp);
+        get_dispatcher_shared_x86_64(init_dcb->disp);
     init_disp_x86_64->disabled_save_area.rip = init_ep;
 
     /* Create caps for init to use */
@@ -758,20 +452,19 @@ struct dcb *spawn_bsp_init(const char *name,
     // we run out of root cnode slots by aligning the memory we declare free
     // to 1MB.
     lpaddr_t init_alloc_end = alloc_phys(0);
-    lpaddr_t align = 1UL << 20;  // 1MB
+    lpaddr_t align = 1UL << 20; // 1MB
     // XXX: No checks are in place to make sure that init_alloc_end_aligned
     // is actually a valid physical memory address (e.g. a location at which
     // RAM exists.
-    lpaddr_t init_alloc_end_aligned = (init_alloc_end + align) & ~(align - 1);
+    lpaddr_t init_alloc_end_aligned = (init_alloc_end + align) & ~(align-1);
     printf("aligning free memory start to 0x%"PRIxLPADDR" (was 0x%"PRIxLPADDR
-    "): wasting %lu kB\n",
-           init_alloc_end_aligned, init_alloc_end, (init_alloc_end_aligned
-                           - init_alloc_end)
-                                                   / 1024);
+           "): wasting %lu kB\n",
+           init_alloc_end_aligned, init_alloc_end,
+           (init_alloc_end_aligned - init_alloc_end) / 1024);
     create_phys_caps(init_alloc_end_aligned);
 
     /* Fill bootinfo struct */
-    bootinfo->mem_spawn_core = NEEDED_KERNEL_SPACE;  // Size of kernel
+    bootinfo->mem_spawn_core = NEEDED_KERNEL_SPACE; // Size of kernel
 
     bootinfo->host_msg = glbl_core_data->bp->msg_base;
     bootinfo->host_msg_bits = glbl_core_data->bp->msg_size_bits;
@@ -780,8 +473,7 @@ struct dcb *spawn_bsp_init(const char *name,
 }
 
 struct dcb *spawn_app_init(struct x86_core_data *core_data,
-                           const char *name,
-                           alloc_phys_func alloc_phys)
+                           const char *name, alloc_phys_func alloc_phys)
 {
     errval_t err;
 
@@ -796,63 +488,51 @@ struct dcb *spawn_app_init(struct x86_core_data *core_data,
 
     // Arch id of the core that booted this core
     char archidchar[30];
-    snprintf(archidchar, sizeof(archidchar), "archid=%d", core_data->src_arch_id);
-
-    const char *argv[] = {
-        name,
-        coreidchar,
-        chanidchar,
-        archidchar
-    };
-
-    struct dcb *init_dcb = spawn_init_common(&spawn_state,
-                                             name,
-                                             ARRAY_LENGTH(argv),
-                                             argv,
-                                             0,
-                                             alloc_phys);
+    snprintf(archidchar, sizeof(archidchar), "archid=%d",
+             core_data->src_arch_id);
+
+    const char *argv[] = { name, coreidchar, chanidchar, archidchar };
+
+    struct dcb *init_dcb = spawn_init_common(&spawn_state, name,
+                                             ARRAY_LENGTH(argv), argv,
+                                             0, alloc_phys);
 
     // Urpc frame cap
     struct cte *urpc_frame_cte = caps_locate_slot(CNODE(spawn_state.taskcn),
-    TASKCN_SLOT_MON_URPC);
+                                                  TASKCN_SLOT_MON_URPC);
     // XXX: Create as devframe so the memory is not zeroed out
-    err = caps_create_new(ObjType_DevFrame,
-                          core_data->urpc_frame_base,
-                          core_data->urpc_frame_bits,
+    err = caps_create_new(ObjType_DevFrame, core_data->urpc_frame_base,
                           core_data->urpc_frame_bits,
-                          urpc_frame_cte);
+                          core_data->urpc_frame_bits, urpc_frame_cte);
     assert(err_is_ok(err));
     urpc_frame_cte->cap.type = ObjType_Frame;
     lpaddr_t urpc_ptr = gen_phys_to_local_phys(urpc_frame_cte->cap.u.frame.base);
 
     /* Map urpc frame at MON_URPC_BASE */
-    paging_k1om_map_table(&init_pml4[X86_64_PML4_BASE(MON_URPC_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_pdpt));
-    paging_k1om_map_table(&init_pdpt[X86_64_PDPT_BASE(MON_URPC_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_pdir));
-    paging_k1om_map_table(&init_pdir[X86_64_PDIR_BASE(MON_URPC_BASE)],
-                          mem_to_local_phys((lvaddr_t) init_ptable));
+    paging_x86_64_map_table(&init_pml4[X86_64_PML4_BASE(MON_URPC_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_pdpt));
+    paging_x86_64_map_table(&init_pdpt[X86_64_PDPT_BASE(MON_URPC_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_pdir));
+    paging_x86_64_map_table(&init_pdir[X86_64_PDIR_BASE(MON_URPC_BASE)],
+                            mem_to_local_phys((lvaddr_t)init_ptable));
     for (int i = 0; i < MON_URPC_SIZE / BASE_PAGE_SIZE; i++) {
-        paging_k1om_map(&init_ptable[X86_64_PTABLE_BASE(MON_URPC_BASE) + i],
-                        urpc_ptr + i * BASE_PAGE_SIZE,
-                        INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R | PF_W));
+        paging_x86_64_map(&init_ptable[X86_64_PTABLE_BASE(MON_URPC_BASE) + i],
+                          urpc_ptr + i * BASE_PAGE_SIZE,
+                          INIT_PAGE_BITMAP | paging_elf_to_page_flags(PF_R | PF_W));
     }
 
     // elf load the domain
     genvaddr_t entry_point;
-    err = elf_load(EM_K1OM,
-                   startup_alloc_init,
-                   &spawn_state,
+    err = elf_load(EM_K1OM, startup_alloc_init, &spawn_state,
                    local_phys_to_mem(core_data->monitor_binary),
-                   core_data->monitor_binary_size,
-                   &entry_point);
+                   core_data->monitor_binary_size, &entry_point);
     if (err_is_fail(err)) {
         //err_print_calltrace(err);
         panic("ELF load of init module failed!");
     }
 
     struct dispatcher_shared_x86_64 *init_disp_x86_64 =
-                    get_dispatcher_shared_x86_64(init_dcb->disp);
+        get_dispatcher_shared_x86_64(init_dcb->disp);
     init_disp_x86_64->disabled_save_area.rip = entry_point;
 
     return init_dcb;
diff --git a/kernel/arch/k1om/syscall.c b/kernel/arch/k1om/syscall.c
deleted file mode 100644 (file)
index 88fcb68..0000000
+++ /dev/null
@@ -1,1034 +0,0 @@
-/**
- * \file
- * \brief System calls implementation.
- */
-
-/*
- * Copyright (c) 2007, 2008, 2009, 2010, 2012, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <kernel.h>
-#include <syscall.h>
-#include <barrelfish_kpi/syscalls.h>
-#include <mdb/mdb.h>
-#include <dispatch.h>
-#include <paging_kernel_arch.h>
-#include <paging_generic.h>
-#include <exec.h>
-#include <arch/x86/apic.h>
-#include <arch/x86/global.h>
-#include <arch/x86/perfmon.h>
-#include <vmkit.h>
-#include <barrelfish_kpi/sys_debug.h>
-#include <barrelfish_kpi/lmp.h>
-#include <barrelfish_kpi/dispatcher_shared_target.h>
-#include <arch/x86/debugregs.h>
-#include <trace/trace.h>
-#include <arch/x86/syscall.h>
-#include <arch/x86/timing.h>
-#include <fpu.h>
-#include <arch/x86/ipi_notify.h>
-
-#define MIN(a,b)        ((a) < (b) ? (a) : (b))
-
-extern uint64_t user_stack_save;
-
-/* FIXME: lots of missing argument checks in this function */
-static struct sysret handle_dispatcher_setup(struct capability *to,
-                                             int cmd, uintptr_t *args)
-{
-    capaddr_t cptr = args[0];
-    int depth    = args[1];
-    capaddr_t vptr = args[2];
-    capaddr_t dptr = args[3];
-    bool run = args[4];
-    capaddr_t odptr = args[5];
-
-    return sys_dispatcher_setup(to, cptr, depth, vptr, dptr, run, odptr);
-}
-
-static struct sysret handle_dispatcher_properties(struct capability *to,
-                                                  int cmd, uintptr_t *args)
-{
-    enum task_type type = args[0];
-    unsigned long deadline = args[1];
-    unsigned long wcet = args[2];
-    unsigned long period = args[3];
-    unsigned long release = args[4];
-    unsigned short weight = args[5];
-
-    return sys_dispatcher_properties(to, type, deadline, wcet, period,
-                                     release, weight);
-}
-
-static struct sysret handle_retype_common(struct capability *root,
-                                          uintptr_t *args,
-                                          bool from_monitor)
-{
-    uint64_t source_cptr     = args[0];
-    uint64_t type            = args[1];
-    uint64_t objbits         = args[2];
-    uint64_t  dest_cnode_cptr = args[3];
-    uint64_t dest_slot       = args[4];
-    uint64_t dest_vbits      = args[5];
-
-    return sys_retype(root, source_cptr, type, objbits, dest_cnode_cptr,
-                      dest_slot, dest_vbits, from_monitor);
-}
-
-static struct sysret handle_retype(struct capability *root,
-                                   int cmd, uintptr_t *args)
-{
-    return handle_retype_common(root, args, false);
-}
-
-static struct sysret handle_create(struct capability *root,
-                                   int cmd, uintptr_t *args)
-{
-    /* Retrieve arguments */
-    enum objtype type         = args[0];
-    uint8_t objbits           = args[1];
-    capaddr_t dest_cnode_cptr = args[2];
-    cslot_t dest_slot         = args[3];
-    uint8_t dest_vbits        = args[4];
-
-    return sys_create(root, type, objbits, dest_cnode_cptr, dest_slot,
-                      dest_vbits);
-}
-
-
-/**
- * Common code for copying and minting except the mint flag and param passing
- */
-static struct sysret copy_or_mint(struct capability *root,
-                                  uintptr_t *args, bool mint)
-{
-    /* Retrive arguments */
-    capaddr_t  destcn_cptr   = args[0];
-    uint64_t dest_slot     = args[1];
-    capaddr_t  source_cptr   = args[2];
-    int      destcn_vbits  = args[3];
-    int      source_vbits  = args[4];
-    uint64_t param1, param2;
-    // params only sent if mint operation
-    if (mint) {
-        param1 = args[5];
-        param2 = args[6];
-    } else {
-        param1 = param2 = 0;
-    }
-
-    return sys_copy_or_mint(root, destcn_cptr, dest_slot, source_cptr,
-                            destcn_vbits, source_vbits, param1, param2, mint);
-}
-
-static struct sysret handle_map(struct capability *ptable,
-                                int cmd, uintptr_t *args)
-{
-    /* Retrieve arguments */
-    uint64_t  slot          = args[0];
-    capaddr_t source_cptr   = args[1];
-    int       source_vbits  = args[2];
-    uint64_t  flags         = args[3];
-    uint64_t  offset        = args[4];
-    uint64_t  pte_count     = args[5];
-
-    return sys_map(ptable, slot, source_cptr, source_vbits, flags, offset,
-                   pte_count);
-}
-
-static struct sysret handle_mint(struct capability *root,
-                                 int cmd, uintptr_t *args)
-{
-    return copy_or_mint(root, args, true);
-}
-
-static struct sysret handle_copy(struct capability *root,
-                                 int cmd, uintptr_t *args)
-{
-    return copy_or_mint(root, args, false);
-}
-
-static struct sysret handle_delete_common(struct capability *root,
-                                   uintptr_t *args,
-                                   bool from_monitor)
-{
-    capaddr_t cptr = args[0];
-    int bits     = args[1];
-    return sys_delete(root, cptr, bits, from_monitor);
-}
-
-static struct sysret handle_delete(struct capability *root,
-                                   int cmd, uintptr_t *args)
-{
-    return handle_delete_common(root, args, false);
-}
-
-
-static struct sysret handle_revoke_common(struct capability *root,
-                                          uintptr_t *args,
-                                          bool from_monitor)
-{
-    capaddr_t cptr = args[0];
-    int bits     = args[1];
-    return sys_revoke(root, cptr, bits, from_monitor);
-}
-
-static struct sysret handle_revoke(struct capability *root,
-                                   int cmd, uintptr_t *args) 
-{
-    return handle_revoke_common(root, args, false);
-}
-
-
-static struct sysret handle_unmap(struct capability *pgtable,
-                                  int cmd, uintptr_t *args)
-{
-    capaddr_t cptr = args[0];
-    int bits       = args[1];
-    size_t entry   = args[2];
-    size_t pages   = args[3];
-
-    errval_t err;
-    struct cte *mapping;
-    err = caps_lookup_slot(&dcb_current->cspace.cap, cptr, bits,
-                                    &mapping, CAPRIGHTS_READ_WRITE);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_CAP_NOT_FOUND));
-    }
-
-    err = page_mappings_unmap(pgtable, mapping, entry, pages);
-    return SYSRET(err);
-}
-
-/// Different handler for cap operations performed by the monitor
-static struct sysret monitor_handle_retype(struct capability *kernel_cap,
-                                           int cmd, uintptr_t *args)
-{
-    errval_t err;
-
-    capaddr_t root_caddr = args[0];
-    capaddr_t root_vbits = args[1];
-
-    struct capability *root;
-    err = caps_lookup_cap(&dcb_current->cspace.cap, root_caddr, root_vbits,
-                          &root, CAPRIGHTS_READ);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_ROOT_CAP_LOOKUP));
-    }
-
-    /* XXX: this hides the first two arguments */
-    return handle_retype_common(root, &args[2], true);
-}
-
-/// Different handler for cap operations performed by the monitor
-static struct sysret monitor_handle_delete(struct capability *kernel_cap,
-                                           int cmd, uintptr_t *args)
-{
-    errval_t err;
-
-    capaddr_t root_caddr = args[0];
-    capaddr_t root_vbits = args[1];
-
-    struct capability *root;
-    err = caps_lookup_cap(&dcb_current->cspace.cap, root_caddr, root_vbits,
-                          &root, CAPRIGHTS_READ);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_ROOT_CAP_LOOKUP));
-    }
-
-    /* XXX: this hides the first two arguments */
-    return handle_delete_common(root, &args[2], true);
-}
-
-/// Different handler for cap operations performed by the monitor
-static struct sysret monitor_handle_revoke(struct capability *kernel_cap,
-                                           int cmd, uintptr_t *args)
-{
-    errval_t err;
-
-    capaddr_t root_caddr = args[0];
-    capaddr_t root_vbits = args[1];
-
-    struct capability *root;
-    err = caps_lookup_cap(&dcb_current->cspace.cap, root_caddr, root_vbits,
-                          &root, CAPRIGHTS_READ);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_ROOT_CAP_LOOKUP));
-    }
-
-    /* XXX: this hides the first two arguments */
-    return handle_revoke_common(root, &args[2], true);
-}
-
-static struct sysret monitor_handle_register(struct capability *kernel_cap,
-                                             int cmd, uintptr_t *args)
-{
-    capaddr_t ep_caddr = args[0];
-    return sys_monitor_register(ep_caddr);
-}
-
-/**
- * \brief Spawn a new core and create a kernel cap for it.
- */
-static struct sysret monitor_spawn_core(struct capability *kernel_cap,
-                                        int cmd, uintptr_t *args)
-{
-    coreid_t core_id       = args[0];
-    enum cpu_type cpu_type = args[1];
-    genvaddr_t entry       = args[2];
-
-    return sys_monitor_spawn_core(core_id, cpu_type, entry);
-}
-
-static struct sysret monitor_get_core_id(struct capability *kernel_cap,
-                                         int cmd, uintptr_t *args)
-{
-    return (struct sysret){.error = SYS_ERR_OK, .value = my_core_id};
-}
-
-static struct sysret monitor_get_arch_id(struct capability *kernel_cap,
-                                         int cmd, uintptr_t *args)
-{
-    return (struct sysret){.error = SYS_ERR_OK, .value = apic_id};
-}
-
-static struct sysret monitor_identify_cap_common(struct capability *kernel_cap,
-                                                 struct capability *root,
-                                                 uintptr_t *args)
-{
-    capaddr_t cptr = args[0];
-    uint8_t bits = args[1];
-    struct capability *retbuf = (void *)args[2];
-
-    return sys_monitor_identify_cap(root, cptr, bits, retbuf);
-}
-
-static struct sysret monitor_identify_cap(struct capability *kernel_cap,
-                                          int cmd, uintptr_t *args)
-{
-    return monitor_identify_cap_common(kernel_cap, &dcb_current->cspace.cap, args);
-}
-
-static struct sysret monitor_identify_domains_cap(struct capability *kernel_cap,
-                                                  int cmd, uintptr_t *args)
-{
-    errval_t err;
-
-    capaddr_t root_caddr = args[0];
-    capaddr_t root_vbits = args[1];
-
-    struct capability *root;
-    err = caps_lookup_cap(&dcb_current->cspace.cap, root_caddr, root_vbits,
-                          &root, CAPRIGHTS_READ);
-
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_ROOT_CAP_LOOKUP));
-    }
-
-    /* XXX: this hides the first two arguments */
-    return monitor_identify_cap_common(kernel_cap, root, &args[2]);
-}
-
-static struct sysret monitor_remote_cap(struct capability *kernel_cap,
-                                        int cmd, uintptr_t *args)
-{
-    struct capability *root = &dcb_current->cspace.cap;
-    capaddr_t cptr = args[0];
-    int bits = args[1];
-    bool remote = args[2];
-
-    struct cte *cte;
-    errval_t err = caps_lookup_slot(root, cptr, bits, &cte, CAPRIGHTS_WRITE);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_IDENTIFY_LOOKUP));
-    }
-
-    set_cap_remote(cte, remote);
-    bool has_desc = has_descendants(cte);
-
-    return (struct sysret){ .error = SYS_ERR_OK, .value = has_desc };
-}
-
-
-static struct sysret monitor_create_cap(struct capability *kernel_cap,
-                                        int cmd, uintptr_t *args)
-{
-    /* XXX: Get the raw metadata of the capability to create */
-    struct capability *src = (struct capability *)args;
-    int pos = sizeof(struct capability) / sizeof(uint64_t);
-
-    /* Certain types cannot be created here */
-    if ((src->type == ObjType_Null) || (src->type == ObjType_EndPoint)
-        || (src->type == ObjType_Dispatcher) || (src->type == ObjType_Kernel)
-        || (src->type == ObjType_IRQTable)) {
-        return SYSRET(SYS_ERR_ILLEGAL_DEST_TYPE);
-    }
-
-    /* Create the cap in the destination */
-    capaddr_t cnode_cptr = args[pos];
-    int cnode_vbits    = args[pos + 1];
-    size_t slot        = args[pos + 2];
-
-    return SYSRET(caps_create_from_existing(&dcb_current->cspace.cap,
-                                            cnode_cptr, cnode_vbits,
-                                            slot, src));
-}
-
-static struct sysret monitor_nullify_cap(struct capability *kernel_cap,
-                                         int cmd, uintptr_t *args)
-{
-    capaddr_t cptr = args[0];
-    uint8_t bits = args[1];
-
-    return sys_monitor_nullify_cap(cptr, bits);
-}
-
-static struct sysret monitor_iden_cnode_get_cap(struct capability *kern_cap,
-                                                int cmd, uintptr_t *args)
-{
-    errval_t err;
-
-    /* XXX: Get the raw metadata of the cnode */
-    int pos = sizeof(struct capability) / sizeof(uint64_t);
-    struct capability *cnode = (struct capability *)args;
-    assert(cnode->type == ObjType_CNode);
-
-    struct capability *cnode_copy;
-    err = mdb_get_copy(cnode, &cnode_copy);
-    if (err_is_fail(err)) {
-        return SYSRET(err);
-    }
-
-    capaddr_t slot = args[pos];
-    struct cte* cte = caps_locate_slot(cnode_copy->u.cnode.cnode, slot);
-
-    // XXX: Write cap data directly back to user-space
-    // FIXME: this should involve a pointer/range check for reliability,
-    // but because the monitor is inherently trusted it's not a security hole
-    struct capability *retbuf = (void *)args[pos + 1];
-    *retbuf = cte->cap;
-
-    return SYSRET(SYS_ERR_OK);
-}
-
-static struct sysret monitor_handle_sync_timer(struct capability *kern_cap,
-                                               int cmd, uintptr_t *args)
-{
-    uint64_t synctime = args[0];
-    return sys_monitor_handle_sync_timer(synctime);
-}
-
-static struct sysret handle_frame_identify(struct capability *to,
-                                           int cmd, uintptr_t *args)
-{
-    // Return with physical base address of frame
-    // XXX: pack size into bottom bits of base address
-    assert(to->type == ObjType_Frame || to->type == ObjType_DevFrame);
-    assert((to->u.frame.base & BASE_PAGE_MASK) == 0);
-    return (struct sysret) {
-        .error = SYS_ERR_OK,
-        .value = to->u.frame.base | to->u.frame.bits,
-    };
-}
-
-static struct sysret handle_frame_modify_flags(struct capability *to,
-                                               int cmd, uintptr_t *args)
-{
-    // Modify flags of (part of) mapped region of frame
-    assert(to->type == ObjType_Frame || to->type == ObjType_DevFrame);
-
-    // unpack arguments
-    size_t offset = args[0]; // in pages; of first page to modify from first
-                             // page in mapped region
-    size_t pages  = args[1]; // #pages to modify
-    size_t flags  = args[2]; // new flags
-
-    page_mappings_modify_flags(to, offset, pages, flags);
-
-    return (struct sysret) {
-        .error = SYS_ERR_OK,
-        .value = 0,
-    };
-}
-
-
-static struct sysret handle_io(struct capability *to, int cmd, uintptr_t *args)
-{
-    uint64_t    port = args[0];
-    uint64_t    data = args[1]; // ignored for input
-
-    return sys_io(to, cmd, port, data);
-}
-
-static struct sysret
-handle_dispatcher_setup_guest (struct capability *to, int cmd, uintptr_t *args)
-{
-    errval_t err;
-    struct dcb *dcb = to->u.dispatcher.dcb;
-
-    capaddr_t epp = args[0];
-    capaddr_t vnodep = args[1];
-    capaddr_t vmcbp = args[2];
-    capaddr_t ctrlp = args[3];
-
-    // 0. Enable VM extensions
-    err = vmkit_enable_virtualization();
-    if (err != SYS_ERR_OK) {
-        return SYSRET(err);
-    }
-
-    // 1. Check arguments
-    // Monitor endpoint for exits of this geust
-    struct cte *ep_cte;
-
-    err = caps_lookup_slot(&dcb_current->cspace.cap, epp, CPTR_BITS,
-                           &ep_cte, CAPRIGHTS_READ_WRITE);
-    if (err_is_fail(err)) {
-        return SYSRET(err);
-    }
-    if (ep_cte->cap.type != ObjType_EndPoint) {
-        return SYSRET(SYS_ERR_VMKIT_ENDPOINT_INVALID);
-    }
-    err = caps_copy_to_cte(&dcb->guest_desc.monitor_ep, ep_cte, false, 0, 0);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_VMKIT_ENDPOINT));
-    }
-
-    // Domain vspace
-    struct capability *vnode_cap;
-    err = caps_lookup_cap(&dcb_current->cspace.cap, vnodep, CPTR_BITS,
-                          &vnode_cap, CAPRIGHTS_WRITE);
-    if (err_is_fail(err)) {
-        return SYSRET(err);
-    }
-    if (vnode_cap->type != ObjType_VNode_x86_64_pml4) {
-        return SYSRET(SYS_ERR_DISP_VSPACE_INVALID);
-    }
-
-    assert(vnode_cap->type == ObjType_VNode_x86_64_pml4);
-
-    // VMCB
-    struct cte *vmcb_cte;
-    err = caps_lookup_slot(&dcb_current->cspace.cap, vmcbp, CPTR_BITS,
-                           &vmcb_cte, CAPRIGHTS_READ_WRITE);
-    if (err_is_fail(err)) {
-        return SYSRET(err);
-    }
-    if (vmcb_cte->cap.type != ObjType_Frame ||
-        vmcb_cte->cap.u.frame.bits < BASE_PAGE_BITS) {
-        return SYSRET(SYS_ERR_VMKIT_VMCB_INVALID);
-    }
-    err = caps_copy_to_cte(&dcb->guest_desc.vmcb, vmcb_cte, false, 0, 0);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_VMKIT_VMCB));
-    }
-
-    // guest control
-    struct cte *ctrl_cte;
-    err = caps_lookup_slot(&dcb_current->cspace.cap, ctrlp, CPTR_BITS,
-                           &ctrl_cte, CAPRIGHTS_READ_WRITE);
-    if (err_is_fail(err)) {
-        return SYSRET(err);
-    }
-    if (ctrl_cte->cap.type != ObjType_Frame ||
-        ctrl_cte->cap.u.frame.bits < BASE_PAGE_BITS) {
-        return SYSRET(SYS_ERR_VMKIT_CTRL_INVALID);
-    }
-    err = caps_copy_to_cte(&dcb->guest_desc.ctrl, ctrl_cte, false, 0, 0);
-    if (err_is_fail(err)) {
-        return SYSRET(err_push(err, SYS_ERR_VMKIT_CTRL));
-    }
-
-    // 2. Set up the target DCB
-/*     dcb->guest_desc.monitor_ep = ep_cap; */
-    dcb->vspace = vnode_cap->u.vnode_x86_64_pml4.base;
-    dcb->is_vm_guest = true;
-/*     dcb->guest_desc.vmcb = vmcb_cap->u.frame.base; */
-/*     dcb->guest_desc.ctrl = (void *)x86_64_phys_to_mem(ctrl_cap->u.frame.base); */
-
-    return SYSRET(SYS_ERR_OK);
-}
-
-static struct sysret monitor_handle_domain_id(struct capability *monitor_cap,
-                                              int cmd, uintptr_t *args)
-{
-    capaddr_t cptr = args[0];
-    domainid_t domain_id = args[1];
-
-    return sys_monitor_domain_id(cptr, domain_id);
-}
-
-/**
- * \brief Set up tracing in the kernel
- */
-static struct sysret handle_trace_setup(struct capability *cap,
-                                        int cmd, uintptr_t *args)
-{
-    struct capability *frame;
-    errval_t err;
-
-    /* lookup passed cap */
-    capaddr_t cptr = args[0];
-    err = caps_lookup_cap(&dcb_current->cspace.cap, cptr, CPTR_BITS, &frame,
-                          CAPRIGHTS_READ_WRITE);
-    if (err_is_fail(err)) {
-        return SYSRET(err);
-    }
-
-    lpaddr_t lpaddr = gen_phys_to_local_phys(frame->u.frame.base);
-    kernel_trace_buf = local_phys_to_mem(lpaddr);
-    //printf("kernel.%u: handle_trace_setup at %lx\n", apic_id, kernel_trace_buf);
-
-    // Copy boot applications.
-    trace_copy_boot_applications();
-
-    return SYSRET(SYS_ERR_OK);
-}
-
-static struct sysret handle_irq_table_set(struct capability *to, int cmd,
-                                          uintptr_t *args)
-{
-    return SYSRET(irq_table_set(args[0], args[1]));
-}
-
-static struct sysret handle_irq_table_delete(struct capability *to, int cmd,
-                                             uintptr_t *args)
-{
-    return SYSRET(irq_table_delete(args[0]));
-}
-
-static struct sysret handle_ipi_notify_send(struct capability *cap,
-                                            int cmd, uintptr_t *args)
-{
-    assert(cap->type == ObjType_Notify_IPI);
-    return ipi_raise_notify(cap->u.notify_ipi.coreid, cap->u.notify_ipi.chanid);
-}
-
-static struct sysret kernel_ipi_register(struct capability *cap,
-                                         int cmd, uintptr_t *args)
-{
-    assert(cap->type == ObjType_Kernel);
-    capaddr_t ep = args[0];
-    int chanid = args[1];
-    return SYSRET(ipi_register_notification(ep, chanid));
-}
-
-static struct sysret kernel_ipi_delete(struct capability *cap,
-                                       int cmd, uintptr_t *args)
-{
-    assert(cap->type == ObjType_Kernel);
-    assert(!"NYI");
-    return SYSRET(SYS_ERR_OK);
-}
-
-static struct sysret dispatcher_dump_ptables(struct capability *cap,
-                                             int cmd, uintptr_t *args)
-{
-    assert(cap->type == ObjType_Dispatcher);
-
-    printf("kernel_dump_ptables\n");
-
-    struct dcb *dispatcher = cap->u.dispatcher.dcb;
-
-    paging_dump_tables(dispatcher);
-
-    return SYSRET(SYS_ERR_OK);
-}
-
-/* 
- * \brief Activate performance monitoring
- * 
- * Activates performance monitoring.
- * \param xargs Expected parameters in args:
- * - performance monitoring type
- * - mask for given type
- * - Counter id
- * - Also count in privileged mode
- * - Number of counts before overflow. This parameter may be used to
- *   set tradeoff between accuracy and overhead. Set the counter to 0
- *   to deactivate the usage of APIC.
- * - Endpoint capability to be invoked when the counter overflows.
- *   The buffer associated with the endpoint needs to be large enough
- *   to hold several overflow notifications depending on the overflow 
- *   frequency.
- */
-static struct sysret performance_counter_activate(struct capability *cap,
-                                                  int cmd, uintptr_t *args)
-{
-    uint8_t event = args[0];
-    uint8_t umask = args[1];
-    uint8_t counter_id = args[2];
-    bool kernel = args[3];
-    uint64_t counter_value = args[4];
-    capaddr_t ep_addr = args[5];
-
-    errval_t err;
-    struct capability *ep;
-    extern struct capability perfmon_callback_ep;
-
-    // Make sure that 
-    assert(ep_addr!=0 || counter_value==0);
-
-    perfmon_init();
-    perfmon_measure_start(event, umask, counter_id, kernel, counter_value);
-
-    if(ep_addr!=0) {
-        
-        err = caps_lookup_cap(&dcb_current->cspace.cap, ep_addr, CPTR_BITS, &ep,
-                               CAPRIGHTS_READ);
-        if(err_is_fail(err)) {
-            return SYSRET(err);
-        }
-        
-        perfmon_callback_ep = *ep; 
-    }
-
-    return SYSRET(SYS_ERR_OK);
-}
-
-/* 
- * \brief Write counter values.
- */
-static struct sysret performance_counter_write(struct capability *cap,
-                                               int cmd, uintptr_t *args)
-{
-    uint8_t counter_id = args[0];
-    uint64_t counter_value = args[1];
-
-    perfmon_measure_write(counter_id, counter_value);
-    return SYSRET(SYS_ERR_OK);
-}
-
-/* 
- * \brief Deactivate performance counters again.
- */
-static struct sysret performance_counter_deactivate(struct capability *cap,
-                                                  int cmd, uintptr_t *args)
-{
-    perfmon_measure_stop();
-    return SYSRET(SYS_ERR_OK);
-}
-
-/*
- * \brief Return system-wide unique ID of this ID cap.
- */
-static struct sysret handle_idcap_identify(struct capability *cap, int cmd,
-                                           uintptr_t *args)
-{
-    idcap_id_t id;
-    struct sysret sysret = sys_idcap_identify(cap, &id);
-    sysret.value = id;
-
-    return sysret;
-}
-
-typedef struct sysret (*invocation_handler_t)(struct capability *to,
-                                              int cmd, uintptr_t *args);
-
-static invocation_handler_t invocations[ObjType_Num][CAP_MAX_CMD] = {
-    [ObjType_Dispatcher] = {
-        [DispatcherCmd_Setup] = handle_dispatcher_setup,
-        [DispatcherCmd_Properties] = handle_dispatcher_properties,
-        [DispatcherCmd_SetupGuest] = handle_dispatcher_setup_guest,
-        [DispatcherCmd_DumpPTables]  = dispatcher_dump_ptables,
-    },
-    [ObjType_Frame] = {
-        [FrameCmd_Identify] = handle_frame_identify,
-        [FrameCmd_ModifyFlags] = handle_frame_modify_flags,
-    },
-    [ObjType_DevFrame] = {
-        [FrameCmd_Identify] = handle_frame_identify,
-        [FrameCmd_ModifyFlags] = handle_frame_modify_flags,
-    },
-    [ObjType_CNode] = {
-        [CNodeCmd_Copy]   = handle_copy,
-        [CNodeCmd_Mint]   = handle_mint,
-        [CNodeCmd_Retype] = handle_retype,
-        [CNodeCmd_Create] = handle_create,
-        [CNodeCmd_Delete] = handle_delete,
-        [CNodeCmd_Revoke] = handle_revoke,
-    },
-    [ObjType_VNode_x86_64_pml4] = {
-        [VNodeCmd_Map]   = handle_map,
-        [VNodeCmd_Unmap] = handle_unmap,
-    },
-    [ObjType_VNode_x86_64_pdpt] = {
-        [VNodeCmd_Map]   = handle_map,
-        [VNodeCmd_Unmap] = handle_unmap,
-    },
-    [ObjType_VNode_x86_64_pdir] = {
-        [VNodeCmd_Map]   = handle_map,
-        [VNodeCmd_Unmap] = handle_unmap,
-    },
-    [ObjType_VNode_x86_64_ptable] = {
-        [VNodeCmd_Map]   = handle_map,
-        [VNodeCmd_Unmap] = handle_unmap,
-    },
-    [ObjType_Kernel] = {
-        [KernelCmd_Spawn_core]   = monitor_spawn_core,
-        [KernelCmd_Get_core_id]  = monitor_get_core_id,
-        [KernelCmd_Get_arch_id]  = monitor_get_arch_id,
-        [KernelCmd_Identify_cap] = monitor_identify_cap,
-        [KernelCmd_Identify_domains_cap] = monitor_identify_domains_cap,
-        [KernelCmd_Remote_cap]   = monitor_remote_cap,
-        [KernelCmd_Iden_cnode_get_cap] = monitor_iden_cnode_get_cap,
-        [KernelCmd_Create_cap]   = monitor_create_cap,
-        [KernelCmd_Nullify_cap]  = monitor_nullify_cap,
-        [KernelCmd_Setup_trace]  = handle_trace_setup,
-        [KernelCmd_Register]     = monitor_handle_register,
-        [KernelCmd_Domain_Id]    = monitor_handle_domain_id,
-        [MonitorCmd_Retype]      = monitor_handle_retype,
-        [MonitorCmd_Delete]      = monitor_handle_delete,
-        [MonitorCmd_Revoke]      = monitor_handle_revoke,
-        [KernelCmd_Sync_timer]   = monitor_handle_sync_timer,
-        [KernelCmd_IPI_Register] = kernel_ipi_register,
-        [KernelCmd_IPI_Delete]   = kernel_ipi_delete,
-    },
-    [ObjType_IRQTable] = {
-        [IRQTableCmd_Set] = handle_irq_table_set,
-        [IRQTableCmd_Delete] = handle_irq_table_delete
-    },
-    [ObjType_IO] = {
-        [IOCmd_Outb] = handle_io,
-        [IOCmd_Outw] = handle_io,
-        [IOCmd_Outd] = handle_io,
-        [IOCmd_Inb] = handle_io,
-        [IOCmd_Inw] = handle_io,
-        [IOCmd_Ind] = handle_io
-    },
-    [ObjType_Notify_IPI] = {
-        [NotifyCmd_Send] = handle_ipi_notify_send
-    },
-    [ObjType_PerfMon] = {
-        [PerfmonCmd_Activate] = performance_counter_activate,
-        [PerfmonCmd_Deactivate] = performance_counter_deactivate,
-        [PerfmonCmd_Write] = performance_counter_write,
-    },
-    [ObjType_ID] = {
-        [IDCmd_Identify] = handle_idcap_identify,
-    }
-};
-
-/* syscall C entry point; called only from entry.S so no prototype in header */
-struct sysret sys_syscall(uint64_t syscall, uint64_t arg0, uint64_t arg1,
-                          uint64_t *args, uint64_t rflags, uint64_t rip);
-struct sysret sys_syscall(uint64_t syscall, uint64_t arg0, uint64_t arg1,
-                          uint64_t *args, uint64_t rflags, uint64_t rip)
-{
-    struct sysret retval = { .error = SYS_ERR_OK, .value = 0 };
-
-    switch(syscall) {
-    case SYSCALL_INVOKE: /* Handle capability invocation */
-    {
-        // unpack "header" word
-        capaddr_t invoke_cptr = arg0 >> 32;
-        uint8_t send_bits = arg0 >> 24;
-        uint8_t invoke_bits = arg0 >> 16;
-        uint8_t length_words = arg0 >> 8;
-        uint8_t flags = arg0;
-
-        debug(SUBSYS_SYSCALL, "sys_invoke(0x%x(%d), 0x%lx)\n",
-              invoke_cptr, invoke_bits, arg1);
-
-        // Capability to invoke
-        struct capability *to = NULL;
-        retval.error = caps_lookup_cap(&dcb_current->cspace.cap, invoke_cptr,
-                                       invoke_bits, &to, CAPRIGHTS_READ);
-        if (err_is_fail(retval.error)) {
-            break;
-        }
-
-        assert(to != NULL);
-        assert(to->type < ObjType_Num);
-
-        // Endpoint cap, do LMP
-        if (to->type == ObjType_EndPoint) {
-            struct dcb *listener = to->u.endpoint.listener;
-            assert(listener != NULL);
-
-            if (listener->disp == 0) {
-                retval.error = SYS_ERR_LMP_NO_TARGET;
-                break;
-            }
-
-            /* limit length of message from buggy/malicious sender */
-            length_words = MIN(length_words, LMP_MSG_LENGTH);
-
-            // does the sender want to yield their timeslice on success?
-            bool sync = flags & LMP_FLAG_SYNC;
-            // does the sender want to yield to the target if undeliverable?
-            bool yield = flags & LMP_FLAG_YIELD;
-
-            // try to deliver message
-            retval.error = lmp_deliver(to, dcb_current, args, length_words,
-                                       arg1, send_bits);
-
-            /* Switch to reciever upon successful delivery with sync flag,
-             * or (some cases of) unsuccessful delivery with yield flag */
-            enum err_code err_code = err_no(retval.error);
-            if ((sync && err_is_ok(retval.error)) ||
-                (yield && (err_code == SYS_ERR_LMP_BUF_OVERFLOW
-                           || err_code == SYS_ERR_LMP_CAPTRANSFER_DST_CNODE_LOOKUP
-                           || err_code == SYS_ERR_LMP_CAPTRANSFER_DST_CNODE_INVALID
-                           || err_code == SYS_ERR_LMP_CAPTRANSFER_DST_SLOT_OCCUPIED))
-                    ) {
-                if (err_is_fail(retval.error)) {
-                    struct dispatcher_shared_generic *current_disp =
-                        get_dispatcher_shared_generic(dcb_current->disp);
-                    struct dispatcher_shared_generic *listener_disp =
-                        get_dispatcher_shared_generic(listener->disp);
-                    debug(SUBSYS_DISPATCH, "LMP failed; %.*s yields to %.*s: %u\n",
-                          DISP_NAME_LEN, current_disp->name,
-                          DISP_NAME_LEN, listener_disp->name, err_code);
-                }
-
-                // special-case context switch: ensure correct state in current DCB
-                dispatcher_handle_t handle = dcb_current->disp;
-                struct dispatcher_shared_x86_64 *disp =
-                    get_dispatcher_shared_x86_64(handle);
-                dcb_current->disabled = dispatcher_is_disabled_ip(handle, rip);
-                struct registers_x86_64 *save_area;
-                if (dcb_current->disabled) {
-                    save_area = &disp->disabled_save_area;
-                } else {
-                    save_area = &disp->enabled_save_area;
-                }
-
-                // save calling dispatcher's registers, so that when the dispatcher
-                // next runs, it has a valid state in the relevant save area.
-                // Save RIP, RFLAGS, RSP and set RAX (return value) for later resume
-                save_area->rax = retval.error; // XXX: x86 1st return register
-                save_area->rip = rip;
-                save_area->eflags = rflags;
-                save_area->rsp = user_stack_save;
-
-                /* save and zero FS/GS selectors (they're unmodified by the syscall path) */
-                __asm ("mov     %%fs, %[fs]     \n\t"
-                       "mov     %%gs, %[gs]     \n\t"
-                       "mov     %[zero], %%fs   \n\t"
-                       "mov     %[zero], %%gs   \n\t"
-                       : /* No output */
-                       :
-                       [fs] "m" (save_area->fs),
-                       [gs] "m" (save_area->gs),
-                       [zero] "r" (0)
-                       );
-
-                dispatch(to->u.endpoint.listener);
-                panic("dispatch returned");
-            }
-        } else { // not endpoint cap, call kernel handler through dispatch table
-            uint64_t cmd = args[0];
-            if (cmd >= CAP_MAX_CMD) {
-                retval.error = SYS_ERR_ILLEGAL_INVOCATION;
-                break;
-            }
-
-            // Call the invocation
-            invocation_handler_t invocation = invocations[to->type][cmd];
-            if(invocation == NULL) {
-                retval.error = SYS_ERR_ILLEGAL_INVOCATION;
-            } else {
-                retval = invocation(to, cmd, &args[1]);
-            }
-        }
-        break;
-    }
-
-        // Yield the CPU to the next dispatcher
-    case SYSCALL_YIELD:
-        retval = sys_yield((capaddr_t)arg0);
-        break;
-
-        // NOP system call for benchmarking purposes
-    case SYSCALL_NOP:
-        break;
-
-        // Debug print system call
-    case SYSCALL_PRINT:
-        retval.error = sys_print((char *)arg0, arg1);
-        break;
-
-        // Reboot!
-        // FIXME: this should be a kernel cap invocation or similarly restricted
-    case SYSCALL_REBOOT:
-        reboot();
-        break;
-
-    case SYSCALL_X86_FPU_TRAP_ON:
-        fpu_trap_on();
-        break;
-
-    case SYSCALL_X86_RELOAD_LDT:
-        maybe_reload_ldt(dcb_current, true);
-        break;
-
-    case SYSCALL_DEBUG:
-        switch(arg0) {
-        case DEBUG_CONTEXT_COUNTER_RESET:
-            dispatch_csc_reset();
-            break;
-
-        case DEBUG_CONTEXT_COUNTER_READ:
-            retval.value = dispatch_get_csc();
-            break;
-
-        case DEBUG_TIMESLICE_COUNTER_READ:
-            retval.value = kernel_now;
-            break;
-
-        case DEBUG_FLUSH_CACHE:
-            wbinvd();
-            break;
-
-        case DEBUG_SEND_IPI:
-            apic_send_std_ipi(arg1, args[0], args[1]);
-            break;
-
-        case DEBUG_SET_BREAKPOINT:
-            debugregs_set_breakpoint(arg1, args[0], args[1]);
-            break;
-
-        case DEBUG_GET_TSC_PER_MS:
-            retval.value = timing_get_tsc_per_ms();
-            break;
-
-        case DEBUG_GET_APIC_TIMER:
-            retval.value = apic_timer_get_count();
-            break;
-
-        case DEBUG_GET_APIC_TICKS_PER_SEC:
-            retval.value = timing_get_apic_ticks_per_sec();
-            break;
-
-        default:
-            printk(LOG_ERR, "invalid sys_debug msg type\n");
-        }
-        break;
-
-    default:
-        printk(LOG_ERR, "sys_syscall: Illegal system call! "
-               "(0x%lx, 0x%lx, 0x%lx)\n", syscall, arg0, arg1);
-        retval.error = SYS_ERR_ILLEGAL_SYSCALL;
-        break;
-    }
-
-    // If dcb_current got removed, dispatch someone else
-    if (dcb_current == NULL) {
-        assert(err_is_ok(retval.error));
-        dispatch(schedule());
-    }
-
-    if (syscall == SYSCALL_INVOKE) {
-        debug(SUBSYS_SYSCALL, "invoke returning 0x%lx 0x%lx\n",
-              retval.error, retval.value);
-    }
-
-    return retval;
-}
diff --git a/kernel/arch/k1om/vmkit.c b/kernel/arch/k1om/vmkit.c
deleted file mode 100644 (file)
index c327c0d..0000000
+++ /dev/null
@@ -1,278 +0,0 @@
-/**
- * \file
- * \brief VMKit Kernel interface.
- */
-
-/*
- * Copyright (c) 2009, 2010, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <string.h>
-#include <kernel.h>
-#include <paging_kernel_arch.h>
-#include <vmkit.h>
-#include <x86.h>
-#include <dispatch.h>
-#include <exec.h>
-#include <barrelfish_kpi/vmkit.h>
-
-#include <amd_vmcb_dev.h>
-
-// SVM relevant CPUID info
-#define CPUID_AMD_EXTFEAT       0x80000001
-#define AMD_EXTFEAT_ECX_SVM     (1 << 2)
-
-// some EXITCODE values
-#define VMEXIT_INTR     0x60
-#define VMEXIT_NMI      0x61
-#define VMEXIT_SMI      0x62
-#define VMEXIT_VMMCALL  0x81
-
-/**
- * \brief The storage area where SVM puts the host state during guest exec.
- */
-static uint8_t host_save_area[BASE_PAGE_SIZE]
-__attribute__ ((aligned(BASE_PAGE_SIZE)));
-
-/**
- * \brief VMCB for the host to save its state.
- */
-static uint8_t host_vmcb[BASE_PAGE_SIZE]
-__attribute__ ((aligned(BASE_PAGE_SIZE)));
-
-static void
-vmkit_init (void)
-{
-    static bool executed = false;
-
-    if (executed) {
-        return;
-    }
-
-    executed = true;
-    memset(host_save_area, 0x0, BASE_PAGE_SIZE);
-    memset(host_vmcb, 0x0, BASE_PAGE_SIZE);
-}
-
-/**
- * \brief Tries to enable hardware assisted virtualization.
- *
- * Checks whether hardware assisted virtualization is available on the platform
- * and enables this feature.
- *
- * \Return Returns VMKIT_ERR_OK on successful initialization of the subsystem
- *         or VMKIT_ERR_UNAVAIL if virtualization is unavailable.
- */
-errval_t
-vmkit_enable_virtualization (void)
-{
-    vmkit_init ();
-
-    // first check what CPUID tells us about SVM support
-    uint32_t cpuid_ecx;
-    cpuid(CPUID_AMD_EXTFEAT, NULL, NULL, &cpuid_ecx, NULL);
-    if (!(cpuid_ecx & AMD_EXTFEAT_ECX_SVM)) {
-        return SYS_ERR_VMKIT_UNAVAIL;
-    }
-
-    // check whether SVM support is deactivated
-    uint64_t msr_vmcr = rdmsr(MSR_AMD_VMCR);
-    if (msr_vmcr & AMD_VMCR_SVMDIS) {
-        return SYS_ERR_VMKIT_UNAVAIL;
-    }
-
-    // from here on we assume that SVM is avail and may be enabled
-
-    // check whether SVM is already enabled
-    uint64_t msr_efer = rdmsr(MSR_IA32_EFER);
-    if (msr_efer & IA32_EFER_SVME) {
-        // SVM is already enabled
-        return SYS_ERR_OK;
-    }
-    // enable SVM
-    addmsr(MSR_IA32_EFER, IA32_EFER_SVME);
-    // check whether SVM is now enabled
-    msr_efer = rdmsr(MSR_IA32_EFER);
-    if (msr_efer & IA32_EFER_SVME) {
-        // SVM enabled
-        // set the host save area
-        wrmsr(MSR_AMD_VM_HSAVE, mem_to_local_phys((lvaddr_t)host_save_area));
-        return SYS_ERR_OK;
-    } else {
-        printk(LOG_WARN, "VMKit: Unable to enable SVM although the hardware "
-               "claims to support it.\n");
-        return SYS_ERR_VMKIT_UNAVAIL;
-    }
-}
-
-static inline void
-vm_exec (struct dcb *dcb)
-{
-    lpaddr_t lpaddr = gen_phys_to_local_phys(dcb->guest_desc.ctrl.cap.u.frame.base);
-    struct guest_control *ctrl = (void *)local_phys_to_mem(lpaddr);
-    register uintptr_t rbx __asm("rbx") = ctrl->regs.rbx;
-    register uintptr_t rcx __asm("rcx") = ctrl->regs.rcx;
-    register uintptr_t rdx __asm("rdx") = ctrl->regs.rdx;
-    register uintptr_t rsi __asm("rsi") = ctrl->regs.rsi;
-    register uintptr_t rdi __asm("rdi") = ctrl->regs.rdi;
-    register uintptr_t r8  __asm("r8")  = ctrl->regs.r8;
-    register uintptr_t r9  __asm("r9")  = ctrl->regs.r9;
-    register uintptr_t r10 __asm("r10") = ctrl->regs.r10;
-    register uintptr_t r11 __asm("r11") = ctrl->regs.r11;
-    register uintptr_t r12 __asm("r12") = ctrl->regs.r12;
-    register uintptr_t r13 __asm("r13") = ctrl->regs.r13;
-    register uintptr_t r14 __asm("r14") = ctrl->regs.r14;
-    register uintptr_t r15 __asm("r15") = ctrl->regs.r15;
-#ifdef NDEBUG
-    register uintptr_t rbp __asm("rbp") = ctrl->regs.rbp;
-
-    __asm volatile ("sti\n\t"       // allow intr to happen inside the host
-                    "vmrun\n\t"     // execute the guest
-                    "cli\n\t"       // disable intr in the host again
-                    "stgi\n\t"      // enable the global intr flag
-        : "+r" (rbx), "+r" (rcx), "+r" (rdx), "+r" (rbp), "+r" (rsi), "+r" (rdi),
-          "+r" (r8), "+r" (r9), "+r" (r10), "+r" (r11), "+r" (r12), "+r" (r13),
-          "+r" (r14), "+r" (r15)
-        : "a" (dcb->guest_desc.vmcb.cap.u.frame.base)
-        : "memory");
-#else
-    static uintptr_t rbp, srbp;
-
-    rbp = ctrl->regs.rbp;
-
-    __asm volatile ("mov %%rbp, %[srbp]\n\t" :: [srbp] "m" (srbp));
-
-    __asm volatile ("mov %[nrbp], %%rbp\n\t"
-                    "sti\n\t"       // allow intr to happen inside the host
-                    "vmrun\n\t"     // execute the guest
-                    "cli\n\t"       // disable intr in the host again
-                    "stgi\n\t"      // enable the global intr flag
-                    "mov %%rbp, %[nrbp]\n\t"
-        : "+r" (rbx), "+r" (rcx), "+r" (rdx), [nrbp] "+m" (rbp),
-                    "+r" (rsi), "+r" (rdi), "+r" (r8), "+r" (r9), "+r" (r10),
-                    "+r" (r11), "+r" (r12), "+r" (r13), "+r" (r14), "+r" (r15)
-        : "a" (dcb->guest_desc.vmcb.cap.u.frame.base)
-        : "memory");
-
-    __asm volatile ("mov %[srbp], %%rbp\n\t"
-                    : [srbp] "+m" (srbp));
-#endif
-
-    ctrl->regs.rbx = rbx;
-    ctrl->regs.rcx = rcx;
-    ctrl->regs.rdx = rdx;
-    ctrl->regs.rbp = rbp;
-    ctrl->regs.rsi = rsi;
-    ctrl->regs.rdi = rdi;
-    ctrl->regs.r8 = r8;
-    ctrl->regs.r9 = r9;
-    ctrl->regs.r10 = r10;
-    ctrl->regs.r11 = r11;
-    ctrl->regs.r12 = r12;
-    ctrl->regs.r13 = r13;
-    ctrl->regs.r14 = r14;
-    ctrl->regs.r15 = r15;
-}
-
-static inline void
-vmload (lpaddr_t vmcb) {
-    assert(!"VMLOAD not supported instruction");
-   // __asm volatile ("vmload" : : "a" (vmcb) : "memory");
-}
-
-static inline void
-vmsave (lpaddr_t vmcb) {
-    assert(!"VMSAVE not supported instruction");
-   // __asm volatile ("vmsave" : : "a" (vmcb) : "memory");
-}
-
-static inline void
-vmkit_switch_to (struct dcb *dcb)
-{
-    assert(dcb != NULL);
-    assert(dcb->is_vm_guest);
-
-    // save the host state
-    vmsave(mem_to_local_phys((lvaddr_t)host_vmcb));
-    // load the guest state
-    vmload(gen_phys_to_local_phys(dcb->guest_desc.vmcb.cap.u.frame.base));
-}
-
-static inline void
-vmkit_switch_from (struct dcb *dcb)
-{
-    assert(dcb != NULL);
-    assert(dcb->is_vm_guest);
-
-    // save the guest state
-    vmsave(gen_phys_to_local_phys(dcb->guest_desc.vmcb.cap.u.frame.base));
-    // load the host state
-    vmload(mem_to_local_phys((lvaddr_t)host_vmcb));
-}
-
-void __attribute__ ((noreturn))
-vmkit_vmenter (struct dcb *dcb)
-{
-    lpaddr_t lpaddr = gen_phys_to_local_phys(dcb->guest_desc.ctrl.cap.u.frame.base);
-    struct guest_control *ctrl = (void *)local_phys_to_mem(lpaddr);
-
-    assert(dcb != NULL);
-    assert(dcb->vspace != 0);
-    assert(dcb->is_vm_guest);
-
-    lpaddr = gen_phys_to_local_phys(dcb->guest_desc.vmcb.cap.u.frame.base);
-    amd_vmcb_t vmcb;
-    amd_vmcb_initialize(&vmcb, (void *)local_phys_to_mem(lpaddr));
-
-    /* We need to set the page translation mode. If nested paging is disabled
-     * then we need to set the guest cr3 to the value of the domains vspace. If
-     * nested paging is enabled then we need to copy the domains vspace into the
-     * ncr3 field of the vmcb. */
-    if (amd_vmcb_np_rd(&vmcb).enable) {
-        amd_vmcb_ncr3_wr(&vmcb, dcb->vspace);
-    } else {
-        amd_vmcb_cr3_wr(&vmcb, dcb->vspace);
-    }
-
-    // Enter the guest
-    vmkit_switch_to(dcb);
-    vm_exec(dcb);
-    vmkit_switch_from(dcb);
-
-    // Here we exited the guest due to some intercept triggered a vm exit
-    // our state is automatically restored by SVM
-
-    uint64_t ec = amd_vmcb_exitcode_rd(&vmcb);
-    /* We treat exits due to pysical interrupts (INTR, NMI, SMI) specially since
-     * they need to be processed by the kernel interrupt service routines */
-    if (ec == VMEXIT_INTR || ec == VMEXIT_NMI || ec == VMEXIT_SMI) {
-
-        ctrl->num_vm_exits_without_monitor_invocation++;
-        // wait for interrupt will enable interrupts and therefore trigger their
-        // corresponding handlers (which may be the monitor)
-        wait_for_interrupt();
-    } else {
-        ctrl->num_vm_exits_with_monitor_invocation++;
-        /* the guest exited not due to an interrupt but some condition the
-         * monitor has to handle, therefore notify the monitor */
-
-        assert(dcb->is_vm_guest);
-
-        // disable the domain
-        scheduler_remove(dcb);
-
-        // call the monitor
-        errval_t err = lmp_deliver_notification(&dcb->guest_desc.monitor_ep.cap);
-        if (err_is_fail(err)) {
-            printk(LOG_ERR, "Unexpected error delivering VMM call");
-        }
-
-        // run the monitor
-        dispatch(dcb->guest_desc.monitor_ep.cap.u.endpoint.listener);
-    }
-}
index 2c004b1..e2dc6b3 100644 (file)
  */
 
 #include <multiboot.h>
+
+#ifdef __k1om__
+#include <target/k1om/offsets_target.h>
+#define KERNEL_STACK_SIZE K1OM_KERNEL_STACK_SIZE
+#define KERNEL_STACK k1om_kernel_stack
+#else
 #include <target/x86_64/offsets_target.h>
+#define KERNEL_STACK_SIZE X86_64_KERNEL_STACK_SIZE
+#define KERNEL_STACK x86_64_kernel_stack
+#endif
 
 /* The flags for the Multiboot header */
 #define MB_FLAGS (MULTIBOOT_HEADER_FLAG_MODS_PGALIGNED | MULTIBOOT_HEADER_FLAG_NEED_MEMINFO)
 
        .text
-       .globl  start, halt
+       .globl start, halt
+
        /* Multiboot header, 4-byte aligned */
        .align  4
        .long   MULTIBOOT_HEADER_MAGIC               /* magic */
@@ -28,7 +38,7 @@
 
 start:
        /* Initialize the stack pointer */
-       lea     (x86_64_kernel_stack + X86_64_KERNEL_STACK_SIZE)(%rip), %rsp
+       lea     (KERNEL_STACK + KERNEL_STACK_SIZE)(%rip), %rsp
 
        /* Reset EFLAGS */
        pushq   $0
index 90bf571..c8b124e 100644 (file)
 #include <barrelfish_kpi/syscalls.h>
 #include <barrelfish_kpi/capabilities.h>
 #include <barrelfish_kpi/lmp.h>
-#include <target/x86_64/offsets_target.h>
 #include <x86.h>
 #include <asmoffsets.h>
 
+#ifdef __k1om__
+#include <target/k1om/offsets_target.h>
+#define KERNEL_STACK_SIZE K1OM_KERNEL_STACK_SIZE
+#define KERNEL_STACK k1om_kernel_stack
+#define PHYS_TO_MEM_OFFSET 0xffffff0000000000
+#else
+#include <target/x86_64/offsets_target.h>
+#define KERNEL_STACK_SIZE X86_64_KERNEL_STACK_SIZE
+#define KERNEL_STACK x86_64_kernel_stack
+#define PHYS_TO_MEM_OFFSET 0xfffffe0000000000
+#endif
+
     .text
     .globl syscall_entry
 
@@ -59,7 +70,7 @@ syscall_entry:
        /* Load pointer to endpoint cap */
        shl     $OBJBITS_CTE, %rsi
        mov     OFFSETOF_CAP_CNODE_CNODE(%rdi), %rcx
-       mov     $0xfffffe0000000000, %rdi       // phys_to_mem()
+       mov     $PHYS_TO_MEM_OFFSET, %rdi       // phys_to_mem()
        add     %rdi, %rcx
        add     %rsi, %rcx
 
@@ -277,7 +288,7 @@ err_disabled:       // Target disabled
 
     /* Yield to target (call dispatch(target) in C) */
     mov %rsi, %rdi /* rdi = target DCB */
-    lea (x86_64_kernel_stack + X86_64_KERNEL_STACK_SIZE)(%rip), %rsp
+    lea (KERNEL_STACK + KERNEL_STACK_SIZE)(%rip), %rsp
     jmp dispatch /* no return */
 
 err_buflen:     /* Target's endpoint buffer is full */
@@ -289,7 +300,7 @@ err_buflen:     /* Target's endpoint buffer is full */
 
     /* Yield to target (call dispatch(target) in C) */
     mov %rsi, %rdi /* rdi = target DCB */
-    lea (x86_64_kernel_stack + X86_64_KERNEL_STACK_SIZE)(%rip), %rsp
+    lea (KERNEL_STACK + KERNEL_STACK_SIZE)(%rip), %rsp
     jmp dispatch /* no return */
 
 #ifdef CONFIG_SCHEDULER_RBED
@@ -304,7 +315,7 @@ lrpc_make_runnable:
        movq    %rsp, user_stack_save(%rip)
 
        /* Get kernel stack */
-       lea     (x86_64_kernel_stack + X86_64_KERNEL_STACK_SIZE)(%rip), %rsp
+       lea     (KERNEL_STACK + KERNEL_STACK_SIZE)(%rip), %rsp
 
        // Save complete register state
        pushq   %rdx
@@ -357,7 +368,7 @@ syscall_path:
         movq    %rsp, user_stack_save(%rip)
 
         /* Get kernel stack */
-        lea (x86_64_kernel_stack + X86_64_KERNEL_STACK_SIZE)(%rip), %rsp
+        lea (KERNEL_STACK + KERNEL_STACK_SIZE)(%rip), %rsp
 
         pushq   %rcx            /* Save user-space RIP */
         pushq   %r11            /* Save user-space RFLAGS */
index 974a3de..1096935 100644 (file)
 #include <irq.h>
 #include <x86.h>
 #include <dispatch.h>
+#ifdef __k1om__
+#include <target/k1om/barrelfish_kpi/cpu_target.h>
+#else
 #include <target/x86_64/barrelfish_kpi/cpu_target.h>
-
+#endif
 /**
  * \brief Reboots the system.
  *
@@ -179,6 +182,17 @@ void __attribute__ ((noreturn)) resume(arch_registers_state_t *state)
  */
 void __attribute__ ((noreturn)) wait_for_interrupt(void)
 {
+#ifdef __k1om__
+    __asm volatile("lea k1om_kernel_stack(%%rip), %%rsp\n\t"
+                   "addq %[stack_size], %%rsp\n\t"
+                   "sti                 \n\t"
+                   // The instruction right after STI is still in interrupt
+                   // shadow. To avoid unecessary calls to HLT we insert a nop
+                   // to make sure pending interrupts are handeled immediately.
+                   "nop                 \n\t"
+                   "hlt                 \n\t"
+                   :: [stack_size] "i" (K1OM_KERNEL_STACK_SIZE) : "rsp" );
+#else
     __asm volatile("lea x86_64_kernel_stack(%%rip), %%rsp\n\t"
                    "addq %[stack_size], %%rsp\n\t"
                    "sti                 \n\t"
@@ -188,6 +202,7 @@ void __attribute__ ((noreturn)) wait_for_interrupt(void)
                    "nop                 \n\t"
                    "hlt                 \n\t"
                    :: [stack_size] "i" (X86_64_KERNEL_STACK_SIZE) : "rsp" );
+#endif
     panic("hlt should not return");
 }
 
index 7d43af8..3441ac3 100644 (file)
 #include <arch_gdb_stub.h>
 #include <barrelfish_kpi/cpu.h>
 
+#ifdef __k1om__
+#define KERNEL_STACK_SIZE K1OM_KERNEL_STACK_SIZE
+#define MEMORY_OFFSET K1OM_MEMORY_OFFSET
+#define KERNEL_STACK k1om_kernel_stack
+#else
+#define KERNEL_STACK_SIZE X86_64_KERNEL_STACK_SIZE
+#define MEMORY_OFFSET X86_64_MEMORY_OFFSET
+#define KERNEL_STACK x86_64_kernel_stack
+#endif
+
 /** \brief GDB register save area / frame.
  *
  * Stores pointer to current save frame used by GDB. Used to read/modify
@@ -27,9 +37,9 @@
 uintptr_t *gdb_arch_registers;
 
 /** \brief Separate stack area for the stub to run on */
-static uintptr_t gdb_stack[X86_64_KERNEL_STACK_SIZE/sizeof(uintptr_t)];
+static uintptr_t gdb_stack[KERNEL_STACK_SIZE/sizeof(uintptr_t)];
 /** \brief Pointer to top of GDB stack area. */
-uintptr_t * SNT gdb_stack_top = &gdb_stack[X86_64_KERNEL_STACK_SIZE/sizeof(uintptr_t)];
+uintptr_t * SNT gdb_stack_top = &gdb_stack[KERNEL_STACK_SIZE/sizeof(uintptr_t)];
 
 /** \brief Converts exception vector to signal number.
  *
@@ -90,9 +100,9 @@ void gdb_handle_exception_onstack(int vector, uintptr_t * NONNULL
 
     /* while we're checking the stack pointer, sanity check that it's
      * within the normal kernel stack region */
-    } else if (save_area[GDB_X86_64_RSP_REG] < (lvaddr_t)&x86_64_kernel_stack ||
-              save_area[GDB_X86_64_RSP_REG] > (lvaddr_t)&x86_64_kernel_stack +
-               X86_64_KERNEL_STACK_SIZE) {
+    } else if (save_area[GDB_X86_64_RSP_REG] < (lvaddr_t)&KERNEL_STACK ||
+              save_area[GDB_X86_64_RSP_REG] > (lvaddr_t)&KERNEL_STACK +
+               KERNEL_STACK_SIZE) {
         printk(LOG_WARN, "BIG FAT WARNING: kernel stack pointer (0x%lx) is "
                "invalid!\n", save_area[GDB_X86_64_RSP_REG]);
         printk(LOG_WARN, "Boldly attempting to continue into GDB anyway...\n");
@@ -234,7 +244,7 @@ static int ensure_mapping(lvaddr_t addr)
     }
 
     /* if address is outside "physical" memory region, fail the access */
-    if (addr < X86_64_MEMORY_OFFSET) {
+    if (addr < MEMORY_OFFSET) {
         return -1;
     }
 
index 73ac2f3..9c576a9 100644 (file)
 #  include <fpu.h>
 #endif
 
+#ifdef __k1om__
+#  define START_KERNEL_PHYS K1OM_START_KERNEL_PHYS
+#else
+#  define START_KERNEL_PHYS X86_64_START_KERNEL_PHYS
+#endif
+
 
 /**
  * \brief Define IRQ handler number 'num'.
@@ -591,7 +597,7 @@ static __attribute__ ((used,noreturn))
     uintptr_t rip = gdb_save_frame[GDB_X86_64_RIP_REG];
     printf("Faulting instruction pointer (or next instruction): 0x%lx\n", rip);
     printf("  => i.e. unrelocated kernel address 0x%lx\n",
-           rip - (uintptr_t)&_start_kernel + X86_64_START_KERNEL_PHYS);
+           rip - (uintptr_t)&_start_kernel + START_KERNEL_PHYS);
 
     printf("Registers:\n");
     printf(" rax: 0x%016lx  r8 : 0x%016lx\n",
index 6d1ceb1..b82dfb3 100644 (file)
 #include <barrelfish_kpi/init.h>
 #include <cap_predicates.h>
 
+#ifdef __k1om__
+#include <target/k1om/offsets_target.h>
+#define MEMORY_OFFSET K1OM_MEMORY_OFFSET
+#else
+#include <target/x86_64/offsets_target.h>
+#define MEMORY_OFFSET X86_64_MEMORY_OFFSET
+#endif
+
 static inline struct cte *cte_for_cap(struct capability *cap)
 {
     return (struct cte *) (cap - offsetof(struct cte, cap));
@@ -53,7 +61,7 @@ static errval_t x86_64_non_ptable(struct capability *dest, cslot_t slot,
                 printf("src type invalid\n");
                 return SYS_ERR_WRONG_MAPPING;
             }
-            if(slot >= X86_64_PML4_BASE(X86_64_MEMORY_OFFSET)) { // Kernel mapped here
+            if(slot >= X86_64_PML4_BASE(MEMORY_OFFSET)) { // Kernel mapped here
                 return SYS_ERR_VNODE_SLOT_RESERVED;
             }
             break;
index 2a4ad67..832dc4c 100644 (file)
 #include <kernel.h>
 #include <paging_kernel_arch.h>
 
+#ifdef __k1om__
+#include <xeon_phi.h>
+#define PADDR_SPACE_LIMIT K1OM_PADDR_SPACE_LIMIT
+#define PTABLE_GLOBAL_PAGE_BIT 0
+#define MEMORY_OFFSET K1OM_MEMORY_OFFSET
+#define KERNEL_INIT_MEMORY K1OM_KERNEL_INIT_MEMORY
+#else
+#define PADDR_SPACE_LIMIT X86_64_PADDR_SPACE_LIMIT
+#define PTABLE_GLOBAL_PAGE_BIT X86_64_PTABLE_GLOBAL_PAGE
+#define MEMORY_OFFSET X86_64_MEMORY_OFFSET
+#define KERNEL_INIT_MEMORY X86_64_KERNEL_INIT_MEMORY
+#endif
+
 /*
  * Table requirements for various address spaces.
  */
-#define MEM_PDPT_SIZE           X86_64_PDPT_ENTRIES(X86_64_PADDR_SPACE_LIMIT)
-#define MEM_PDIR_SIZE           X86_64_PDIR_ENTRIES(X86_64_PADDR_SPACE_LIMIT)
+#define MEM_PDPT_SIZE           X86_64_PDPT_ENTRIES(PADDR_SPACE_LIMIT)
+#define MEM_PDIR_SIZE           X86_64_PDIR_ENTRIES(PADDR_SPACE_LIMIT)
 
 /*
  * Page attribute bitmaps for various address spaces.
  */
 #define MEM_PAGE_BITMAP                                 \
     (X86_64_PTABLE_PRESENT | X86_64_PTABLE_READ_WRITE | \
-     X86_64_PTABLE_GLOBAL_PAGE)
+     PTABLE_GLOBAL_PAGE_BIT)
 #define DEVICE_PAGE_BITMAP                                      \
     (X86_64_PTABLE_PRESENT | X86_64_PTABLE_READ_WRITE |         \
-     X86_64_PTABLE_CACHE_DISABLED | X86_64_PTABLE_GLOBAL_PAGE)
+     X86_64_PTABLE_CACHE_DISABLED | PTABLE_GLOBAL_PAGE_BIT)
 
 /**
  * Kernel page map level 4 table.
@@ -102,8 +115,8 @@ static int paging_map_mem(lpaddr_t base, size_t size, uint64_t bitmap)
     paging_align(&vbase, &base, &size, X86_64_MEM_PAGE_SIZE);
 
     // Is mapped region out of range?
-    assert(base + size <= (lpaddr_t)X86_64_PADDR_SPACE_LIMIT);
-    if(base + size > (lpaddr_t)X86_64_PADDR_SPACE_LIMIT) {
+    assert(base + size <= (lpaddr_t)PADDR_SPACE_LIMIT);
+    if(base + size > (lpaddr_t)PADDR_SPACE_LIMIT) {
         return -1;
     }
 
@@ -160,9 +173,16 @@ void paging_x86_64_reset(void)
     }
 
     // Map an initial amount of memory
-    if(paging_x86_64_map_memory(0, X86_64_KERNEL_INIT_MEMORY) != 0) {
+    if(paging_x86_64_map_memory(0, KERNEL_INIT_MEMORY) != 0) {
+        panic("error while mapping physical memory!");
+    }
+    
+#ifdef __k1om__
+    /* mapping the Xeon Phi SBOX registers to provide serial input */
+    if (paging_x86_64_map_memory(XEON_PHI_SBOX_BASE, XEON_PHI_SBOX_SIZE) != 0) {
         panic("error while mapping physical memory!");
     }
+#endif
 
     // Switch to new page layout
     paging_x86_64_context_switch(mem_to_local_phys((lvaddr_t)pml4));
@@ -188,7 +208,7 @@ void paging_x86_64_make_good_pml4(lpaddr_t base)
     debug(SUBSYS_PAGING, "Is now a PML4: table = 0x%"PRIxLPADDR"\n", base);
 
     // Map memory
-    for(i = X86_64_PML4_BASE(X86_64_MEMORY_OFFSET); i < X86_64_PTABLE_SIZE; i++) {
+    for(i = X86_64_PML4_BASE(MEMORY_OFFSET); i < X86_64_PTABLE_SIZE; i++) {
         newpml4[i] = pml4[i];
     }
 }
index 3e4be34..9aaa397 100644 (file)
@@ -216,20 +216,6 @@ static void create_phys_caps(lpaddr_t init_alloc_addr)
     assert(last_end_addr != 0);
 
     if (last_end_addr < X86_64_PADDR_SPACE_SIZE) {
-#if 0
-        /*
-         * FIXME: adding the full range results in too many caps to add
-         * to the cnode (and we can't handle such big caps in user-space
-         * yet anyway) so instead we limit it to something much smaller
-         */
-        size_t size = X86_64_PADDR_SPACE_SIZE - last_end_addr;
-        const lpaddr_t phys_region_limit = 1UL << 48; // PCI implementation limit
-        if (last_end_addr > phys_region_limit) {
-            size = 0; // end of RAM is already too high!
-        } else if (last_end_addr + size > phys_region_limit) {
-            size = phys_region_limit - last_end_addr;
-        }
-#endif
         /*
          * XXX: with the new machines and the Xeon Phi we need to extend
          *      this range to the full 48bit physical address range
index d7afcd4..a00ef46 100644 (file)
 #include <paging_kernel_arch.h>
 #include <paging_generic.h>
 #include <exec.h>
+#include <fpu.h>
 #include <arch/x86/x86.h>
 #include <arch/x86/apic.h>
 #include <arch/x86/global.h>
 #include <arch/x86/perfmon.h>
-#include <vmkit.h>
-#include <barrelfish_kpi/sys_debug.h>
-#include <barrelfish_kpi/lmp.h>
-#include <barrelfish_kpi/dispatcher_shared_target.h>
 #include <arch/x86/debugregs.h>
-#include <trace/trace.h>
 #include <arch/x86/syscall.h>
 #include <arch/x86/timing.h>
-#include <fpu.h>
 #include <arch/x86/ipi_notify.h>
+#include <barrelfish_kpi/sys_debug.h>
+#include <barrelfish_kpi/lmp.h>
+#include <barrelfish_kpi/dispatcher_shared_target.h>
+#include <trace/trace.h>
+#ifndef __k1om__
+#include <vmkit.h>
 #include <amd_vmcb_dev.h>
+#endif
 
 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
 
@@ -82,7 +84,7 @@ static struct sysret handle_retype_common(struct capability *root,
     uint64_t source_cptr     = args[0];
     uint64_t type            = args[1];
     uint64_t objbits         = args[2];
-    uint64_t  dest_cnode_cptr = args[3];
+    uint64_t dest_cnode_cptr = args[3];
     uint64_t dest_slot       = args[4];
     uint64_t dest_vbits      = args[5];
 
@@ -158,7 +160,7 @@ static struct sysret handle_map(struct capability *ptable,
 
     TRACE(KERNEL, SC_MAP, 0);
     struct sysret sr = sys_map(ptable, slot, source_cptr, source_vbits, flags, offset,
-                   pte_count);
+                                  pte_count);
     TRACE(KERNEL, SC_MAP, 1);
     return sr;
 }
@@ -180,7 +182,7 @@ static struct sysret handle_delete_common(struct capability *root,
                                    bool from_monitor)
 {
     capaddr_t cptr = args[0];
-    int bits     = args[1];
+    int bits       = args[1];
 
     TRACE(KERNEL, SC_DELETE, 0);
     struct sysret sr = sys_delete(root, cptr, bits, from_monitor);
@@ -201,7 +203,7 @@ static struct sysret handle_revoke_common(struct capability *root,
                                           bool from_monitor)
 {
     capaddr_t cptr = args[0];
-    int bits     = args[1];
+    int bits       = args[1];
 
     TRACE(KERNEL, SC_REVOKE, 0);
     struct sysret sr = sys_revoke(root, cptr, bits, from_monitor);
@@ -342,7 +344,8 @@ static struct sysret monitor_identify_cap_common(struct capability *kernel_cap,
                                                  uintptr_t *args)
 {
     capaddr_t cptr = args[0];
-    uint8_t bits = args[1];
+    uint8_t bits   = args[1];
+    
     struct capability *retbuf = (void *)args[2];
 
     return sys_monitor_identify_cap(root, cptr, bits, retbuf);
@@ -411,8 +414,8 @@ static struct sysret monitor_create_cap(struct capability *kernel_cap,
 
     /* Create the cap in the destination */
     capaddr_t cnode_cptr = args[pos];
-    int cnode_vbits    = args[pos + 1];
-    size_t slot        = args[pos + 2];
+    int cnode_vbits      = args[pos + 1];
+    size_t slot          = args[pos + 2];
 
     return SYSRET(caps_create_from_existing(&dcb_current->cspace.cap,
                                             cnode_cptr, cnode_vbits,
@@ -423,7 +426,7 @@ static struct sysret monitor_nullify_cap(struct capability *kernel_cap,
                                          int cmd, uintptr_t *args)
 {
     capaddr_t cptr = args[0];
-    uint8_t bits = args[1];
+    uint8_t bits   = args[1];
 
     return sys_monitor_nullify_cap(cptr, bits);
 }
@@ -463,7 +466,6 @@ static struct sysret monitor_handle_sync_timer(struct capability *kern_cap,
     return sys_monitor_handle_sync_timer(synctime);
 }
 
-
 static struct sysret handle_frame_identify(struct capability *to,
                                            int cmd, uintptr_t *args)
 {
@@ -505,6 +507,7 @@ static struct sysret handle_io(struct capability *to, int cmd, uintptr_t *args)
     return sys_io(to, cmd, port, data);
 }
 
+#ifndef __k1om__
 static struct sysret
 handle_dispatcher_setup_guest (struct capability *to, int cmd, uintptr_t *args)
 {
@@ -593,6 +596,7 @@ handle_dispatcher_setup_guest (struct capability *to, int cmd, uintptr_t *args)
 
     return SYSRET(SYS_ERR_OK);
 }
+#endif
 
 static struct sysret monitor_handle_domain_id(struct capability *monitor_cap,
                                               int cmd, uintptr_t *args)
@@ -857,7 +861,9 @@ static invocation_handler_t invocations[ObjType_Num][CAP_MAX_CMD] = {
     [ObjType_Dispatcher] = {
         [DispatcherCmd_Setup] = handle_dispatcher_setup,
         [DispatcherCmd_Properties] = handle_dispatcher_properties,
+#ifndef __k1om__
         [DispatcherCmd_SetupGuest] = handle_dispatcher_setup_guest,
+#endif
         [DispatcherCmd_DumpPTables]  = dispatcher_dump_ptables,
     },
     [ObjType_KernelControlBlock] = {
@@ -1038,13 +1044,13 @@ struct sysret sys_syscall(uint64_t syscall, uint64_t arg0, uint64_t arg1,
                  panic("Dispatcher needs to be enabled for this invocation");
                }
 
-                // save calling dispatcher's registers, so that when the dispatcher
-                // next runs, it has a valid state in the relevant save area.
-                // Save RIP, RFLAGS, RSP and set RAX (return value) for later resume
-                save_area->rax = retval.error; // XXX: x86 1st return register
-                save_area->rip = rip;
-                save_area->eflags = rflags;
-                save_area->rsp = user_stack_save;
+               // save calling dispatcher's registers, so that when the dispatcher
+               // next runs, it has a valid state in the relevant save area.
+               // Save RIP, RFLAGS, RSP and set RAX (return value) for later resume
+               save_area->rax = retval.error; // XXX: x86 1st return register
+               save_area->rip = rip;
+               save_area->eflags = rflags;
+               save_area->rsp = user_stack_save;
 
                if(!dcb_current->is_vm_guest) {
                  /* save and zero FS/GS selectors (they're unmodified by the syscall path) */
@@ -1059,11 +1065,15 @@ struct sysret sys_syscall(uint64_t syscall, uint64_t arg0, uint64_t arg1,
                         [zero] "r" (0)
                         );
                } else {
+#ifndef __k1om__
                  lpaddr_t lpaddr = gen_phys_to_local_phys(dcb_current->guest_desc.vmcb.cap.u.frame.base);
                  amd_vmcb_t vmcb;
                  amd_vmcb_initialize(&vmcb, (void *)local_phys_to_mem(lpaddr));
                  save_area->fs = amd_vmcb_fs_selector_rd(&vmcb);
                  save_area->gs = amd_vmcb_gs_selector_rd(&vmcb);
+#else
+          panic("VM Guests not supported on Xeon Phi");
+#endif
                }
 
                 dispatch(to->u.endpoint.listener);
index ee9c32e..e05b147 100644 (file)
@@ -505,17 +505,11 @@ static errval_t caps_create(enum objtype type, lpaddr_t lpaddr, uint8_t bits,
             src_cap.u.vnode_x86_64_pml4.base =
                 genpaddr + i * ((genpaddr_t)1 << objbits_vnode);
 
-#ifdef __k1om__
-            lpaddr_t var = gen_phys_to_local_phys(src_cap.u.vnode_x86_64_pml4.base);
-                        paging_k1om_make_good_pml4(var);
-#else
-#ifdef __x86_64__
+#if defined(__x86_64__) || defined(__k1om__)
             // Make it a good PML4 by inserting kernel/mem VSpaces
-            lpaddr_t var =
-                gen_phys_to_local_phys(src_cap.u.vnode_x86_64_pml4.base);
+            lpaddr_t var = gen_phys_to_local_phys(src_cap.u.vnode_x86_64_pml4.base);
             paging_x86_64_make_good_pml4(var);
 #endif
-#endif
 
             // Insert the capability
             err = set_cap(&dest_caps[i].cap, &src_cap);
index 84d60f9..b5f1d75 100644 (file)
 #ifndef KERNEL_ARCH_K1OM_PAGING_H
 #define KERNEL_ARCH_K1OM_PAGING_H
 
-#include <target/k1om/paging_kernel_target.h>
-#include <paging_kernel_helper.h>
-
-/** Physical memory page size is 2 MBytes */
-#define X86_64_MEM_PAGE_SIZE            X86_64_LARGE_PAGE_SIZE
-
-/** Mask for physical memory page */
-#define X86_64_MEM_PAGE_MASK            0x1fffff
-
-/**
- * Resolves to required number of entries in page directory pointer table to map
- * 'limit' number of bytes.
- */
-#define X86_64_PDPT_ENTRIES(limit)     (X86_64_PML4_BASE((limit) - 1) + 1)
-
-/**
- * Resolves to required number of entries in page directory to map 'limit'
- * number of bytes.
- */
-#define X86_64_PDIR_ENTRIES(limit)     (X86_64_PDPT_BASE((limit) - 1) + 1)
-
-/**
- * Resolves to required number of entries in page table to map 'limit' number
- * of bytes.
- */
-#define X86_64_PTABLE_ENTRIES(limit)   (X86_64_PDIR_BASE((limit) - 1) + 1)
-
-/**
- * \brief Switch context.
- *
- * Assigns given physical base address to the CR3 register,
- * effectively switching context to new address space. Be
- * cautious that you only switch to "good" page tables.
- *
- * \param addr  Physical base address of page table.
- */
-static void inline paging_context_switch(lpaddr_t addr)
-{
-    paging_k1om_context_switch(addr);
-}
-
-static lvaddr_t inline paging_map_device(lpaddr_t base, size_t size)
-{
-    return paging_k1om_map_device(base, size);
-}
-
-static inline bool is_root_pt(enum objtype type) {
-    return type == ObjType_VNode_x86_64_pml4;
-}
-
-static inline size_t get_pte_size(void) {
-    return sizeof(union x86_64_ptable_entry);
-}
-
-static inline void do_one_tlb_flush(genvaddr_t vaddr)
-{
-    __asm__ __volatile__("invlpg %0" : : "m" (*(char *)vaddr));
-}
-
-static inline void do_selective_tlb_flush(genvaddr_t vaddr, genvaddr_t vend)
-{
-    for (genvaddr_t addr = vaddr; addr < vend; addr += X86_64_BASE_PAGE_SIZE) {
-        __asm__ __volatile__("invlpg %0" : : "m" (*(char *)addr));
-    }
-}
-
-static inline void do_full_tlb_flush(void) {
-    // XXX: FIXME: Going to reload cr3 to flush the entire TLB.
-    // This is inefficient.
-    // The current implementation is also not multicore safe.
-    // We should only invalidate the affected entry using invlpg
-    // and figure out which remote tlbs to flush.
-    uint64_t cr3;
-    __asm__ __volatile__("mov %%cr3,%0" : "=a" (cr3) : );
-    __asm__ __volatile__("mov %0,%%cr3" :  : "a" (cr3));
-}
-
+#include <arch/x86_64/paging_kernel_arch.h>
 
 #endif // KERNEL_ARCH_X86_64_PAGING_H
index 9031058..7fa932b 100644 (file)
 #ifndef KERNEL_TARGET_K1OM_OFFSETS_H
 #define KERNEL_TARGET_K1OM_OFFSETS_H
 
+
+
 /**
  * Absolute size of virtual address space. This is 48-bit on x86-64
  * currently, which equals 256 TBytes and allows for 512 PML4 slots,
  * each of which can map 512 GBytes.
  */
-#define K1OM_VADDR_SPACE_SIZE        ((genpaddr_t)1 << 48)
+#define K1OM_VADDR_SPACE_BITS 48
+#define K1OM_VADDR_SPACE_SIZE        ((genpaddr_t)1 << K1OM_VADDR_SPACE_BITS)
 
 /**
  * Absolute size of physical address space. This is also 40-bit.
@@ -48,7 +51,8 @@
  *
  * The Intel® Xeon Phi™ coprocessor supports 40-bit physical address in 64-bit.
  */
-#define K1OM_PADDR_SPACE_SIZE        ((genpaddr_t)1 << 40)
+#define X1OM_PADDR_SPACE_BITS 40
+#define K1OM_PADDR_SPACE_SIZE        ((genpaddr_t)1 << X1OM_PADDR_SPACE_BITS)
 
 /**
  * Start address of kernel image in physical memory. This is passed to
@@ -61,6 +65,8 @@
  */
 #define K1OM_KERNEL_STACK_SIZE       0x4000
 
+
+
 /**
  * Maximum physical address space mappable by the kernel.  Adjust this
  * for a bigger physical address space.  We set this to 40-bit,
diff --git a/kernel/include/target/k1om/paging_kernel_target.h b/kernel/include/target/k1om/paging_kernel_target.h
deleted file mode 100644 (file)
index 5a57b08..0000000
+++ /dev/null
@@ -1,330 +0,0 @@
-/**
- * \file
- * \brief x86-64 kernel page-table structures.
- */
-
-/*
- * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#ifndef KERNEL_TARGET_K1OM_PAGING_H
-#define KERNEL_TARGET_K1OM_PAGING_H
-
-#include <capabilities.h>
-#include <barrelfish_kpi/paging_arch.h>
-
-// Functions defined elsewhere. Move the declerations to appropriate includes
-int paging_k1om_map_memory(lpaddr_t base, size_t size);
-lvaddr_t paging_k1om_map_device(lpaddr_t base, size_t size);
-void paging_k1om_reset(void);
-void paging_k1om_make_good_pml4(lpaddr_t base);
-
-/// All flags valid for page access protection from user-space
-#define X86_64_PTABLE_ACCESS_MASK \
-    (X86_64_PTABLE_EXECUTE_DISABLE | X86_64_PTABLE_USER_SUPERVISOR | \
-     X86_64_PTABLE_READ_WRITE)
-
-/// All arch-specific flags valid to be set from user-space
-#define X86_64_PTABLE_FLAGS_MASK                                        \
-    (X86_64_PTABLE_ATTR_INDEX |             \
-     X86_64_PTABLE_DIRTY | X86_64_PTABLE_ACCESSED |                     \
-     X86_64_PTABLE_CACHE_DISABLED | X86_64_PTABLE_WRITE_THROUGH)
-
-/// Mask out all arch-specific flags except those valid from user-space
-#define X86_64_PTABLE_FLAGS(flags)     (flags & X86_64_PTABLE_FLAGS_MASK)
-
-/// Mask out all flags except those for access protection
-#define X86_64_PTABLE_ACCESS(flags)    (flags & X86_64_PTABLE_ACCESS_MASK)
-
-/** True if page entry is present in memory */
-#define X86_64_IS_PRESENT(entry)                        \
-    ((*(uint64_t *)(entry)) & X86_64_PTABLE_PRESENT)
-
-/**
- * A page directory entry.
- */
-union x86_64_pdir_entry {
-    uint64_t raw;
-    struct {
-        uint64_t        present         :1;
-        uint64_t        read_write      :1;
-        uint64_t        user_supervisor :1;
-        uint64_t        write_through   :1;
-        uint64_t        cache_disabled  :1;
-        uint64_t        accessed        :1;
-        uint64_t        reserved        :3;
-        uint64_t        available       :3;
-        uint64_t        base_addr       :28;
-        uint64_t        reserved2       :12;
-        uint64_t        available2      :11;
-        uint64_t        execute_disable :1;
-    } d;
-};
-
-/**
- * A page table entry.
- */
-union x86_64_ptable_entry {
-    uint64_t raw;
-    struct {
-        uint64_t        present         :1;
-        uint64_t        read_write      :1;
-        uint64_t        user_supervisor :1;
-        uint64_t        write_through   :1;
-        uint64_t        cache_disabled  :1;
-        uint64_t        accessed        :1;
-        uint64_t        dirty           :1;
-        uint64_t        always1         :1;
-        uint64_t        global          :1;
-        uint64_t        available       :3;
-        uint64_t        attr_index      :1;
-        uint64_t        reserved        :8;
-        uint64_t        base_addr       :19;
-        uint64_t        reserved2       :12;
-        uint64_t        available2      :11;
-        uint64_t        execute_disable :1;
-    } large;
-    struct {
-        uint64_t        present         :1;
-        uint64_t        read_write      :1;
-        uint64_t        user_supervisor :1;
-        uint64_t        write_through   :1;
-        uint64_t        cache_disabled  :1;
-        uint64_t        accessed        :1;
-        uint64_t        dirty           :1;
-        uint64_t        attr_index      :1;
-        uint64_t        global          :1;
-        uint64_t        available       :3;
-        uint64_t        base_addr       :28;
-        uint64_t        reserved2       :12;
-        uint64_t        available2      :11;
-        uint64_t        execute_disable :1;
-    } base;
-};
-
-/**
- * \brief Clear page directory.
- *
- * Clears page directory pointed to by 'p'.
- *
- * \param p     Pointer to page directory to clear.
- */
-static inline void paging_k1om_clear_pdir(union x86_64_pdir_entry * COUNT(X86_64_PTABLE_SIZE)
-                                            NONNULL p)
-{
-    for (int i = 0; i < X86_64_PTABLE_SIZE; i++) {
-        p[i].raw = X86_64_PTABLE_CLEAR;
-    }
-}
-
-/**
- * \brief Clear page table.
- *
- * Clears page table pointed to by 'p'.
- *
- * \param p     Pointer to page table to clear.
- */
-static inline void paging_k1om_clear_ptable(union x86_64_ptable_entry * COUNT(X86_64_PTABLE_SIZE)
-                                              NONNULL p)
-{
-    for (int i = 0; i < X86_64_PTABLE_SIZE; i++) {
-        p[i].raw = X86_64_PTABLE_CLEAR;
-    }
-}
-
-/**
- * \brief Maps from page directory entry to page directory/table.
- *
- * Maps page directory or table, based at 'base', from page directory entry
- * pointed to by 'entry'.
- *
- * \param entry Pointer to page directory entry to point from.
- * \param base  Base virtual address of page directory/table to point to.
- */
-static inline void paging_k1om_map_table(union x86_64_pdir_entry *entry,
-                                           lpaddr_t base)
-{
-    union x86_64_pdir_entry tmp;
-    tmp.raw = X86_64_PTABLE_CLEAR;
-    tmp.d.present = 1;
-    tmp.d.read_write = 1;
-    tmp.d.user_supervisor = 1;
-    tmp.d.base_addr = base >> 12;
-
-    *entry = tmp;
-}
-
-/**
- * \brief Maps a large page.
- *
- * From large page table entry, pointed to by 'entry', maps physical address
- * 'base' with page attribute bitmap 'bitmap'.
- *
- * \param entry         Pointer to page table entry to map from.
- * \param base          Physical address to map to (will be page-aligned).
- * \param bitmap        Bitmap to apply to page attributes.
- */
-static inline void paging_k1om_map_large(union x86_64_ptable_entry *entry,
-                                           lpaddr_t base, uint64_t bitmap)
-{
-    union x86_64_ptable_entry tmp;
-    tmp.raw = X86_64_PTABLE_CLEAR;
-
-    tmp.large.present = bitmap & X86_64_PTABLE_PRESENT ? 1 : 0;
-    tmp.large.read_write = bitmap & X86_64_PTABLE_READ_WRITE ? 1 : 0;
-    tmp.large.user_supervisor = bitmap & X86_64_PTABLE_USER_SUPERVISOR ? 1 : 0;
-    tmp.large.write_through = bitmap & X86_64_PTABLE_WRITE_THROUGH ? 1 : 0;
-    tmp.large.cache_disabled = bitmap & X86_64_PTABLE_CACHE_DISABLED ? 1 : 0;
-    tmp.large.global = 0; // always zero
-    tmp.large.attr_index = bitmap & X86_64_PTABLE_ATTR_INDEX ? 1 : 0;
-    tmp.large.execute_disable = bitmap & X86_64_PTABLE_EXECUTE_DISABLE ? 1 : 0;
-    tmp.large.always1 = 1;
-    tmp.large.base_addr = base >> 21;
-
-    *entry = tmp;
-}
-
-/**
- * \brief Maps a normal (small) page.
- *
- * From small page table entry, pointed to by 'entry', maps physical address
- * 'base' with page attribute bitmap 'bitmap'.
- *
- * \param entry         Pointer to page table entry to map from.
- * \param base          Physical address to map to (will be page-aligned).
- * \param bitmap        Bitmap to apply to page attributes.
- */
-static inline void paging_k1om_map(union x86_64_ptable_entry * NONNULL entry,
-                                     lpaddr_t base, uint64_t bitmap)
-{
-    union x86_64_ptable_entry tmp;
-    tmp.raw = X86_64_PTABLE_CLEAR;
-
-    tmp.base.present = bitmap & X86_64_PTABLE_PRESENT ? 1 : 0;
-    tmp.base.read_write = bitmap & X86_64_PTABLE_READ_WRITE ? 1 : 0;
-    tmp.base.user_supervisor = bitmap & X86_64_PTABLE_USER_SUPERVISOR ? 1 : 0;
-    tmp.base.write_through = bitmap & X86_64_PTABLE_WRITE_THROUGH ? 1 : 0;
-    tmp.base.cache_disabled = bitmap & X86_64_PTABLE_CACHE_DISABLED ? 1 : 0;
-    tmp.base.attr_index = bitmap & X86_64_PTABLE_ATTR_INDEX ? 1 : 0;
-    tmp.base.global = 0; // always zero
-    tmp.base.execute_disable = bitmap & X86_64_PTABLE_EXECUTE_DISABLE ? 1 : 0;
-    tmp.base.base_addr = base >> 12;
-
-    *entry = tmp;
-}
-
-/**
- * \brief Modify flags of a normal (small) page.
- *
- * From small page table entry, pointed to by 'entry', maps physical address
- * 'base' with page attribute bitmap 'bitmap'.
- *
- * \param entry         Pointer to page table entry to map from.
- * \param bitmap        Bitmap to apply to page attributes.
- */
-static inline void paging_k1om_modify_flags(union x86_64_ptable_entry * NONNULL entry,
-                                              uint64_t bitmap)
-{
-    union x86_64_ptable_entry tmp = *entry;
-
-    tmp.base.present = bitmap & X86_64_PTABLE_PRESENT ? 1 : 0;
-    tmp.base.read_write = bitmap & X86_64_PTABLE_READ_WRITE ? 1 : 0;
-    tmp.base.user_supervisor = bitmap & X86_64_PTABLE_USER_SUPERVISOR ? 1 : 0;
-    tmp.base.write_through = bitmap & X86_64_PTABLE_WRITE_THROUGH ? 1 : 0;
-    tmp.base.cache_disabled = bitmap & X86_64_PTABLE_CACHE_DISABLED ? 1 : 0;
-    tmp.base.attr_index = bitmap & X86_64_PTABLE_ATTR_INDEX ? 1 : 0;
-    tmp.base.global =  0;
-    tmp.base.execute_disable = bitmap & X86_64_PTABLE_EXECUTE_DISABLE ? 1 : 0;
-
-    *entry = tmp;
-}
-
-static inline void paging_unmap(union x86_64_ptable_entry * NONNULL entry)
-{
-    entry->raw = X86_64_PTABLE_CLEAR;
-}
-
-/**
- * \brief Convert Capability access rights to X86-64 page flags.
- *
- * Returns corresponding X86-64 page flags to given capability access rights
- * mask 'rights'.
- *
- * \param rights        Capability rightsmask.
- *
- * \return X86-64 page flags.
- */
-static inline uint64_t paging_k1om_cap_to_page_flags(CapRights rights)
-{
-    uint64_t pageflags = 0;
-
-    // Sanity-check given flags
-    if(!(rights & CAPRIGHTS_READ) &&
-       (rights & CAPRIGHTS_WRITE || rights & CAPRIGHTS_EXECUTE)) {
-        printk(LOG_ERR, "Page mapped writable and/or executable, but not "
-               "readable. Impossible on X86! Will map non-everything "
-               "instead.\n");
-    }
-
-    // Convert flags
-    pageflags |= rights & CAPRIGHTS_READ ? X86_64_PTABLE_USER_SUPERVISOR : 0;
-    pageflags |= rights & CAPRIGHTS_WRITE ? X86_64_PTABLE_READ_WRITE : 0;
-    pageflags |= rights & CAPRIGHTS_EXECUTE ? 0 : X86_64_PTABLE_EXECUTE_DISABLE;
-
-    return pageflags;
-}
-
-/**
- * \brief Switch context.
- *
- * Assigns given physical base address of PML4 'pml4' to the CR3
- * register, effectively switching context to new address space. Be
- * cautious that you only switch to "good" (as explained in
- * paging_make_good_pml4()) PML4s!
- *
- * \param pml4  Physical base address of PML4 table.
- */
-static void inline paging_k1om_context_switch(lpaddr_t pml4)
-{
-    __asm volatile("mov %[pml4], %%cr3"
-                   : /* No output */
-                   :
-                   [pml4] "r" (pml4)
-                   );
-}
-
-/**
- * \brief Mask out page attributes.
- *
- * Masks out all attributes and access rights from 'attr' according to
- * 'mask'. This is architecture-specific. On x86-64, except for the
- * execute disable attribute, rights are given by setting a
- * corresponding bit. Thus, setting that bit within 'mask' to zero,
- * masks out the right. For the execute disable bit, the right is
- * masked out when the bit is set, so the mask works the other way
- * around in this case: When the bit is set in 'mask', but not set in
- * 'attr', it will be set in the return value, so mask-out behavior is
- * preserved.
- *
- * \param attr  The page attributes to mask.
- * \param mask  Mask for the page attributes.
- *
- * \return Masked version of 'attr'.
- */
-static inline uint64_t paging_k1om_mask_attrs(uint64_t attr, uint64_t mask)
-{
-    // First, mask out all "bit-sets-enabled" attributes
-    attr &= mask | X86_64_PTABLE_EXECUTE_DISABLE;
-
-    // Now, mask out all "bit-sets-disabled" attributes
-    attr |= mask & X86_64_PTABLE_EXECUTE_DISABLE;
-
-    return attr;
-}
-
-#endif // KERNEL_TARGET_K1OM_PAGING_H
index dcab060..c7030f2 100644 (file)
  * currently, which equals 256 TBytes and allows for 512 PML4 slots,
  * each of which can map 512 GBytes.
  */
-#define X86_64_VADDR_SPACE_SIZE        ((genpaddr_t)1 << 48)
+#define X86_64_VADDR_SPACE_BITS 48
+#define X86_64_VADDR_SPACE_SIZE        ((genpaddr_t)1 << X86_64_VADDR_SPACE_BITS)
 
 /**
  * Absolute size of physical address space. This is also 48-bit.
  */
-#define X86_64_PADDR_SPACE_SIZE        ((genpaddr_t)1 << 48)
+#define X86_64_PADDR_SPACE_BITS 48
+#define X86_64_PADDR_SPACE_SIZE        ((genpaddr_t)1 << X86_64_PADDR_SPACE_BITS)
 
 /**
  * Start address of kernel image in physical memory. This is passed to
index 805e7d3..93575e2 100644 (file)
@@ -30,10 +30,17 @@ void paging_x86_64_make_good_pml4(lpaddr_t base);
      X86_64_PTABLE_READ_WRITE)
 
 /// All arch-specific flags valid to be set from user-space
+#ifdef __k1om__
+#define X86_64_PTABLE_FLAGS_MASK                                        \
+    (X86_64_PTABLE_ATTR_INDEX | X86_64_PTABLE_DIRTY |                   \
+     X86_64_PTABLE_ACCESSED | X86_64_PTABLE_CACHE_DISABLED |            \
+     X86_64_PTABLE_WRITE_THROUGH)
+#else
 #define X86_64_PTABLE_FLAGS_MASK                                        \
     (X86_64_PTABLE_GLOBAL_PAGE | X86_64_PTABLE_ATTR_INDEX |             \
      X86_64_PTABLE_DIRTY | X86_64_PTABLE_ACCESSED |                     \
      X86_64_PTABLE_CACHE_DISABLED | X86_64_PTABLE_WRITE_THROUGH)
+#endif
 
 /// Mask out all arch-specific flags except those valid from user-space
 #define X86_64_PTABLE_FLAGS(flags)     (flags & X86_64_PTABLE_FLAGS_MASK)
@@ -66,6 +73,24 @@ union x86_64_pdir_entry {
     } d;
 };
 
+#ifdef __k1om__
+#define X86_64_PHYSADDR_BITS X1OM_PADDR_SPACE_BITS // TODO: Take that from offsets target
+#else
+#define X86_64_PHYSADDR_BITS X86_64_PADDR_SPACE_BITS
+#endif
+#define X86_64_PAGING_ENTRY_SIZE 64
+#define X86_64_PAGING_AVAIL2_BITS 11
+#define X86_64_PAGING_FLAG_BITS 12
+#define X86_64_PAGING_LARGE_FLAGE_BITS 21
+#define X86_64_PAGING_RESERVED_BITS \
+                (X86_64_PAGING_ENTRY_SIZE - X86_64_PHYSADDR_BITS - \
+                 X86_64_PAGING_AVAIL2_BITS - 1)
+#define X86_64_PAGING_LARGE_BASE_BITS \
+                (X86_64_PHYSADDR_BITS - X86_64_PAGING_LARGE_FLAGE_BITS)
+#define X86_64_PAGING_BASE_BASE_BITS \
+                (X86_64_PHYSADDR_BITS - X86_64_PAGING_FLAG_BITS)
+
+
 /**
  * A page table entry.
  */
@@ -84,8 +109,8 @@ union x86_64_ptable_entry {
         uint64_t        available       :3;
         uint64_t        attr_index      :1;
         uint64_t        reserved        :8;
-        uint64_t        base_addr       :27;
-        uint64_t        reserved2       :4;
+        uint64_t        base_addr       :X86_64_PAGING_LARGE_BASE_BITS;
+        uint64_t        reserved2       :X86_64_PAGING_RESERVED_BITS;
         uint64_t        available2      :11;
         uint64_t        execute_disable :1;
     } large;
@@ -100,13 +125,15 @@ union x86_64_ptable_entry {
         uint64_t        attr_index      :1;
         uint64_t        global          :1;
         uint64_t        available       :3;
-        uint64_t        base_addr       :36;
-        uint64_t        reserved2       :4;
+        uint64_t        base_addr       :X86_64_PAGING_BASE_BASE_BITS;
+        uint64_t        reserved2       :X86_64_PAGING_RESERVED_BITS;
         uint64_t        available2      :11;
         uint64_t        execute_disable :1;
     } base;
 };
 
+STATIC_ASSERT_SIZEOF(union x86_64_ptable_entry, (X86_64_PAGING_ENTRY_SIZE / 8));
+
 /**
  * \brief Clear page directory.
  *
@@ -181,7 +208,12 @@ static inline void paging_x86_64_map_large(union x86_64_ptable_entry *entry,
     tmp.large.user_supervisor = bitmap & X86_64_PTABLE_USER_SUPERVISOR ? 1 : 0;
     tmp.large.write_through = bitmap & X86_64_PTABLE_WRITE_THROUGH ? 1 : 0;
     tmp.large.cache_disabled = bitmap & X86_64_PTABLE_CACHE_DISABLED ? 1 : 0;
+#ifdef __k1om__
+       /* The Xeon Phi has no support for global pages */
+       tmp.large.global = 0;
+#else
     tmp.large.global = bitmap & X86_64_PTABLE_GLOBAL_PAGE ? 1 : 0;
+#endif
     tmp.large.attr_index = bitmap & X86_64_PTABLE_ATTR_INDEX ? 1 : 0;
     tmp.large.execute_disable = bitmap & X86_64_PTABLE_EXECUTE_DISABLE ? 1 : 0;
     tmp.large.always1 = 1;
@@ -212,7 +244,12 @@ static inline void paging_x86_64_map(union x86_64_ptable_entry * NONNULL entry,
     tmp.base.write_through = bitmap & X86_64_PTABLE_WRITE_THROUGH ? 1 : 0;
     tmp.base.cache_disabled = bitmap & X86_64_PTABLE_CACHE_DISABLED ? 1 : 0;
     tmp.base.attr_index = bitmap & X86_64_PTABLE_ATTR_INDEX ? 1 : 0;
+#ifdef __k1om__
+       /* The Xeon Phi has no support for global pages */
+       tmp.base.global = 0;
+#else
     tmp.base.global = bitmap & X86_64_PTABLE_GLOBAL_PAGE ? 1 : 0;
+#endif
     tmp.base.execute_disable = bitmap & X86_64_PTABLE_EXECUTE_DISABLE ? 1 : 0;
     tmp.base.base_addr = base >> 12;
 
@@ -239,7 +276,12 @@ static inline void paging_x86_64_modify_flags(union x86_64_ptable_entry * NONNUL
     tmp.base.write_through = bitmap & X86_64_PTABLE_WRITE_THROUGH ? 1 : 0;
     tmp.base.cache_disabled = bitmap & X86_64_PTABLE_CACHE_DISABLED ? 1 : 0;
     tmp.base.attr_index = bitmap & X86_64_PTABLE_ATTR_INDEX ? 1 : 0;
+#ifdef __k1om__
+       /* XXX: The Xeon Phi does no support global pages */
+       tmp.base.global =  0;
+#else
     tmp.base.global = bitmap & X86_64_PTABLE_GLOBAL_PAGE ? 1 : 0;
+#endif
     tmp.base.execute_disable = bitmap & X86_64_PTABLE_EXECUTE_DISABLE ? 1 : 0;
 
     *entry = tmp;