Changeset 29


Ignore:
Timestamp:
Jun 21, 2017, 8:35:30 AM (7 years ago)
Author:
max@…
Message:

Update. The kernel now enables the GDT/IDT, and has trap entries. A
x86_printf function is added for debugging purposes only. The new Makefile
will come in another commit.

Location:
trunk/hal/x86_64
Files:
4 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/hal/x86_64/hal_boot.S

    r25 r29  
    22 * hal_boot.S - Kernel boot entry point
    33 *
    4  * Author        Maxime Villard (2017)
    5  *               This code is inspired a lot from the NetBSD boot procedure,
    6  *               written by Maxime Villard too.
    7  *
    8  * Copyright (c) UPMC Sorbonne Universites
    9  *
    10  * This file is part of ALMOS-kernel.
    11  *
    12  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     4 * Copyright (c) 2017 Maxime Villard
     5 * This code is inspired a lot from the NetBSD boot procedure, written by
     6 * Maxime Villard too. XXX copyright
     7 *
     8 * This file is part of ALMOS-MKH.
     9 *
     10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    1311 * under the terms of the GNU General Public License as published by
    1412 * the Free Software Foundation; version 2.0 of the License.
    1513 *
    16  * ALMOS-kernel is distributed in the hope that it will be useful, but
     14 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1715 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1816 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    2018 *
    2119 * You should have received a copy of the GNU General Public License
    22  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     20 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
    2321 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2422 */
    2523
     24#define x86_ASM
    2625#include <hal_boot.h>
    2726#include <hal_register.h>
     
    99982:                                      ;
    10099
     100/*
     101 * killkpt - Destroy a kernel page table (long mode)
     102 *      rbx = page table address
     103 *      rcx = number of pages to destroy
     104 */
     105#define killkpt \
     1061:      movq    $0,(%rbx)       ; \
     107        addq    $PDE_SIZE,%rbx  ; \
     108        loop    1b              ;
     109
    101110/* 32bit version of PG_NX */
    102111#define PG_NX32 0x80000000
     
    106115        .globl  start_x86_64
    107116        .globl  L4paddr
     117        .globl  iom_base
    108118
    109119/*
     
    125135        .type   L4paddr, @object
    126136        L4paddr:        .quad   0       /* paddr of L4 */
     137
     138        .type   iom_base, @object
     139        iom_base:       .quad   0       /* virt. addr. of ISA I/O MEM */
     140
    127141
    128142#define GDT64_LIMIT gdt64_end-gdt64_start-1
     
    152166farjmp64:
    153167        .long   RELOC(longmode)
    154         .word   GSEL(GCODE_SEL, SEL_KPL)
     168        .word   GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)
    155169.align 64
    156170
     
    169183
    170184        /* Make sure it is a multiboot-compliant bootloader. */
    171         cmpl    $MULTIBOOT_INFO_MAGIC,%eax
    172         jne     boot_panic
     185//      cmpl    $MULTIBOOT_INFO_MAGIC,%eax
     186//      jne     boot_panic
    173187
    174188        movl    $RELOC(tmpstk),%esp
     
    246260        subl    %eax,%ecx
    247261        shrl    $PGSHIFT,%ecx
    248         orl     $(PG_V|PG_KR),%eax
     262        orl     $(PG_V|PG_KR|PG_G),%eax
    249263        fillkpt
    250264
     
    254268        subl    %eax,%ecx
    255269        shrl    $PGSHIFT,%ecx
    256         orl     $(PG_V|PG_KW),%eax
     270        orl     $(PG_V|PG_KW|PG_G),%eax
    257271        fillkpt_nox
    258272
     
    261275        movl    $TABLESIZE,%ecx         /* length of BOOTSTRAP TABLES */
    262276        shrl    $PGSHIFT,%ecx
    263         orl     $(PG_V|PG_KW),%eax
     277        orl     $(PG_V|PG_KW|PG_G),%eax
    264278        fillkpt_nox
    265279
     
    268282        movl    $IOM_SIZE,%ecx  /* size of ISA I/O MEM */
    269283        shrl    $PGSHIFT,%ecx
    270         orl     $(PG_V|PG_KW/*|PG_N*/),%eax
     284        orl     $(PG_V|PG_KW|PG_G),%eax
    271285        fillkpt_nox
    272286
     
    335349         */
    336350        movl    %cr4,%eax
    337         orl     $(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT),%eax
     351        orl     $(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT|CR4_PGE),%eax
    338352        movl    %eax,%cr4
    339353
     
    381395         */
    382396        movabsq $longmode_hi,%rax
    383         jmp     *%rax /* XXX XXX: <------ this instruction faults */
     397        jmp     *%rax
    384398
    385399longmode_hi:
     
    397411        movq    $KERNBASE,%r8
    398412
    399 
    400         /*
    401          * STOP HERE FOR NOW
    402          */
    403 1:      nop
    404         jmp     1b
    405 
    406 
    407 boot_panic:
    408         jmp     boot_panic
    409 
     413#if L2_SLOT_KERNBASE > 0
     414        movq    $(NKL2_KIMG_ENTRIES+1),%rcx
     415        leaq    (PROC0_PTP2_OFF)(%rsi),%rbx     /* old, phys address */
     416        addq    %r8,%rbx                        /* new, virt address */
     417        killkpt
     418#endif
     419
     420#if L3_SLOT_KERNBASE > 0
     421        movq    $NKL3_KIMG_ENTRIES,%rcx
     422        leaq    (PROC0_PTP3_OFF)(%rsi),%rbx     /* old, phys address */
     423        addq    %r8,%rbx                        /* new, virt address */
     424        killkpt
     425#endif
     426
     427        movq    $NKL4_KIMG_ENTRIES,%rcx
     428        leaq    (PROC0_PML4_OFF)(%rsi),%rbx     /* old, phys address of PML4 */
     429        addq    %r8,%rbx                        /* new, virt address of PML4 */
     430        killkpt
     431
     432        /* Relocate atdevbase. */
     433        movq    $(TABLESIZE+KERNBASE),%rdx
     434        addq    %rsi,%rdx
     435        movq    %rdx,iom_base(%rip)
     436
     437        /* Set up bootstrap stack. */
     438        leaq    (PROC0_STK_OFF)(%rsi),%rax
     439        addq    %r8,%rax
     440        leaq    (STKSIZE)(%rax),%rsp
     441        xorq    %rbp,%rbp                       /* mark end of frames */
     442
     443        xorw    %ax,%ax
     444        movw    %ax,%gs
     445        movw    %ax,%fs
     446
     447        /* The first physical page available. */
     448        leaq    (TABLESIZE)(%rsi),%rdi
     449
     450        call    init_x86_64
     451
  • trunk/hal/x86_64/hal_boot.h

    r25 r29  
    22 * hal_boot.h - General values used by the boot procedure
    33 *
    4  * Author        Maxime Villard (2017)
     4 * Copyright (c) 2017 Maxime Villard
     5 *
     6 * This file is part of ALMOS-MKH.
    57 *
    6  * Copyright (c) UPMC Sorbonne Universites
    7  *
    8  * This file is part of ALMOS-kernel.
    9  *
    10  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     8 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    119 * under the terms of the GNU General Public License as published by
    1210 * the Free Software Foundation; version 2.0 of the License.
    1311 *
    14  * ALMOS-kernel is distributed in the hope that it will be useful, but
     12 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1513 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1614 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1816 *
    1917 * You should have received a copy of the GNU General Public License
    20  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     18 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
    2119 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2220 */
     
    3634/* -------------------------------------------------------------------------- */
    3735
     36#define ASM_ALIGN_TEXT  .align 16
     37#define ASM_ENTRY(x) \
     38        .text; ASM_ALIGN_TEXT; .globl x; .type x,@function; x:
     39
     40/* -------------------------------------------------------------------------- */
     41
    3842#define PSL_MBO         0x00000002
    3943
    4044#define STKPAGES        4
     45#define STKSIZE         (PAGE_SIZE * STKPAGES)
    4146
    4247#define NKL4_KIMG_ENTRIES       1
     
    107112#define L4_SLOT_KERNBASE        511
    108113
     114/*
     115 * L3 = (KERNBASE % NBPD_L4) / NBPD_L3
     116 * L2 = (KERNBASE % NBPD_L3) / NBPD_L2
     117 */
     118#define L3_SLOT_KERNBASE        510
     119#define L2_SLOT_KERNBASE        0
     120
    109121#define PDIR_SLOT_KERN  L4_SLOT_KERN
    110122#define PDIR_SLOT_PTE   L4_SLOT_PTE
  • trunk/hal/x86_64/hal_kentry.S

    r25 r29  
    11/*
    2  * hal_kentry.S - exception/interrupt/syscall kernel entry point for MIPS32
     2 * hal_kentry.S - Entry points into the kernel (traps, interrupts, syscalls)
    33 *
    4  * AUthors   Ghassan Almaless (2007,2008,2009,2010,2011,2012)
    5  *           Mohamed Lamine Karaoui (2015)
    6  *           Alain Greiner (2017)
    7  *
    8  * Copyright (c) UPMC Sorbonne Universites
     4 * Copyright (c) 2017 Maxime Villard
    95 *
    106 * This file is part of ALMOS-MKH.
     
    2016 *
    2117 * You should have received a copy of the GNU General Public License
    22  * along with ALMOS-MKH; if not, write to the Free Software Foundation,
     18 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
    2319 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2420 */
    2521
     22#define x86_ASM
     23#include <hal_boot.h>
     24#include <hal_kentry.h>
    2625
    27 #---------------------------------------------------------------------------------
    28 # This code is the unique kernel entry point in case of exception, interrupt,
    29 # or syscall for the TSAR_MIPS32 architecture. 
    30 #
    31 # - If the core is in user mode:
    32 #   . we desactivate the MMU.
    33 #   . we save the context in the uzone of the calling thread descriptor.
    34 #   . we increment the cores_in_kernel variable.
    35 #   . we call the relevant exception/interrupt/syscall handler
    36 #
    37 # - If the core is already in kernel mode:
    38 #   . we save the context in the kernel stack
    39 #   . we call the relevant exception/interrupt/syscall handler
    40 #
    41 # - In both cases, when the handler returns:
    42 #   . we restore the context
    43 #   . we reactivate the MMU ???
    44 #---------------------------------------------------------------------------------
     26#define TRAPENTRY(a)    \
     27        pushq $(a);     \
     28        jmp alltraps;
    4529
    46         .section   .kentry,"ax",@progbits
    47         .extern    cpu_do_interrupt
    48         .extern    cpu_do_exception
    49         .extern    cpu_do_syscall
    50         .extern    cpu_kentry
    51         .extern    cpu_kexit
     30#define ZTRAPENTRY(a) \
     31        pushq $0; \
     32        TRAPENTRY(a)
    5233
    53         .global    kentry
    54         .global    kentry_load
     34        .text
     35        .globl  hal_trap_entry
     36        .type   hal_trap_entry, @function
    5537
    56 #define SAVE_SIZE      CPU_REGS_NR*4
    57 #define LID_WIDTH      2
    58 #define CXY_WIDTH      8
    59 #define CXY_MASK       0xFF
    60 #define MMU_MODE_MASK  0xF
    61 #define GID_MASK       0x3FF   
    62 #define LID_MASK       0x3     
     38ASM_ENTRY(x86_trap00)
     39        ZTRAPENTRY(T_DIVIDE)
    6340
    64 #---------------------------------------------------------------------------------
    65 # Kernel Entry point
    66 #---------------------------------------------------------------------------------
     41ASM_ENTRY(x86_trap01)
     42        ZTRAPENTRY(T_TRCTRAP)
    6743
    68 kentry:
    69        
    70 #---------------------------------------------------------------------------------------       
    71 # this code is executed when the core is in user mode:
    72 # - we use the uzone defined in user thread descriptor.
    73 # - we set the MMU off, and save the CP2_MODE register to uzone.
    74 # - we save the user thread stack pointer to uzone and load the kernel stack pointer
    75 # - we store the uzone pointer in $27
     44ASM_ENTRY(x86_trap02)
     45        ZTRAPENTRY(T_NMI)
    7646
    77 user_mode:
     47ASM_ENTRY(x86_trap03)
     48        ZTRAPENTRY(T_BPTFLT)
    7849
    79 #---------------------------------------------------------------------------------------       
    80 # this code is executed when the core is in kernel mode:
    81 # - we use an uzone allocated in kernel stack.
    82 # - we set the MMU off, set the MMU data_paddr extension to local_cxy,
    83 #   and save the CP2_MODE and CP2_DEXT to uzone.
    84 # - we save the kernel stack pointer to uzone and load the new kernel stack pointer
    85 # - we store the uzone pointer in $27
     50ASM_ENTRY(x86_trap04)
     51        ZTRAPENTRY(T_OFLOW)
    8652
    87 kernel_mode:
     53ASM_ENTRY(x86_trap05)
     54        ZTRAPENTRY(T_BOUND)
    8855
    89 #--------------------------------------------------------------------------------------
    90 # this code is executed in both modes, with the two following assumptions:
    91 # - $27 contains the pointer on uzone to save the cpu registers
    92 # - $29 contains the kernel stack pointer
     56ASM_ENTRY(x86_trap06)
     57        ZTRAPENTRY(T_PRIVINFLT)
    9358
    94 unified_mode:   
     59ASM_ENTRY(x86_trap07)
     60        ZTRAPENTRY(T_DNA)
    9561
    96 #---------------------------------------------------------------------------------------
    97 # Depending on XCODE (in $1) , call the apropriate handler. The three called
    98 # functions take the same two arguments: thread pointer and uzone pointer.
     62ASM_ENTRY(x86_trap08)
     63        TRAPENTRY(T_DOUBLEFLT)
    9964
     65ASM_ENTRY(x86_trap09)
     66        ZTRAPENTRY(T_FPOPFLT)
    10067
    101 cause_excp:
     68ASM_ENTRY(x86_trap0a)
     69        TRAPENTRY(T_TSSFLT)
    10270
    103 cause_sys:
     71ASM_ENTRY(x86_trap0b)
     72        ZTRAPENTRY(T_SEGNPFLT)
    10473
    105 cause_int:
     74ASM_ENTRY(x86_trap0c)
     75        ZTRAPENTRY(T_STKFLT)
    10676
    107 # -----------------------------------------------------------------------------------
    108 # Kentry exit
    109 # -----------------------------------------------------------------------------------
    110 kentry_exit:
     77ASM_ENTRY(x86_trap0d)
     78        ZTRAPENTRY(T_PROTFLT)
    11179
    112 # Possible value for MMU_MODE
    113 # In kernel mode : 0x7/0x3
    114 # In user mode   : 0xF
     80ASM_ENTRY(x86_trap0e)
     81        TRAPENTRY(T_PAGEFLT)
    11582
    116 # DP_EXT can either be local or remote
    117 # Once these register set we can no longuer
    118 # access global data
     83ASM_ENTRY(x86_trap0f)
     84        ZTRAPENTRY(T_ASTFLT)
    11985
    120 out_mmu_F:
     86ASM_ENTRY(x86_trap10)
     87        ZTRAPENTRY(T_ARITHTRAP)
    12188
    122 out_mmu_7:
     89ASM_ENTRY(x86_trap11)
     90        TRAPENTRY(T_ALIGNFLT)
    12391
    124 out_mmu_3:
     92ASM_ENTRY(x86_trap12)
     93        ZTRAPENTRY(T_MCA)
    12594
    126 out_kentry:
     95ASM_ENTRY(x86_trap13)
     96        ZTRAPENTRY(T_XMM)
    12797
    128 kentry_load:
    129         # theses nops are required to load the eret instruction
    130         # while we are in virtual mode (processor pipeline) ?
     98ASM_ENTRY(x86_trap14)
     99ASM_ENTRY(x86_trap15)
     100ASM_ENTRY(x86_trap16)
     101ASM_ENTRY(x86_trap17)
     102ASM_ENTRY(x86_trap18)
     103ASM_ENTRY(x86_trap19)
     104ASM_ENTRY(x86_trap1a)
     105ASM_ENTRY(x86_trap1b)
     106ASM_ENTRY(x86_trap1c)
     107ASM_ENTRY(x86_trap1d)
     108ASM_ENTRY(x86_trap1e)
     109ASM_ENTRY(x86_trap1f)
     110        /* 20 - 31 reserved for future exp */
     111        ZTRAPENTRY(T_RESERVED)
    131112
     113/*
     114 * Arguments pushed on the stack:
     115 *  tf_trapno
     116 *  tf_err: Dummy inserted if not defined
     117 *  tf_rip
     118 *  tf_cs
     119 *  tf_rflags
     120 *  tf_rsp
     121 *  tf_ss
     122 */
     123alltraps:
     124        movq    %rsp,%rdi
     125        call    hal_trap_entry
     126        /* NOTREACHED */
    132127
    133 #-------------------------------------------------------------------------------
     128        .data
     129        .globl  x86_traps
     130        .type   x86_traps, @object
    134131
     132        .align  64
     133x86_traps:
     134        .quad   x86_trap00, x86_trap01
     135        .quad   x86_trap02, x86_trap03
     136        .quad   x86_trap04, x86_trap05
     137        .quad   x86_trap06, x86_trap07
     138        .quad   x86_trap08, x86_trap09
     139        .quad   x86_trap0a, x86_trap0b
     140        .quad   x86_trap0c, x86_trap0d
     141        .quad   x86_trap0e, x86_trap0f
     142        .quad   x86_trap10, x86_trap11
     143        .quad   x86_trap12, x86_trap13
     144        .quad   x86_trap14, x86_trap15
     145        .quad   x86_trap16, x86_trap17
     146        .quad   x86_trap18, x86_trap19
     147        .quad   x86_trap1a, x86_trap1b
     148        .quad   x86_trap1c, x86_trap1d
     149        .quad   x86_trap1e, x86_trap1f
     150
  • trunk/hal/x86_64/hal_kentry.h

    r25 r29  
    11/*
    2  * cpu-regs.h - mips register map
     2 * hal_kentry.h - General values used in the different kernel entries
    33 *
    4  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
    5  * Copyright (c) 2011,2012 UPMC Sorbonne Universites
     4 * Copyright (c) 2017 Maxime Villard
    65 *
    7  * This file is part of ALMOS-kernel.
     6 * This file is part of ALMOS-MKH.
    87 *
    9  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     8 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    109 * under the terms of the GNU General Public License as published by
    1110 * the Free Software Foundation; version 2.0 of the License.
    1211 *
    13  * ALMOS-kernel is distributed in the hope that it will be useful, but
     12 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1413 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1514 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1716 *
    1817 * You should have received a copy of the GNU General Public License
    19  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     18 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
    2019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2120 */
    2221
    23 #ifndef _HAL_KENTRY_H_
    24 #define _HAL_KENTRY_H_
     22#define T_PRIVINFLT      0      /* privileged instruction */
     23#define T_BPTFLT         1      /* breakpoint trap */
     24#define T_ARITHTRAP      2      /* arithmetic trap */
     25#define T_ASTFLT         3      /* asynchronous system trap */
     26#define T_PROTFLT        4      /* protection fault */
     27#define T_TRCTRAP        5      /* trace trap */
     28#define T_PAGEFLT        6      /* page fault */
     29#define T_ALIGNFLT       7      /* alignment fault */
     30#define T_DIVIDE         8      /* integer divide fault */
     31#define T_NMI            9      /* non-maskable interrupt */
     32#define T_OFLOW         10      /* overflow trap */
     33#define T_BOUND         11      /* bounds check fault */
     34#define T_DNA           12      /* device not available fault */
     35#define T_DOUBLEFLT     13      /* double fault */
     36#define T_FPOPFLT       14      /* fp coprocessor operand fetch fault */
     37#define T_TSSFLT        15      /* invalid tss fault */
     38#define T_SEGNPFLT      16      /* segment not present fault */
     39#define T_STKFLT        17      /* stack fault */
     40#define T_MCA           18      /* machine check */
     41#define T_XMM           19      /* SSE FP exception */
     42#define T_RESERVED      20      /* reserved fault base */
    2543
    26 #define      KSP          0
    27 #define      AT           1
    28 #define      V0           2
    29 #define      V1           3
    30 #define      A0           4
    31 #define      A1           5
    32 #define      A2           6
    33 #define      A3           7
    34 #define      T0           8
    35 #define      T1           9
    36 #define      T2           10
    37 #define      T3           11
    38 #define      T4           12
    39 #define      T5           13
    40 #define      T6           14
    41 #define      T7           15
    42 #define      T8           16
    43 #define      T9           17
    44 #define      S0           18
    45 #define      S1           19
    46 #define      S2           20
    47 #define      S3           21
    48 #define      S4           22
    49 #define      S5           23
    50 #define      S6           24
    51 #define      S7           25
    52 #define      S8           26
    53 #define      GP           27
    54 #define      RA           28
    55 #define      EPC          29
    56 #define      CR           30
    57 #define      SP           31
    58 #define      SR           32
    59 #define      LO           33
    60 #define      HI           34
    61 #define      TLS_K1       35
    62 #define      DP_EXT           36   // DATA PADDR EXTENSION
    63 #define      MMU_MD           37   // MMU MODE
    64 #define      REGS_NR      38
     44/* Trap's coming from user mode */
     45#define T_USER  0x100
    6546
    66 #define CPU_IN_KERNEL 1
     47#ifndef x86_ASM
    6748
    68 #endif  /* _HAL_ENTRY_H_ */
     49/*
     50 * The x86_64 trap frame.
     51 */
     52struct trapframe {
     53        uint64_t tf_rdi;
     54        uint64_t tf_rsi;
     55        uint64_t tf_rdx;
     56        uint64_t tf_r10;
     57        uint64_t tf_r8;
     58        uint64_t tf_r9;
     59
     60        uint64_t tf_arg6;
     61        uint64_t tf_arg7;
     62        uint64_t tf_arg8;
     63        uint64_t tf_arg9;
     64
     65        uint64_t tf_rcx;
     66        uint64_t tf_r11;
     67        uint64_t tf_r12;
     68        uint64_t tf_r13;
     69        uint64_t tf_r14;
     70        uint64_t tf_r15;
     71        uint64_t tf_rbp;
     72        uint64_t tf_rbx;
     73        uint64_t tf_rax;
     74
     75        uint64_t tf_gs;
     76        uint64_t tf_fs;
     77        uint64_t tf_es;
     78        uint64_t tf_ds;
     79
     80        uint64_t tf_trapno;
     81
     82        /* These are pushed for a trap */
     83        uint64_t tf_err;
     84        uint64_t tf_rip;
     85        uint64_t tf_cs;
     86        uint64_t tf_rflags;
     87
     88        /* These are always pushed */
     89        uint64_t tf_rsp;
     90        uint64_t tf_ss;
     91};
     92
     93/*
     94 * Our small trap frame.
     95 */
     96struct small_trapframe {
     97        uint64_t tf_trapno;
     98
     99        /* These are pushed for a trap */
     100        uint64_t tf_err;
     101        uint64_t tf_rip;
     102        uint64_t tf_cs;
     103        uint64_t tf_rflags;
     104
     105        /* These are always pushed */
     106        uint64_t tf_rsp;
     107        uint64_t tf_ss;
     108};
     109
     110#endif
     111
  • trunk/hal/x86_64/hal_register.h

    r25 r29  
    22 * hal_register.h - Values available in some x86 registers
    33 *
    4  * Author        Maxime Villard (2017)
     4 * Copyright (c) 2017 Maxime Villard
     5 *
     6 * This file is part of ALMOS-MKH.
    57 *
    6  * Copyright (c) UPMC Sorbonne Universites
    7  *
    8  * This file is part of ALMOS-kernel.
    9  *
    10  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     8 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    119 * under the terms of the GNU General Public License as published by
    1210 * the Free Software Foundation; version 2.0 of the License.
    1311 *
    14  * ALMOS-kernel is distributed in the hope that it will be useful, but
     12 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1513 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1614 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1816 *
    1917 * You should have received a copy of the GNU General Public License
    20  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     18 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
    2119 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2220 */
  • trunk/hal/x86_64/hal_segmentation.h

    r25 r29  
    11/*
    2  * hal_boot.S - Kernel boot entry point
     2 * hal_segmentation.h - Segmentation-related values and structures
    33 *
    4  * Author        Maxime Villard (2017)
     4 * Copyright (c) 2017 Maxime Villard
     5 *
     6 * This file is part of ALMOS-MKH.
    57 *
    6  * Copyright (c) UPMC Sorbonne Universites
    7  *
    8  * This file is part of ALMOS-kernel.
    9  *
    10  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     8 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    119 * under the terms of the GNU General Public License as published by
    1210 * the Free Software Foundation; version 2.0 of the License.
    1311 *
    14  * ALMOS-kernel is distributed in the hope that it will be useful, but
     12 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1513 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1614 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1816 *
    1917 * You should have received a copy of the GNU General Public License
    20  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     18 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
    2119 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2220 */
    2321
    2422/*
    25  * Selectors
     23 * The GDT has NGDT_MEM entries of 8-byte-sized, static memory entries. Then
     24 * there is a certain number of 16-byte-sized TSS entries, one for each CPU.
    2625 */
     26#define GDT_NULL_SEL    0       /* Null descriptor */
     27#define GDT_KCODE_SEL   1       /* Kernel code descriptor */
     28#define GDT_KDATA_SEL   2       /* Kernel data descriptor */
     29#define GDT_UCODE_SEL   3       /* User code descriptor */
     30#define GDT_UDATA_SEL   4       /* User data descriptor */
     31#define NGDT_MEM        6               /* aligned */
    2732
    28 #define ISPL(s)         ((s) & SEL_RPL) /* what is the priority level of a selector */
     33#define GDT_CPU0TSS_SEL 0       /* cpu0's tss */
     34#define NGDT_DYN        (256 - (NGDT_MEM / 2) * 16)
     35
     36/*
     37 * GDT Selectors Privileges
     38 */
     39#define ISPL(s)         ((s) & SEL_RPL)
     40#define USERMODE(c, f)          (ISPL(c) == SEL_UPL)
     41#define KERNELMODE(c, f)        (ISPL(c) == SEL_KPL)
    2942#define SEL_KPL         0               /* kernel privilege level */
    3043#define SEL_UPL         3               /* user privilege level */
    3144#define SEL_RPL         3               /* requester's privilege level mask */
    32 #define ISLDT(s)        ((s) & SEL_LDT) /* is it local or global */
    3345#define SEL_LDT         4               /* local descriptor table */
    3446
    35 #define GSEL(s,r)       (((s) << 3) | r)
     47#define SYSSEL_START    (NGDT_MEM * 8)
    3648
     49/* Create a selector, with an index and a privilege */
     50#define GDT_FIXED_SEL(s,r)      (((s) << 3) | r)
     51#define GDT_DYNAM_SEL(s,r)      ((SYSSEL_START + ((s) << 4)) | r)
    3752
     53#ifndef x86_ASM
    3854
    3955/*
    40  * Entries in the Global Descriptor Table (GDT). They remain static.
     56 * Offsets.
    4157 */
    42 #define GNULL_SEL       0       /* Null descriptor */
    43 #define GCODE_SEL       1       /* Kernel code descriptor */
    44 #define GDATA_SEL       2       /* Kernel data descriptor */
    45 #define GUCODE_SEL      3       /* User code descriptor */
    46 #define GUDATA_SEL      4       /* User data descriptor */
     58#define GDT_ADDR_MEM(s,i)       \
     59        ((struct gdt_memseg *)((s) + ((i) << 3)))
     60#define GDT_ADDR_SYS(s,i)       \
     61        ((struct gdt_sysseg *)((s) + (SYSSEL_START + ((i) << 4))))
    4762
     63/*
     64 * System segment descriptor (16 bytes): used for TSS and LDT.
     65 */
     66struct gdt_sysseg {
     67        uint64_t sd_lolimit:16; /* segment extent (lsb) */
     68        uint64_t sd_lobase:24;  /* segment base address (lsb) */
     69        uint64_t sd_type:5;     /* segment type */
     70        uint64_t sd_dpl:2;      /* segment descriptor priority level */
     71        uint64_t sd_p:1;        /* segment descriptor present */
     72        uint64_t sd_hilimit:4;  /* segment extent (msb) */
     73        uint64_t sd_xx1:3;      /* avl, long and def32 (not used) */
     74        uint64_t sd_gran:1;     /* limit granularity (byte/page) */
     75        uint64_t sd_hibase:40;  /* segment base address (msb) */
     76        uint64_t sd_xx2:8;      /* reserved */
     77        uint64_t sd_zero:5;     /* must be zero */
     78        uint64_t sd_xx3:19;     /* reserved */
     79} __packed;
     80
     81/*
     82 * Memory segment descriptor (8 bytes): used for cs, ds, etc.
     83 */
     84struct gdt_memseg {
     85        unsigned sd_lolimit:16; /* segment extent (lsb) */
     86        unsigned sd_lobase:24;  /* segment base address (lsb) */
     87        unsigned sd_type:5;     /* segment type */
     88        unsigned sd_dpl:2;      /* segment descriptor priority level */
     89        unsigned sd_p:1;        /* segment descriptor present */
     90        unsigned sd_hilimit:4;  /* segment extent (msb) */
     91        unsigned sd_avl:1;      /* available */
     92        unsigned sd_long:1;     /* long mode */
     93        unsigned sd_def32:1;    /* default 32 vs 16 bit size */
     94        unsigned sd_gran:1;     /* limit granularity (byte/page) */
     95        unsigned sd_hibase:8;   /* segment base address (msb) */
     96} __packed;
     97
     98/*
     99 * Common part of the above structures. Used to walk descriptor tables.
     100 */
     101struct common_segment_descriptor {
     102        unsigned sdc_lolimit:16;
     103        unsigned sdc_lobase:24;
     104        unsigned sdc_type:5;
     105        unsigned sdc_other:19;
     106} __packed;
     107
     108/*
     109 * IDT descriptors (16 bytes).
     110 */
     111struct idt_seg {
     112        uint64_t gd_looffset:16;/* gate offset (lsb) */
     113        uint64_t gd_selector:16;/* gate segment selector */
     114        uint64_t gd_ist:3;      /* IST select */
     115        uint64_t gd_xx1:5;      /* reserved */
     116        uint64_t gd_type:5;     /* segment type */
     117        uint64_t gd_dpl:2;      /* segment descriptor priority level */
     118        uint64_t gd_p:1;        /* segment descriptor present */
     119        uint64_t gd_hioffset:48;/* gate offset (msb) */
     120        uint64_t gd_xx2:8;      /* reserved */
     121        uint64_t gd_zero:5;     /* must be zero */
     122        uint64_t gd_xx3:19;     /* reserved */
     123} __packed;
     124
     125/*
     126 * Region descriptors, used to load gdt/idt tables before segments yet exist.
     127 */
     128struct region_descriptor {
     129        uint16_t rd_limit;      /* segment extent */
     130        uint64_t rd_base;       /* base address  */
     131} __packed;
     132
     133struct tss {
     134        uint32_t tss_reserved1;
     135        uint64_t tss_rsp0;
     136        uint64_t tss_rsp1;
     137        uint64_t tss_rsp3;
     138        uint32_t tss_reserved2;
     139        uint32_t tss_reserved3;
     140        uint64_t tss_ist[7];
     141        uint32_t tss_reserved4;
     142        uint32_t tss_reserved5;
     143        uint32_t tss_iobase;
     144} __packed;
     145
     146#define IOMAP_INVALOFF  0xffff
     147
     148void lgdt(struct region_descriptor *);
     149void lidt(struct region_descriptor *);
     150void ltr(u_short);
     151
     152#endif /* !x86_ASM */
     153
     154/* system segments and gate types */
     155#define SDT_SYSNULL      0      /* system null */
     156#define SDT_SYS286TSS    1      /* system 286 TSS available */
     157#define SDT_SYSLDT       2      /* system local descriptor table */
     158#define SDT_SYS286BSY    3      /* system 286 TSS busy */
     159#define SDT_SYS286CGT    4      /* system 286 call gate */
     160#define SDT_SYSTASKGT    5      /* system task gate */
     161#define SDT_SYS286IGT    6      /* system 286 interrupt gate */
     162#define SDT_SYS286TGT    7      /* system 286 trap gate */
     163#define SDT_SYSNULL2     8      /* system null again */
     164#define SDT_SYS386TSS    9      /* system 386 TSS available */
     165#define SDT_SYSNULL3    10      /* system null again */
     166#define SDT_SYS386BSY   11      /* system 386 TSS busy */
     167#define SDT_SYS386CGT   12      /* system 386 call gate */
     168#define SDT_SYSNULL4    13      /* system null again */
     169#define SDT_SYS386IGT   14      /* system 386 interrupt gate */
     170#define SDT_SYS386TGT   15      /* system 386 trap gate */
     171
     172/* memory segment types */
     173#define SDT_MEMRO       16      /* memory read only */
     174#define SDT_MEMROA      17      /* memory read only accessed */
     175#define SDT_MEMRW       18      /* memory read write */
     176#define SDT_MEMRWA      19      /* memory read write accessed */
     177#define SDT_MEMROD      20      /* memory read only expand dwn limit */
     178#define SDT_MEMRODA     21      /* memory read only expand dwn limit accessed */
     179#define SDT_MEMRWD      22      /* memory read write expand dwn limit */
     180#define SDT_MEMRWDA     23      /* memory read write expand dwn limit accessed */
     181#define SDT_MEME        24      /* memory execute only */
     182#define SDT_MEMEA       25      /* memory execute only accessed */
     183#define SDT_MEMER       26      /* memory execute read */
     184#define SDT_MEMERA      27      /* memory execute read accessed */
     185#define SDT_MEMEC       28      /* memory execute only conforming */
     186#define SDT_MEMEAC      29      /* memory execute only accessed conforming */
     187#define SDT_MEMERC      30      /* memory execute read conforming */
     188#define SDT_MEMERAC     31      /* memory execute read accessed conforming */
     189
     190/*
     191 * Entries in the Interrupt Descriptor Table (IDT)
     192 */
     193#define NCPUIDT 32      /* reserved entries for CPU exceptions */
     194#define NIDT    256     /* total number of IDT entries */
     195
  • trunk/hal/x86_64/hal_types.h

    r25 r29  
    3131#endif
    3232
     33#define __packed        __attribute__((__packed__))
     34#define __arraycount(a) (sizeof(a) / sizeof(*(a)))
     35#define __in_kdata      __attribute__((section(".kdata")))
     36
     37
    3338/**************************************************************************
    3439 *                      Exact-width integer types. 
     
    4651typedef   signed long long int      int64_t;
    4752typedef unsigned long long int     uint64_t;
     53
     54typedef uint64_t                     size_t;
    4855
    4956/***************************************************************************
Note: See TracChangeset for help on using the changeset viewer.