LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
sc.c File Reference

(29312b1 fbconsole, panic: render panic after framebuffer was given to userspace - without clearing screen contents)

#include "sc.h"
#include "msr.h"
#include "string.h"
#include "mm.h"
#include "cpu.h"
#include "scheduler.h"
#include "pic.h"
#include "panic.h"
#include "vm.h"
#include "mq.h"
#include "flexarray.h"
#include "errno.h"
+ Include dependency graph for sc.c:

Go to the source code of this file.

Data Structures

struct  idt_entry
 
struct  gdt_entry
 
struct  table_pointer
 
struct  tss
 
struct  cpu_local_data
 

Macros

#define GDT_ACCESSED   0x01
 
#define GDT_RW   0x02
 
#define GDT_CONFORMING   0x04
 
#define GDT_EXECUTE   0x08
 
#define GDT_SYSTEM   0x10
 
#define GDT_RING1   0x20
 
#define GDT_RING2   0x40
 
#define GDT_RING3   0x60
 
#define GDT_PRESENT   0x80
 

Functions

struct idt_entry __attribute__ ((packed))
 
void sc_handle (cpu_state *cpu)
 
void _syscall_handler (void)
 
void reload_cs (void)
 
void idt_entry_0 (void)
 
void idt_entry_1 (void)
 
void idt_entry_2 (void)
 
void idt_entry_3 (void)
 
void idt_entry_4 (void)
 
void idt_entry_5 (void)
 
void idt_entry_6 (void)
 
void idt_entry_7 (void)
 
void idt_entry_8 (void)
 
void idt_entry_9 (void)
 
void idt_entry_10 (void)
 
void idt_entry_11 (void)
 
void idt_entry_12 (void)
 
void idt_entry_13 (void)
 
void idt_entry_14 (void)
 
void idt_entry_15 (void)
 
void idt_entry_16 (void)
 
void idt_entry_17 (void)
 
void idt_entry_18 (void)
 
void idt_entry_19 (void)
 
void idt_entry_20 (void)
 
void idt_entry_21 (void)
 
void idt_entry_22 (void)
 
void idt_entry_23 (void)
 
void idt_entry_24 (void)
 
void idt_entry_25 (void)
 
void idt_entry_26 (void)
 
void idt_entry_27 (void)
 
void idt_entry_28 (void)
 
void idt_entry_29 (void)
 
void idt_entry_30 (void)
 
void idt_entry_31 (void)
 
void idt_entry_32 (void)
 
void idt_entry_33 (void)
 
void idt_entry_34 (void)
 
void idt_entry_35 (void)
 
void idt_entry_36 (void)
 
void idt_entry_37 (void)
 
void idt_entry_38 (void)
 
void idt_entry_39 (void)
 
void idt_entry_40 (void)
 
void idt_entry_41 (void)
 
void idt_entry_42 (void)
 
void idt_entry_43 (void)
 
void idt_entry_44 (void)
 
void idt_entry_45 (void)
 
void idt_entry_46 (void)
 
void idt_entry_47 (void)
 
void set_iopb (struct vm_table *context, ptr_t new_iopb)
 
void init_gdt (void)
 
void interrupt_del_queue (uint8_t interrupt, uint64_t mq)
 
static void interrupt_delall_queue (uint64_t mq)
 
void interrupt_add_queue (uint8_t interrupt, uint64_t mq)
 
static void _set_idt_entry (int index, ptr_t base)
 
static void _setup_idt (void)
 
void init_sc (void)
 
static void enable_iopb (struct vm_table *context)
 
static cpu_stateschedule_process (cpu_state *old_cpu)
 
static bool handle_userspace_exception (cpu_state *cpu)
 
 __attribute__ ((force_align_arg_pointer))
 

Variables

typedef __attribute__
 
uint16_t baseLow
 
uint16_t selector
 
uint8_t ist
 
uint8_t flags
 
uint16_t baseMid
 
uint32_t baseHigh
 
uint32_t _reserved
 
uint16_t limitLow
 
uint8_t type
 
uint8_t size
 
uint16_t limit
 
uint64_t base
 
uint32_t _reserved0
 
uint64_t rsp0
 
uint64_t rsp1
 
uint64_t rsp2
 
