From: Matthias Kruk Date: Fri, 6 Sep 2019 08:34:56 +0000 (+0900) Subject: Create code and data segments for all privilege levels in order to allow more fine... X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=2f5c93bff1171de33a936f7cdf2157f8b7116a9d;p=corax Create code and data segments for all privilege levels in order to allow more fine-grained separation of processes in the future --- diff --git a/kernel/arch/defs.h b/kernel/arch/defs.h index 72e9903..cc2b14b 100644 --- a/kernel/arch/defs.h +++ b/kernel/arch/defs.h @@ -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) @@ -39,11 +40,17 @@ #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 @@ -97,7 +104,13 @@ #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 @@ -107,8 +120,6 @@ #endif /* CONFIG_APIC */ #define NUM_SYS_VECTORS 1 -#define CORAX_SYS_VECTOR 0xCC - /* i8259 definitions */ #define PIC1_BASE 0x20 #define PIC1_COMM PIC1_BASE diff --git a/kernel/arch/entry.S b/kernel/arch/entry.S index 0603c6d..fe7aade 100644 --- a/kernel/arch/entry.S +++ b/kernel/arch/entry.S @@ -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 diff --git a/kernel/arch/init.S b/kernel/arch/init.S index e35ddcf..6f9961b 100644 --- a/kernel/arch/init.S +++ b/kernel/arch/init.S @@ -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)