]> git.corax.cc Git - corax/commitdiff
Create code and data segments for all privilege levels in order to allow more fine...
authorMatthias Kruk <m@m10k.eu>
Fri, 6 Sep 2019 08:34:56 +0000 (17:34 +0900)
committerMatthias Kruk <m@m10k.eu>
Fri, 6 Sep 2019 08:34:56 +0000 (17:34 +0900)
kernel/arch/defs.h
kernel/arch/entry.S
kernel/arch/init.S

index 72e9903228b560b28380999bfe2a472bf32ed256..cc2b14bf0cfe156e7a7f47543385df6ab5054a39 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of the Corax operating system.
- * Copyright (C) 2016 Matthias Kruk
+ * Copyright (C) 2016-2019 Matthias Kruk
  *
  * Corax is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,6 +29,7 @@
 #define SD_TYPE_CALL_GATE   0x8c00
 #define SD_TYPE_TRAP_GATE   0x8f00
 
+#define DPL(n)      ((n) << 13)
 #define DPL_KERNEL  0
 #define DPL_DRIVER  (1 << 13)
 #define DPL_SYSTEM  (2 << 13)
 #define RPL_SYSTEM  2
 #define RPL_USER    3
 
-#define KERNEL_DATA 0x10
-#define KERNEL_CODE 0x08
+#define SEGM_CS(n)  (0x10 * (n) + 0x08 | (n))
+#define SEGM_DS(n)  (0x10 * (n) + 0x10 | (n))
 
-#define USER_DATA   0x23
-#define USER_CODE   0x1b
+#define KERNEL_CODE SEGM_CS(RPL_KERNEL)
+#define KERNEL_DATA SEGM_DS(RPL_KERNEL)
+#define DRIVER_CODE SEGM_CS(RPL_DRIVER)
+#define DRIVER_DATA SEGM_DS(RPL_DRIVER)
+#define SYSTEM_CODE SEGM_CS(RPL_SYSTEM)
+#define SYSTEM_DATA SEGM_DS(RPL_SYSTEM)
+#define USER_CODE   SEGM_CS(RPL_USER)
+#define USER_DATA   SEGM_DS(RPL_USER)
 
 #define GDT_ENTRIES 16
 #define IDT_ENTRIES 256
@@ -55,7 +62,7 @@
 #define TSS_OFFSET  (IDT_OFFSET + (IDT_ENTRIES * 8))
 #define TSS_SIZE    102
 
-#define TSS_SEL     (0x28 | RPL_USER)
+#define TSS_SEL     (0x48 | RPL_USER)
 
 #define CR0_PG_BIT      0x80000000
 #define CR4_PSE_BIT     0x00000010
 
 #define EXC_VECTOR0         0x00
 #define INT_VECTOR0         0x40
-#define SYS_VECTOR0         0xCC
+#define SYS_VECTOR0         SYS_VECTOR_CORAX
+
+#define SYS_VECTOR_CORAX    0xCC
+
+#if FEATURE(POSIX)
+#define SYS_VECTOR_POSIX    0x80
+#endif /* FEATURE(POSIX) */
 
 #define NUM_EXC_VECTORS     21
 #if CONFIG_APIC == 1
 #endif /* CONFIG_APIC */
 #define NUM_SYS_VECTORS     1
 
-#define CORAX_SYS_VECTOR    0xCC
-
 /* i8259 definitions */
 #define PIC1_BASE           0x20
 #define PIC1_COMM           PIC1_BASE
index 0603c6d6543f82d4db1a85914b962dc4c660ce16..fe7aade0379e31686b15ddea34eed616f1476dae 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of the Corax operating system.
- * Copyright (C) 2016 Matthias Kruk
+ * Copyright (C) 2016-2019 Matthias Kruk
  *
  * Corax is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -70,6 +70,9 @@
 .global _int_entry23
 #endif /* CONFIG_APIC */
 
+#if FEATURE(POSIX)
+.global _sys_entryPX
+#endif /* FEATURE(POSIX) */
 .global _sys_entryCC
 
 .global _int_entry_common
@@ -300,9 +303,19 @@ _int_entry23:
 
 #endif /* CONFIG_APIC */
 
+#if FEATURE(POSIX)
+
+_sys_entryPX:
+       pushl   $0
+       pushl   $SYS_VECTOR_POSIX
+       jmp     _int_entry_common
+
+#endif /* FEATURE(POSIX) */
+
 _sys_entryCC:
     pushl   $0
     pushl   $SYS_VECTOR0
+       /* fall through */
 
 _int_entry_common:
     pusha