uint64_t _reserved1
 
uint64_t ist1
 
uint64_t ist2
 
uint64_t ist3
 
uint64_t ist4
 
uint64_t ist5
 
uint64_t ist6
 
uint64_t ist7
 
uint64_t _reserved2
 
uint16_t _reserved3
 
uint16_t iopb_offset
 
static cpu_local_data_cpu0
 
static struct idt_entry _idt [256]
 
static flexarray_t interrupt_queues [16] = { 0 }
 

Data Structure Documentation

◆ idt_entry

struct idt_entry

Definition at line 24 of file sc.c.

+ Collaboration diagram for idt_entry:
Data Fields
uint32_t _reserved
uint32_t baseHigh
uint16_t baseLow
uint16_t baseMid
uint8_t flags
uint8_t ist
uint16_t selector

◆ gdt_entry

struct gdt_entry

Definition at line 34 of file sc.c.

+ Collaboration diagram for gdt_entry:
Data Fields
uint8_t baseHigh
uint16_t baseLow
uint8_t baseMid
uint16_t limitLow
uint8_t size
uint8_t type

◆ table_pointer

struct table_pointer

Definition at line 43 of file sc.c.

+ Collaboration diagram for table_pointer:
Data Fields
uint64_t base
uint16_t limit

◆ tss

struct tss

Definition at line 48 of file sc.c.

+ Collaboration diagram for tss:
Data Fields
uint32_t _reserved0
uint64_t _reserved1
uint64_t _reserved2
uint16_t _reserved3
uint16_t iopb_offset
uint64_t ist1
uint64_t ist2
uint64_t ist3
uint64_t ist4
uint64_t ist5
uint64_t ist6
uint64_t ist7
uint64_t rsp0
uint64_t rsp1
uint64_t rsp2

◆ cpu_local_data

struct cpu_local_data

Definition at line 70 of file sc.c.

+ Collaboration diagram for cpu_local_data:
Data Fields
ptr_t kernel_stack
struct tss tss

Macro Definition Documentation

◆ GDT_ACCESSED

#define GDT_ACCESSED   0x01

Definition at line 14 of file sc.c.

◆ GDT_CONFORMING

#define GDT_CONFORMING   0x04

Definition at line 16 of file sc.c.

◆ GDT_EXECUTE

#define GDT_EXECUTE   0x08

Definition at line 17 of file sc.c.

◆ GDT_PRESENT

#define GDT_PRESENT   0x80

Definition at line 22 of file sc.c.

◆ GDT_RING1

#define GDT_RING1   0x20

Definition at line 19 of file sc.c.

◆ GDT_RING2

#define GDT_RING2   0x40

Definition at line 20 of file sc.c.

◆ GDT_RING3

#define GDT_RING3   0x60

Definition at line 21 of file sc.c.

◆ GDT_RW

#define GDT_RW   0x02

Definition at line 15 of file sc.c.

◆ GDT_SYSTEM

#define GDT_SYSTEM   0x10

Definition at line 18 of file sc.c.

Function Documentation

◆ __attribute__() [1/2]

◆ __attribute__() [2/2]

struct idt_entry __attribute__ ( (packed)  )

Definition at line 28 of file hpet.c.

◆ _set_idt_entry()

static void _set_idt_entry ( int  index,
ptr_t  base 
)
static

Definition at line 241 of file sc.c.

References _idt, base, idt_entry::baseHigh, idt_entry::baseLow, idt_entry::baseMid, idt_entry::flags, idt_entry::ist, and idt_entry::selector.

+ Here is the caller graph for this function:

◆ _setup_idt()

◆ _syscall_handler()

void _syscall_handler ( void  )
extern
+ Here is the caller graph for this function:

◆ enable_iopb()

static void enable_iopb ( struct vm_table context)
static

Definition at line 323 of file sc.c.

References _cpu0, ALLOCATOR_REGION_USER_IOPERM, tss::iopb_offset, KiB, cpu_local_data::tss, vm_context_get_physical_for_virtual(), and vm_context_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_userspace_exception()

static bool handle_userspace_exception ( cpu_state cpu)
static

Definition at line 351 of file sc.c.

