Skip to content

Commit

Permalink
More user space header cleanup (#67)
Browse files Browse the repository at this point in the history
* Split machine-specific code in libjinue.
* Move and rename JINUE_X86_SYSCALL_IRQ`, and use it in the stub in
libjinue.
* Split <jinue/shared/asm/syscalls.h>
  • Loading branch information
phaubertin authored Oct 21, 2024
1 parent c3bef3a commit e58cbb5
Show file tree
Hide file tree
Showing 28 changed files with 214 additions and 98 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ $(kernel_img): kernel
kernel:
$(MAKE) -C kernel

# build the user space API library
.PHONY: libjinue
libjinue:
$(MAKE) -C $(libjinue)

# build the user space loader
.PHONY: loader
loader:
Expand Down
3 changes: 3 additions & 0 deletions include/jinue/jinue.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
#ifndef _JINUE_JINUE_H
#define _JINUE_JINUE_H

#include <jinue/shared/asm/descriptors.h>
#include <jinue/shared/asm/errno.h>
#include <jinue/shared/asm/logging.h>
#include <jinue/shared/asm/machine.h>
#include <jinue/shared/asm/mman.h>
#include <jinue/shared/asm/memory.h>
#include <jinue/shared/asm/permissions.h>
#include <jinue/shared/asm/syscalls.h>
Expand Down
41 changes: 41 additions & 0 deletions include/jinue/shared/asm/descriptors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2019-2024 Philippe Aubertin.
* All rights reserved.
* 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.
*
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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.
*/

#ifndef _JINUE_SHARED_ASM_DESCRIPTORS_H
#define _JINUE_SHARED_ASM_DESCRIPTORS_H

/* On a Unix system, the first three file descriptors typically represent
* stdout, stdin and stderr. Lets leave them unused to allow this usage. */

/** descriptor assigned to refer to own process in loader and initial process */
#define JINUE_DESC_SELF_PROCESS 3

#endif
31 changes: 23 additions & 8 deletions include/jinue/shared/asm/i686.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,45 @@


/** number of bits in a virtual address that represent the offset within a page */
#define JINUE_PAGE_BITS 12
#define JINUE_PAGE_BITS 12

/** size of a page */
#define JINUE_PAGE_SIZE (1<<JINUE_PAGE_BITS) /* 4096 */
#define JINUE_PAGE_SIZE (1<<JINUE_PAGE_BITS) /* 4096 */

/** bit mask for offset in a page */
#define JINUE_PAGE_MASK (JINUE_PAGE_SIZE - 1)
#define JINUE_PAGE_MASK (JINUE_PAGE_SIZE - 1)

/** The virtual address range starting at JINUE_KLIMIT is reserved by the
* kernel. The region above JINUE_KLIMIT has the same mapping in all address
* spaces. JINUE_KLIMIT must be aligned on a page directory boundary in PAE
* mode. */
#define JINUE_KLIMIT 0xc0000000
#define JINUE_KLIMIT 0xc0000000

/** stack base address (stack top) */
#define JINUE_STACK_BASE JINUE_KLIMIT
#define JINUE_STACK_BASE JINUE_KLIMIT

/** initial stack size */
#define JINUE_STACK_SIZE (128 * 1024)
#define JINUE_STACK_SIZE (128 * 1024)

/** stack portion reserved for environment, arguments and auxiliary vectors */
#define JINUE_RESERVED_STACK_SIZE (32 * 1024)
#define JINUE_RESERVED_STACK_SIZE (32 * 1024)

/** initial stack lower address */
#define JINUE_STACK_START (JINUE_STACK_BASE - JINUE_STACK_SIZE)
#define JINUE_STACK_START (JINUE_STACK_BASE - JINUE_STACK_SIZE)

/** interrupt vector for system call software interrupt */
#define JINUE_I686_SYSCALL_IRQ 0x80

/** slow/safe interrupt-based system call implementation */
#define JINUE_I686_HOWSYSCALL_INTERRUPT 0

/** AMD's fast system call implementation (SYSCALL/SYSLEAVE) */
#define JINUE_I686_HOWSYSCALL_FAST_AMD 1

/** Intel's fast system call implementation (SYSENTER/SYSEXIT) */
#define JINUE_I686_HOWSYSCALL_FAST_INTEL 2

/** last system call implementation index */
#define JINUE_I686_HOWSYSCALL_LAST 2

#endif
47 changes: 47 additions & 0 deletions include/jinue/shared/asm/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019-2024 Philippe Aubertin.
* All rights reserved.
* 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.
*
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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.
*/

#ifndef _JINUE_SHARED_ASM_LOGGING_H
#define _JINUE_SHARED_ASM_LOGGING_H

/** maximum string length for the PUTS system call */
#define JINUE_PUTS_MAX_LENGTH 120

/** log level "info" for the PUTS system call */
#define JINUE_PUTS_LOGLEVEL_INFO 'I'

/** log level "warning" for the PUTS system call */
#define JINUE_PUTS_LOGLEVEL_WARNING 'W'

/** log level "error" for the PUTS system call */
#define JINUE_PUTS_LOGLEVEL_ERROR 'E'

#endif
47 changes: 47 additions & 0 deletions include/jinue/shared/asm/mman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019-2024 Philippe Aubertin.
* All rights reserved.
* 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.
*
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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.
*/

#ifndef _JINUE_SHARED_ASM_MMAN_H
#define _JINUE_SHARED_ASM_MMAN_H

/** map page with no access permission */
#define JINUE_PROT_NONE 0

/** map page with read permission */
#define JINUE_PROT_READ (1<<0)

/** map page with write permission */
#define JINUE_PROT_WRITE (1<<1)

/** map page with execution permission */
#define JINUE_PROT_EXEC (1<<2)

#endif
54 changes: 2 additions & 52 deletions include/jinue/shared/asm/syscalls.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 Philippe Aubertin.
* Copyright (C) 2019-2024 Philippe Aubertin.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -32,11 +32,7 @@
#ifndef _JINUE_SHARED_ASM_SYSCALLS_H
#define _JINUE_SHARED_ASM_SYSCALLS_H

/** interrupt vector for system call software interrupt */
#define JINUE_SYSCALL_IRQ 0x80


/** rebooot the system */
/** reboot the system */
#define JINUE_SYS_REBOOT 2

/** send a fixed-length string to kernel logger */
Expand Down Expand Up @@ -93,50 +89,4 @@
/** start of function numbers for user space messages */
#define JINUE_SYS_USER_BASE 4096


/** slow/safe interrupt-based system call implementation */
#define JINUE_SYSCALL_IMPL_INTERRUPT 0

/** AMD's fast system call implementation (SYSCALL/SYSLEAVE) */
#define JINUE_SYSCALL_IMPL_FAST_AMD 1

/** Intel's fast system call implementation (SYSENTER/SYSEXIT) */
#define JINUE_SYSCALL_IMPL_FAST_INTEL 2

/** last system call implementation index */
#define JINUE_SYSCALL_IMPL_LAST 2


/** maximum string length for the PUTS system call */
#define JINUE_PUTS_MAX_LENGTH 120

/** log level "info" for the PUTS system call */
#define JINUE_PUTS_LOGLEVEL_INFO 'I'

/** log level "warning" for the PUTS system call */
#define JINUE_PUTS_LOGLEVEL_WARNING 'W'

/** log level "error" for the PUTS system call */
#define JINUE_PUTS_LOGLEVEL_ERROR 'E'


/** map page with no access permission */
#define JINUE_PROT_NONE 0

/** map page with read permission */
#define JINUE_PROT_READ (1<<0)

/** map page with write permission */
#define JINUE_PROT_WRITE (1<<1)

/** map page with execution permission */
#define JINUE_PROT_EXEC (1<<2)


/** descriptor assigned to refer to own process in loader and initial process
*
* On a Unix system, the first three file descriptors typically represent
* stdout, stdin and stderr. Lets leave them unused to allow this usage. */
#define JINUE_SELF_PROCESS_DESCRIPTOR 3

#endif
2 changes: 1 addition & 1 deletion kernel/application/syscalls/puts.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/

#include <jinue/shared/asm/errno.h>
#include <jinue/shared/asm/syscalls.h>
#include <jinue/shared/asm/logging.h>
#include <kernel/application/syscalls.h>
#include <kernel/domain/services/logging.h>

Expand Down
2 changes: 1 addition & 1 deletion kernel/domain/alloc/page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <jinue/shared/asm/syscalls.h>
#include <jinue/shared/asm/mman.h>
#include <kernel/domain/alloc/page_alloc.h>
#include <kernel/domain/alloc/vmalloc.h>
#include <kernel/machine/asm/machine.h>
Expand Down
2 changes: 1 addition & 1 deletion kernel/domain/services/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <jinue/shared/asm/syscalls.h>
#include <jinue/shared/asm/logging.h>
#include <kernel/domain/services/logging.h>
#include <kernel/utils/list.h>
#include <kernel/types.h>
Expand Down
7 changes: 4 additions & 3 deletions kernel/infrastructure/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <jinue/shared/asm/syscalls.h>
#include <jinue/shared/asm/descriptors.h>
#include <jinue/shared/asm/mman.h>
#include <kernel/domain/alloc/page_alloc.h>
#include <kernel/domain/alloc/vmalloc.h>
#include <kernel/domain/entities/descriptor.h>
Expand Down Expand Up @@ -499,14 +500,14 @@ static void initialize_stack(
* Initialize descriptors for user space loader
*
* This function initializes a single descriptor which references the process
* itself (JINUE_SELF_PROCESS_DESCRIPTOR).
* itself (JINUE_DESC_SELF_PROCESS).
*
* @param process process in which the ELF binary is loaded
*
* */
static void initialize_descriptors(process_t *process) {
descriptor_t *desc;
(void)dereference_unused_descriptor(&desc, process, JINUE_SELF_PROCESS_DESCRIPTOR);
(void)dereference_unused_descriptor(&desc, process, JINUE_DESC_SELF_PROCESS);

desc->object = &process->header;
desc->flags = DESCRIPTOR_FLAG_IN_USE;
Expand Down
9 changes: 5 additions & 4 deletions kernel/infrastructure/i686/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <kernel/interface/i686/interrupt.h>
#include <kernel/interface/i686/trap.h>
#include <kernel/interface/syscalls.h>
#include <kernel/machine/asm/machine.h>
#include <kernel/machine/init.h>
#include <kernel/utils/utils.h>
#include <assert.h>
Expand Down Expand Up @@ -128,7 +129,7 @@ static void init_idt(void) {
/* set interrupt gate flags */
unsigned int flags = SEG_TYPE_INTERRUPT_GATE | SEG_FLAG_NORMAL_GATE;

if(idx == JINUE_SYSCALL_IRQ) {
if(idx == JINUE_I686_SYSCALL_IRQ) {
flags |= SEG_FLAG_USER;
}
else {
Expand Down Expand Up @@ -221,7 +222,7 @@ static void select_syscall_implementation(void) {
if(cpu_has_feature(CPU_FEATURE_SYSCALL)) {
uint64_t msrval;

syscall_implementation = JINUE_SYSCALL_IMPL_FAST_AMD;
syscall_implementation = JINUE_I686_HOWSYSCALL_FAST_AMD;

msrval = rdmsr(MSR_EFER);
msrval |= MSR_FLAG_EFER_SCE;
Expand All @@ -234,7 +235,7 @@ static void select_syscall_implementation(void) {
wrmsr(MSR_STAR, msrval);
}
else if(cpu_has_feature(CPU_FEATURE_SYSENTER)) {
syscall_implementation = JINUE_SYSCALL_IMPL_FAST_INTEL;
syscall_implementation = JINUE_I686_HOWSYSCALL_FAST_INTEL;

wrmsr(MSR_IA32_SYSENTER_CS, SEG_SELECTOR(GDT_KERNEL_CODE, RPL_KERNEL));
wrmsr(MSR_IA32_SYSENTER_EIP, (uint64_t)(uintptr_t)fast_intel_entry);
Expand All @@ -243,7 +244,7 @@ static void select_syscall_implementation(void) {
wrmsr(MSR_IA32_SYSENTER_ESP, (uint64_t)(uintptr_t)NULL);
}
else {
syscall_implementation = JINUE_SYSCALL_IMPL_INTERRUPT;
syscall_implementation = JINUE_I686_HOWSYSCALL_INTERRUPT;
}
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/infrastructure/i686/vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <jinue/shared/asm/syscalls.h>
#include <jinue/shared/asm/logging.h>
#include <kernel/domain/services/logging.h>
#include <kernel/infrastructure/i686/io.h>
#include <kernel/infrastructure/i686/vga.h>
Expand Down
Loading

0 comments on commit e58cbb5

Please sign in to comment.