index e35ddcfe91a3ed1b3191f4b33a3eb71ea5eea43f..6f9961b1b093ae060e30ab6e89541cc8f3bc26a9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of the Corax operating system.
- * Copyright (C) 2016 Matthias Kruk
+ * Copyright (C) 2016-2019 Matthias Kruk
  *
  * Corax is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -84,6 +84,9 @@
 #endif /* CONFIG_APIC */
 
 .extern _sys_entryCC
+#if FEATURE(POSIX)
+.extern _sys_entryPX
+#endif /* FEATURE(POSIX) */
 
 /* this table is used to fill the IDT in arch_init(),
    saves us ~260B of memory, compared with an unrolled loop */
@@ -190,10 +193,13 @@ arch_init:
 
     movl    $_cpu, %eax
     addl    $GDT_OFFSET, %eax
+
+       /* create zero entry */
     movl    $0, (%eax)
     movl    $0, 4(%eax)
-    addl    $8, %eax
 
+       /* create code segments */
+       addl    $8, %eax
     movl    $DPL_KERNEL, 16(%esp)
     movl    $0xffffffff, 12(%esp)
     movl    $0, 8(%esp)
@@ -201,19 +207,38 @@ arch_init:
     movl    %eax, (%esp)
     call    _segment_descriptor_set
 
-    movl    $SD_TYPE_DATA, 4(%esp)
-    addl    $8, (%esp)
-    call    _segment_descriptor_set
+       addl    $16, (%esp)
+       movl    $DPL_DRIVER, 16(%esp)
+       call    _segment_descriptor_set
 
-    movl    $DPL_USER, 16(%esp)
-    addl    $16, (%esp)
-    call    _segment_descriptor_set
+       addl    $16, (%esp)
+       movl    $DPL_SYSTEM, 16(%esp)
+       call    _segment_descriptor_set
 
-    movl    $SD_TYPE_CODE, 4(%esp)
-    subl    $8, (%esp)
-    call    _segment_descriptor_set
+       addl    $16, (%esp)
+       movl    $DPL_USER, 16(%esp)
+       call    _segment_descriptor_set
 
-    addl    $16, (%esp)
+       /* create data segments */
+       subl    $40, (%esp)
+       movl    $DPL_KERNEL, 16(%esp)
+       movl    $SD_TYPE_DATA, 4(%esp)
+       call    _segment_descriptor_set
+
+       addl    $16, (%esp)
+       movl    $DPL_DRIVER, 16(%esp)
+       call    _segment_descriptor_set
+
+       addl    $16, (%esp)
+       movl    $DPL_SYSTEM, 16(%esp)
+       call    _segment_descriptor_set
+
+       addl    $16, (%esp)
+       movl    $DPL_USER, 16(%esp)
+       call    _segment_descriptor_set
+
+       /* add TSS entry */
+    addl    $8, (%esp)
     movl    $SD_TYPE_TSS, 4(%esp)
     movl    $_cpu, %eax
     addl    $TSS_OFFSET, %eax
@@ -221,9 +246,7 @@ arch_init:
     movl    $TSS_SIZE, 12(%esp)
     call    _segment_descriptor_set
 
-    movw    $GDT_ENTRIES, %ax
-    shlw    $3, %ax
-    addw    $1, %ax
+    movw    $((GDT_ENTRIES * 8) + 1), %ax
     movw    %ax, (%esp)
     movl    $_cpu, %eax
     addl    $GDT_OFFSET, %eax
@@ -238,7 +261,7 @@ arch_init:
     movw    %ax, %ss
     ljmp    $KERNEL_CODE, $_update_cs
     .local _update_cs
-    _update_cs:
+_update_cs:
 
     movw    $TSS_SEL, %ax
     ltr     %ax
@@ -330,9 +353,21 @@ arch_init:
     _idt_fill_done:
     */
 
+#if FEATURE(POSIX)
+       /* add an entry for POSIX syscalls */
+       movl    $_sys_entryPX, 12(%esp)
+       movl    $_cpu, %eax
+       addl    $(IDT_OFFSET + (SYS_VECTOR_POSIX * 8)), %eax
+       movl    %eax, (%esp)
+       call    _segment_descriptor_set
+#endif /* FEATURE(POSIX) */
+
     /* add an entry for Corax syscalls */
+       movl    $DPL_SYSTEM, 16(%esp)
     movl    $_sys_entryCC, 12(%esp)
-    addl    $((SYS_VECTOR0 - (INT_VECTOR0 + NUM_INT_VECTORS)) * 8), (%esp)
+       movl    $_cpu, %eax
+       addl    $(IDT_OFFSET + (SYS_VECTOR_CORAX * 8)), %eax
+       movl    %eax, (%esp)
     call    _segment_descriptor_set
 
     movw    $((IDT_ENTRIES * 8) + 1), (%esp)