References DUMP_CPU, cpu_state::error_code, cpu_state::interrupt, kill_reason_abort, kill_reason_segv, logw, cpu_state::rip, scheduler_current_process, scheduler_handle_pf(), and scheduler_kill_current().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ idt_entry_0()

void idt_entry_0 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_1()

void idt_entry_1 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_10()

void idt_entry_10 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_11()

void idt_entry_11 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_12()

void idt_entry_12 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_13()

void idt_entry_13 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_14()

void idt_entry_14 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_15()

void idt_entry_15 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_16()

void idt_entry_16 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_17()

void idt_entry_17 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_18()

void idt_entry_18 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_19()

void idt_entry_19 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_2()

void idt_entry_2 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_20()

void idt_entry_20 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_21()

void idt_entry_21 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_22()

void idt_entry_22 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_23()

void idt_entry_23 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_24()

void idt_entry_24 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_25()

void idt_entry_25 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_26()

void idt_entry_26 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_27()

void idt_entry_27 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_28()

void idt_entry_28 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_29()

void idt_entry_29 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_3()

void idt_entry_3 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_30()

void idt_entry_30 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_31()

void idt_entry_31 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_32()

void idt_entry_32 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_33()

void idt_entry_33 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_34()

void idt_entry_34 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_35()

void idt_entry_35 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_36()

void idt_entry_36 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_37()

void idt_entry_37 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_38()

void idt_entry_38 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_39()

void idt_entry_39 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_4()

void idt_entry_4 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_40()

void idt_entry_40 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_41()

void idt_entry_41 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_42()

void idt_entry_42 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_43()

void idt_entry_43 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_44()

void idt_entry_44 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_45()

void idt_entry_45 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_46()

void idt_entry_46 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_47()

void idt_entry_47 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_5()

void idt_entry_5 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_6()

void idt_entry_6 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_7()

void idt_entry_7 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_8()

void idt_entry_8 ( void  )
extern
+ Here is the caller graph for this function:

◆ idt_entry_9()

void idt_entry_9 ( void  )
extern
+ Here is the caller graph for this function:

◆ init_gdt()

void init_gdt ( void  )

Definition at line 154 of file sc.c.

References _cpu0, tss::_reserved1, tss::_reserved2, tss::_reserved3, ALLOCATOR_REGION_KERNEL_HEAP, base, gdt_entry::baseHigh, gdt_entry::baseLow, gdt_entry::baseMid, GDT_ACCESSED, GDT_EXECUTE, GDT_PRESENT, GDT_RING3, GDT_RW, GDT_SYSTEM, tss::iopb_offset, tss::ist1, cpu_local_data::kernel_stack, KiB, table_pointer::limit, gdt_entry::limitLow, memset(), reload_cs(), tss::rsp0, tss::rsp1, tss::rsp2, gdt_entry::size, cpu_local_data::tss, gdt_entry::type, vm_context_alloc_pages(), and VM_KERNEL_CONTEXT.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init_sc()

void init_sc ( void  )

Definition at line 309 of file sc.c.

References _cpu0, _setup_idt(), _syscall_handler(), and write_msr().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interrupt_add_queue()

void interrupt_add_queue ( uint8_t  interrupt,
uint64_t  mq 
)

Definition at line 226 of file sc.c.

References EEXIST, error, flexarray_append(), interrupt_delall_queue(), interrupt_queues, kernel_alloc, logw, mq, mq_notify_teardown(), and new_flexarray().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interrupt_del_queue()

void interrupt_del_queue ( uint8_t  interrupt,
uint64_t  mq 
)

Definition at line 211 of file sc.c.

References flexarray_find(), flexarray_remove(), interrupt_queues, and mq.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interrupt_delall_queue()

static void interrupt_delall_queue ( uint64_t  mq)
static

Definition at line 220 of file sc.c.

References interrupt_del_queue(), interrupt_queues, and mq.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reload_cs()

void reload_cs ( void  )
extern
+ Here is the caller graph for this function:

◆ sc_handle()

void sc_handle ( cpu_state cpu)
extern

◆ schedule_process()

static cpu_state * schedule_process ( cpu_state old_cpu)
static

Definition at line 338 of file sc.c.

References enable_iopb(), schedule_next(), and vm_context_activate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_iopb()

