MonkOS  v0.1
A simple 64-bit operating system (x86_64)
syscall.c
Go to the documentation of this file.
1 //============================================================================
2 /// @file syscall.c
3 /// @brief System call support.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license that can
7 // be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #include <core.h>
11 #include <kernel/x86/cpu.h>
14 #include <kernel/mem/segments.h>
15 #include <kernel/syscall/syscall.h>
16 
17 // Model-specific registers used to set up system calls.
18 #define MSR_IA32_STAR 0xc0000081
19 #define MSR_IA32_LSTAR 0xc0000082
20 #define MSR_IA32_FMASK 0xc0000084
21 
22 static void
24 {
25  // Do nothing for now.
26 }
27 
28 void
30 {
31  // Request the CPU's extended features.
32  registers4_t regs;
33  cpuid(0x80000001, &regs);
34 
35  // Bit 11 of rdx tells us if the SYSCALL/SYSRET instructions are
36  // available. If they're not, raise an invalid opcode exception.
37  if (!(regs.rdx & (1 << 11))) {
39  }
40 
41  // Update the IA32_STAR MSR with the segment selectors that will be used
42  // by SYSCALL and SYSRET.
43  uint64_t star = rdmsr(MSR_IA32_STAR);
44  star &= 0x00000000ffffffff;
45  star |= (uint64_t)SEGMENT_SELECTOR_KERNEL_CODE << 32;
46  star |= (uint64_t)((SEGMENT_SELECTOR_USER_CODE - 16) | 3) << 48;
47  wrmsr(MSR_IA32_STAR, star);
48 
49  // Write the address of the system call handler used by SYSCALL.
51 
52  // Write the CPU flag mask used during SYSCALL.
54 }
__forceinline uint64_t rdmsr(uint32_t id)
Definition: cpu_inl.h:27
__forceinline void invalid_opcode()
Definition: cpu_inl.h:152
void syscall_init()
Set up the CPU to handle system calls.
Definition: syscall.c:29
__forceinline void wrmsr(uint32_t id, uint64_t value)
Definition: cpu_inl.h:38
Core include file.
#define MSR_IA32_FMASK
Definition: syscall.c:20
Memory segment definitions.
__forceinline void cpuid(uint32_t code, registers4_t *regs)
Definition: cpu_inl.h:17
CPU exceptions.
#define SEGMENT_SELECTOR_USER_CODE
Definition: segments.h:18
System call support.
Interrupt handling operations.
x86 CPU-specific function implementations.
#define SEGMENT_SELECTOR_KERNEL_CODE
Definition: segments.h:16
#define MSR_IA32_STAR
Definition: syscall.c:18
A record describing the first 4 general-purpose registers.
Definition: cpu.h:61
#define MSR_IA32_LSTAR
Definition: syscall.c:19
static void syscall_handle()
Definition: syscall.c:23
uint64_t rdx
Definition: cpu.h:66