void set_iopb ( struct vm_table context,
ptr_t  new_iopb 
)

Definition at line 134 of file sc.c.

References _cpu0, ALLOCATOR_REGION_USER_IOPERM, tss::iopb_offset, KiB, cpu_local_data::tss, vm_context_get_physical_for_virtual(), vm_context_map(), and VM_KERNEL_CONTEXT.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ __attribute__

static __attribute__
inline

Syscall clone (group 0, call 1)

Clone the process into a new process while optionally sharing all memory (which is commonly called a thread then).

Parameters
[in]share_memoryShare memory with original process
[in]entryEntrypoint of the child. Special case: NULL, will continue at the same code location (only valid if share_memory == 1)
[out]pidPID of the new process in the old process, 0 in the new process, -1 denotes an error

Syscall sleep (group 0, call 2)

Sleep for the given time or yield CPU time to another process, if given time is 0

Parameters
[in]nanosecondsNanoseconds until first normally scheduling the process again

Syscall get_pid (group 0, call 3)

Return own or parent PID

Parameters
[in]parentIf true, returns parent PID. Own PID otherwise
[out]pidOwn or parent PID

Syscall sbrk (group 1, call 0)

Increment the data segment by inc bytes and return the new end of the data segment.

Parameters
[in]incMinimum number of bytes to add the data segment
[out]data_endPointer to the last byte in the data segment

Syscall framebuffer (group 2, call 0)

Take control of the framebuffer (if still in control by kernel)

Parameters
[out]framebufferPointer to the framebuffer
[out]widthWidth of the framebuffer
[out]heightHeight of the framebuffer
[out]strideStride (number of bytes per pixel row) of the framebuffer
[out]colorFormatTBD

Syscall ioperm (group 2, call 1)

Request access to I/O ports, this is i386 and amd64 only

Parameters
[in]fromFirst port for which access is requested
[in]numNumber of ports for which access is requested
[in]turn_onRequest (true) or drop (false) access to port
[out]errorError code, zero on success

Syscall interrupt_notify (group 2, call 2)

Request to be notified of hardware interrupts or cancel notifications

Parameters
[in]interruptNumber of the interrupt to be notified for
[in]notifyAdd notification (true) or cancel (false)
[in]queueMessage queue to send notifications to
[out]errorError code, 0 if success

Syscall create_mutex (group 3, call 0)

This syscall creates a new mutex

Parameters
[out]mutexpthread_mutex_t value for the new mutex
[out]errorerror code, zero if success

Syscall destroy_mutex (group 3, call 1)

destroys the given mutex, returns an error if locked

Parameters
[in]mutexmutex to destroy as pthread_mutex_t
[out]errorerror code, zero if success

Syscall lock_mutex (group 3, call 2)

Waits for the mutex to be free and locks it or tries to lock it without blocking

Parameters
[in]mutexmutex to lock as pthread_mutex_t
[in]trylockTry to lock instead of block
[out]errorerror of this operation, zero if success

Syscall unlock_mutex (group 3, call 3)

Unlocks a mutex

Parameters
[in]mutexmutex to lock as pthread_mutex_t
[out]errorerror of this operation, zero if success

Syscall create_condvar (group 3, call 4)

Create a new condvar processes can wait on

Parameters
[out]condvarpthread_cond_t value for the new condvar
[out]errorerror code, zero if success

Syscall destroy_condvar (group 3, call 5)

Destroys a given condvar. Errors if processes are waiting on it

Parameters
[in]condvarpthread_cond_t value to destroy
[out]errorerror code, zero if success

Syscall signal_condvar (group 3, call 6)

Signals a condvar to unlock up to amount processes

Parameters
[in]condvarcondvar to signal
[in]amountmaximum amount of processes to unlock
[out]errorerror code, zero if success

Syscall wait_condvar (group 3, call 7)

Makes the calling process blocking wait on the condvar

Parameters
[in]condvarcondvar to wait on
[in]timeoutWait timeout, not yet implemented
[out]errorerror code, zero if success

Syscall mq_create (group 4, call 0)

create a new message queue

Parameters
[in]global_readAllow other processes to read from this queue
[in]global_writeAllow other processes to write to this queue
[in]message_limitMax count of messages to store in the queue
[in]data_limitMax amount of data to store in the queue
[out]mqUnique identifier for the new queue
[out]errorerror code, zero if success

Syscall mq_destroy (group 4, call 1)

Destroy a given message queue

Parameters
[in]mqID of the message queue to destroy
[out]errorerror code, zero if success

Syscall mq_poll (group 4, call 2)

Retrieve message from given queue

Parameters
[in]queueMessage queue ID, 0 is process queue
[in]waitWait for a message to be available
[in]messagePointer where to store the message, has to be allocated with enough storage
[out]errorError code, 0 on success

Syscall mq_send (group 4, call 3)

Send message to given queue

Parameters
[in]queueMessage queue ID, 0 is process queue
[in]processRecipient process, to send messages to other processes process queue
[in]messagePointer to message from queue
[out]errorError code, 0 if success

Syscall service_register (group 4, call 4)

Register as implementation of the given service

Parameters
[in]uuidUUID of the service to register for
[in]queueMessage queue ID, 0 is process queue
[out]errorError code, 0 if success

Syscall service_discover (group 4, call 5)

Send message to implementations of given service

Parameters
[in]uuidUUID of the service to discover
[in]queueMessage queue ID, 0 is process queue
[in]messageMessage to send, UUID will be filled in automatically, has to be of type ServiceDiscover
[out]errorError code, 0 if success

Syscall read (group 5, call 0)

Read nanoseconds since system start

Parameters
[out]nanosecondsNanoseconds since system start (or rather, since timer initialization)

Syscall print (group 255, call 0)

Print a message to the screen at a location defined by the kernel

Parameters
[in]strMessage to print

Definition at line 424 of file sc.c.

◆ _cpu0

cpu_local_data* _cpu0
static

Definition at line 129 of file sc.c.

◆ _idt

struct idt_entry _idt[256]
static

Definition at line 130 of file sc.c.

◆ _reserved

uint8_t _reserved

Definition at line 6 of file sc.c.

◆ _reserved0

uint32_t _reserved0

Definition at line 0 of file sc.c.

◆ _reserved1

uint64_t _reserved1

Definition at line 6 of file sc.c.

◆ _reserved2

uint64_t _reserved2

Definition at line 16 of file sc.c.

◆ _reserved3

uint16_t _reserved3

Definition at line 17 of file sc.c.

◆ base

uint64_t base

Definition at line 1 of file sc.c.

◆ baseHigh

uint8_t baseHigh

Definition at line 5 of file sc.c.

◆ baseLow

uint16_t baseLow

Definition at line 0 of file sc.c.

◆ baseMid

uint8_t baseMid

Definition at line 4 of file sc.c.

◆ flags

uint8_t flags

Definition at line 3 of file sc.c.

◆ interrupt_queues

flexarray_t interrupt_queues[16] = { 0 }
static

Definition at line 132 of file sc.c.

◆ iopb_offset

uint16_t iopb_offset

Definition at line 18 of file sc.c.

◆ ist

uint8_t ist

Definition at line 2 of file sc.c.

◆ ist1

uint64_t ist1

Definition at line 8 of file sc.c.

◆ ist2

uint64_t ist2

Definition at line 9 of file sc.c.

◆ ist3

uint64_t ist3

Definition at line 10 of file sc.c.

◆ ist4

uint64_t ist4

Definition at line 11 of file sc.c.

◆ ist5

uint64_t ist5

Definition at line 12 of file sc.c.

◆ ist6

uint64_t ist6

Definition at line 13 of file sc.c.

◆ ist7

uint64_t ist7

Definition at line 14 of file sc.c.

◆ limit

uint16_t limit

Definition at line 0 of file sc.c.

◆ limitLow

uint16_t limitLow

Definition at line 0 of file sc.c.

◆ rsp0

uint64_t rsp0

Definition at line 2 of file sc.c.

◆ rsp1

uint64_t rsp1

Definition at line 3 of file sc.c.

◆ rsp2

uint64_t rsp2

Definition at line 4 of file sc.c.

◆ selector

uint16_t selector

Definition at line 1 of file sc.c.

◆ size

uint8_t size

Definition at line 4 of file sc.c.

◆ type

uint8_t type

Definition at line 3 of file sc.c.