Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cbc1752e73 | |||
| df90bd7313 | |||
| 1375596310 | |||
| 61e5ebf513 | |||
| 58ae7f9f89 | |||
| b4ea4b8ca1 | |||
| f36fe615cc | |||
| c43b23bed3 | |||
| e52948830e | |||
| 31f7eb34a6 | |||
| c5ef8c53a7 | |||
| 108e04a8f0 | |||
| 091830e508 | |||
| 98528dbc4a | |||
| b9208aceba | |||
| aa77b0e7a2 | |||
| d4ed72cc46 | |||
| 8e64741a9e | |||
| 4b7cf4e04a | |||
| 58d50a5e9a | |||
| 669ab6ba8a | |||
| 185d3f6677 | |||
| 9fb5b69745 | |||
| 85020f2254 | |||
| 2dafa56d4b | |||
| a5ce86147d | |||
| 112b4204a7 | |||
| e258bfcc9f | |||
| 43440310f2 | |||
| 365272e360 | |||
| 08db5fe1f7 | |||
| 29a528780b | |||
| 75b7e08e96 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -6,7 +6,7 @@ quark-kernel
|
||||
autom4te.cache/
|
||||
.deps
|
||||
.dirstamp
|
||||
.vscode
|
||||
#.vscode
|
||||
aclocal.m4
|
||||
ar-lib
|
||||
compile
|
||||
|
||||
45
.vscode/c_cpp_properties.json
vendored
Normal file
45
.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"~/.quark/include"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/opt/cross/bin/i686-elf-gcc",
|
||||
"cStandard": "c11",
|
||||
"cppStandard": "c++11",
|
||||
"intelliSenseMode": "linux-gcc-x86",
|
||||
"compilerArgs": [
|
||||
"-ffreestanding",
|
||||
"-nostdlib"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "i686-cross",
|
||||
"includePath": [
|
||||
"/home/nathan/Documents/projects/quark-os/quark-kernel/**",
|
||||
"/opt/quark-os/rootfs/include/",
|
||||
"${workspaceFolder}/include"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/opt/cross/bin/i686-elf-gcc",
|
||||
"cppStandard": "c++14",
|
||||
"intelliSenseMode": "linux-gcc-x86",
|
||||
"compilerArgs": [
|
||||
"-ffreestanding ",
|
||||
"-nostdlib"
|
||||
],
|
||||
"cStandard": "c11",
|
||||
"mergeConfigurations": false,
|
||||
"browse": {
|
||||
"path": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"limitSymbolsToIncludedHeaders": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
37
.vscode/launch.json
vendored
Normal file
37
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(gdb) Launch",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "/opt/quark-os/rootfs/apps/quark-kernel",
|
||||
"args": [],
|
||||
"stopAtEntry": true,
|
||||
"stopAtConnect": true,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": true,
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerServerAddress": "localhost:1234",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}/*,
|
||||
{
|
||||
"text": "-target-select remote localhost:1234",
|
||||
"ignoreFailures": false
|
||||
}*/
|
||||
],
|
||||
"preLaunchTask": "Launch QEMU (i386)"
|
||||
/*"debugServerPath": "/bin/qemu-system-i386",
|
||||
"debugServerArgs": "-hda /opt/quark-os/quark.img -serial stdio -s -S",
|
||||
"serverLaunchTimeout": 2000*/
|
||||
}
|
||||
]
|
||||
}
|
||||
28
.vscode/settings.json
vendored
Normal file
28
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"files.exclude": {
|
||||
"**/*.o": true,
|
||||
"**/ar-lib": true,
|
||||
"**/Makefile": true,
|
||||
"**/*.in": true,
|
||||
"**/stamp-h1": true,
|
||||
"**/*.a": true,
|
||||
"aclocal.m4": true,
|
||||
"compile": true,
|
||||
"config.log": true,
|
||||
"config.status": true,
|
||||
"configure*": true,
|
||||
"depcomp": true,
|
||||
"install-sh": true,
|
||||
"missing": true,
|
||||
"**/.deps": true,
|
||||
"autom4te.cache": true,
|
||||
"**/quark-kernel": true
|
||||
},
|
||||
"C_Cpp.default.cStandard": "c11",
|
||||
"files.associations": {
|
||||
"memmap.h": "c",
|
||||
"pid.h": "c",
|
||||
"sighandler.h": "c",
|
||||
"string.h": "c"
|
||||
}
|
||||
}
|
||||
27
.vscode/tasks.json
vendored
Normal file
27
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||
// for the documentation about the tasks.json format
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"label": "make",
|
||||
"type": "shell",
|
||||
"command": "PATH=\"$PATH:/opt/cross/bin\" make",
|
||||
"problemMatcher": ["$gcc"],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "Launch QEMU (i386)",
|
||||
"type": "shell",
|
||||
"command": "qemu-system-i386 -hda /opt/quark-os/quark.img -serial stdio -s -S",
|
||||
"isBackground": true,
|
||||
"group": {
|
||||
"kind": "test",
|
||||
"isDefault": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
nobase_include_HEADERS = types/status.h types/syscallid.h types/physaddr.h types/syscallarg.h
|
||||
nobase_include_HEADERS = types/status.h types/syscallid.h types/physaddr.h \
|
||||
types/syscallarg.h sys/syscalls.h types/pid.h types/oid.h \
|
||||
types/sigaction.h types/message.h
|
||||
20
include/addressspace.h
Normal file
20
include/addressspace.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef QUARK_ADDRESSSPACE_H
|
||||
#define QUARK_ADDRESSSPACE_H
|
||||
|
||||
#include "types/physaddr.h"
|
||||
|
||||
typedef struct address_space_t
|
||||
{
|
||||
physaddr_t page_table;
|
||||
int refcount;
|
||||
} address_space_t;
|
||||
|
||||
address_space_t *address_space_construct();
|
||||
|
||||
void address_space_switch(address_space_t *address_space);
|
||||
|
||||
address_space_t *address_space_release(address_space_t *address_space);
|
||||
|
||||
void address_space_destroy(address_space_t *address_space);
|
||||
|
||||
#endif
|
||||
@@ -39,3 +39,11 @@ struct avltree_t *avl_remove(struct avltree_t *tree, int key);
|
||||
* @return void*
|
||||
*/
|
||||
void *avl_get(struct avltree_t *tree, int key);
|
||||
|
||||
/**
|
||||
* @brief Removes every node present on the given tree.
|
||||
*
|
||||
* @param tree
|
||||
* @return NULL
|
||||
*/
|
||||
void *avl_clear(struct avltree_t *tree);
|
||||
@@ -8,13 +8,11 @@
|
||||
* This function contructs the heap's internal tables, allocating pages as needed
|
||||
* to do so.
|
||||
*
|
||||
* @param page_stack Pointer to the page stack descriptor
|
||||
* @param base Base location of the heap to contruct
|
||||
* @param heap_size Total size in bytes of the heap
|
||||
* @param block_size Size in bytes of a single unit of allocation
|
||||
* @return a status code
|
||||
*/
|
||||
int kminit(void *base, void* start, size_t heap_size, size_t block_size);
|
||||
int kminit(void *base, size_t heap_size);
|
||||
|
||||
/**
|
||||
* @brief Allocates a block of memory containing at least `size` bytes.
|
||||
|
||||
@@ -5,13 +5,19 @@
|
||||
#include "queue.h"
|
||||
#include "mmgr.h"
|
||||
#include "syscalls.h"
|
||||
#include "addressspace.h"
|
||||
#include "process.h"
|
||||
#include "types/message.h"
|
||||
#include "types/syscallid.h"
|
||||
#include "types/status.h"
|
||||
#include "types/pid.h"
|
||||
#include "types/oid.h"
|
||||
#include "types/sighandler.h"
|
||||
#include <libmalloc/memmap.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define MAX_SYSCALL_ID 256
|
||||
#define module_limit 8
|
||||
#define MODULE_LIMIT 8
|
||||
|
||||
#define IO_OP 1 << 0
|
||||
#define IO_SYNC 0 << 0
|
||||
@@ -22,8 +28,6 @@
|
||||
#define IO_PORT 1 << 1
|
||||
#define IO_MAILBOX 2 << 1
|
||||
|
||||
typedef unsigned long (*signal_handler_t)(void*, void*);
|
||||
|
||||
struct process_context_t;
|
||||
|
||||
struct module_t
|
||||
@@ -40,94 +44,42 @@ struct boot_info_t
|
||||
size_t memory_size;
|
||||
memory_map_t map;
|
||||
size_t module_count;
|
||||
struct module_t modules[module_limit];
|
||||
};
|
||||
|
||||
struct message_t
|
||||
{
|
||||
unsigned long sender;
|
||||
unsigned long code;
|
||||
unsigned long args[6];
|
||||
};
|
||||
|
||||
enum process_state_t
|
||||
{
|
||||
PROCESS_ACTIVE,
|
||||
PROCESS_REQUESTING,
|
||||
PROCESS_SENDING
|
||||
};
|
||||
|
||||
struct process_t
|
||||
{
|
||||
unsigned long pid;
|
||||
int priority;
|
||||
physaddr_t page_table;
|
||||
enum process_state_t state;
|
||||
struct queue_t sending_queue;
|
||||
struct queue_t message_queue;
|
||||
struct message_t *message_buffer;
|
||||
struct process_context_t *ctx;
|
||||
};
|
||||
|
||||
struct port_t
|
||||
{
|
||||
unsigned long id;
|
||||
unsigned long owner_pid;
|
||||
};
|
||||
|
||||
struct signal_action_t
|
||||
{
|
||||
unsigned long pid;
|
||||
signal_handler_t func_ptr;
|
||||
void (*trampoline_ptr)();
|
||||
void *userdata;
|
||||
};
|
||||
|
||||
struct signal_context_t
|
||||
{
|
||||
unsigned long signal_id;
|
||||
};
|
||||
|
||||
struct kernel_t
|
||||
{
|
||||
struct syscall_t syscall_table[MAX_SYSCALL_ID];
|
||||
struct priority_queue_t priority_queue;
|
||||
struct avltree_t *interrupt_handlers;
|
||||
struct avltree_t *port_table;
|
||||
struct avltree_t *process_table;
|
||||
struct process_t *active_process;
|
||||
int next_pid;
|
||||
struct module_t modules[MODULE_LIMIT];
|
||||
};
|
||||
|
||||
void kernel_initialize(struct boot_info_t *boot_info);
|
||||
|
||||
error_t set_syscall(int id, int arg_count, int pid, void *func_ptr);
|
||||
process_t *kernel_get_process(pid_t pid);
|
||||
|
||||
size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags);
|
||||
process_t *kernel_get_active_process();
|
||||
|
||||
error_t kernel_set_syscall(int id, int arg_count, void *func_ptr);
|
||||
|
||||
size_t kernel_do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags);
|
||||
|
||||
error_t kernel_load_module(struct module_t *module);
|
||||
|
||||
unsigned long kernel_current_pid();
|
||||
pid_t kernel_current_pid();
|
||||
|
||||
struct process_context_t *kernel_current_context();
|
||||
|
||||
error_t kernel_store_active_context(struct process_context_t *context);
|
||||
|
||||
unsigned long kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space);
|
||||
pid_t kernel_spawn_process(void *program_entry, int priority, address_space_t *address_space);
|
||||
|
||||
struct process_context_t *kernel_advance_scheduler();
|
||||
|
||||
error_t kernel_terminate_process(size_t process_id);
|
||||
void kernel_schedule_process(process_t *process);
|
||||
|
||||
error_t kernel_terminate_process(pid_t process_id);
|
||||
|
||||
error_t kernel_create_port(unsigned long id);
|
||||
|
||||
error_t kernel_remove_port(unsigned long id);
|
||||
|
||||
unsigned long kernel_get_port_owner(unsigned long id);
|
||||
pid_t kernel_get_port_owner(unsigned long id);
|
||||
|
||||
error_t kernel_send_message(unsigned long recipient, struct message_t *message);
|
||||
|
||||
error_t kernel_queue_message(unsigned long recipient, struct message_t *message);
|
||||
int kernel_receive_message(struct message_t *buffer, int flags);
|
||||
|
||||
error_t kernel_register_interrupt_handler(unsigned long interrupt, signal_handler_t handler, void *userdata);
|
||||
|
||||
@@ -137,6 +89,10 @@ error_t kernel_execute_interrupt_handler(unsigned long interrupt);
|
||||
|
||||
error_t kernel_signal_return();
|
||||
|
||||
int receive_message(struct message_t *buffer, int flags);
|
||||
error_t kernel_create_object(size_t size, unsigned long flags, oid_t *id);
|
||||
|
||||
void panic(const char *message) __attribute__ ((noreturn));
|
||||
error_t kernel_attach_object(oid_t id, void *virt_addr);
|
||||
|
||||
error_t kernel_release_object(oid_t id);
|
||||
|
||||
void kernel_panic(const char *message) __attribute__ ((noreturn));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "sharedobject.h"
|
||||
#include "platform/paging.h"
|
||||
#include "types/physaddr.h"
|
||||
#include "types/status.h"
|
||||
@@ -23,7 +24,7 @@ physaddr_t reserve_pages(size_t size);
|
||||
* @param size
|
||||
* @return int
|
||||
*/
|
||||
int free_pages(physaddr_t location, size_t size);
|
||||
size_t free_pages(physaddr_t location);
|
||||
|
||||
/**
|
||||
* @brief Reserves a single page and returns its physical address.
|
||||
@@ -92,6 +93,36 @@ physaddr_t create_address_space();
|
||||
*/
|
||||
physaddr_t current_address_space();
|
||||
|
||||
/**
|
||||
* @brief Maps a region in virtual memory with the specified flags.
|
||||
*
|
||||
* @param page
|
||||
* @param frame
|
||||
* @param size
|
||||
* @param flags
|
||||
*/
|
||||
error_t map_region(void *page, physaddr_t frame, size_t size, int flags);
|
||||
|
||||
/**
|
||||
* @brief Maps a shared object into virtual memory.
|
||||
*
|
||||
* @param location
|
||||
* @param object
|
||||
* @return
|
||||
*/
|
||||
error_t map_object(void *location, shared_object_t *object);
|
||||
|
||||
/**
|
||||
* @brief Unmaps a region in virtual memory. All pages which contain some part
|
||||
* of the region specified will be unmapped.
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
*
|
||||
* @returns The physical address of the first page that was unmapped
|
||||
*/
|
||||
physaddr_t unmap_region(void *page, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Maps a single page with the specified flags.
|
||||
*
|
||||
@@ -100,7 +131,7 @@ physaddr_t current_address_space();
|
||||
* @param flags
|
||||
* @return int
|
||||
*/
|
||||
int map_page(void *page, physaddr_t frame, int flags);
|
||||
error_t map_page(void *page, physaddr_t frame, int flags);
|
||||
|
||||
/**
|
||||
* @brief Unmaps a single page, returning the physical address of the frame it
|
||||
|
||||
@@ -33,6 +33,8 @@ struct process_context_t
|
||||
|
||||
void load_context(struct process_context_t *context);
|
||||
|
||||
void context_construct(struct process_context_t *context);
|
||||
|
||||
void *get_context_pc(struct process_context_t *context);
|
||||
|
||||
void set_context_pc(struct process_context_t *context, void *pc);
|
||||
|
||||
39
include/process.h
Normal file
39
include/process.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef QUARK_PROCESS_H
|
||||
#define QUARK_PROCESS_H
|
||||
|
||||
#include "types/message.h"
|
||||
#include "types/pid.h"
|
||||
#include "types/physaddr.h"
|
||||
#include "types/status.h"
|
||||
#include "queue.h"
|
||||
#include "addressspace.h"
|
||||
#include "sharedobject.h"
|
||||
|
||||
typedef enum process_state_t
|
||||
{
|
||||
PROCESS_ACTIVE,
|
||||
PROCESS_REQUESTING,
|
||||
PROCESS_SENDING
|
||||
} process_state_t;
|
||||
|
||||
typedef struct process_t
|
||||
{
|
||||
pid_t pid;
|
||||
int priority;
|
||||
address_space_t *address_space;
|
||||
struct avltree_t *shared_objects;
|
||||
process_state_t state;
|
||||
struct queue_t message_queue;
|
||||
struct message_t *message_buffer;
|
||||
struct process_context_t *ctx;
|
||||
} process_t;
|
||||
|
||||
process_t *process_construct(pid_t pid, void *entry, void *stack, int priority, address_space_t *address_space);
|
||||
|
||||
error_t process_queue_message(process_t *process, message_t *msg);
|
||||
|
||||
error_t process_store_object(process_t *process, int id, void *location);
|
||||
|
||||
void *process_get_object(process_t *process, int id);
|
||||
|
||||
#endif
|
||||
19
include/sharedobject.h
Normal file
19
include/sharedobject.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef QUARK_SHARED_OBJECT_H
|
||||
#define QUARK_SHARED_OBJECT_H
|
||||
|
||||
#include "types/physaddr.h"
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct shared_object_t
|
||||
{
|
||||
physaddr_t phys_addr;
|
||||
size_t size;
|
||||
unsigned long access_flags;
|
||||
unsigned long refcount;
|
||||
} shared_object_t;
|
||||
|
||||
struct shared_object *create_shared_object(size_t size, unsigned long flags);
|
||||
|
||||
void destroy_shared_object(struct shared_object_t *obj);
|
||||
|
||||
#endif
|
||||
236
include/sys/syscalls.h
Normal file
236
include/sys/syscalls.h
Normal file
@@ -0,0 +1,236 @@
|
||||
#ifndef _QUARK_SYSCALLS_H
|
||||
#define _QUARK_SYSCALLS_H
|
||||
|
||||
#include <types/syscallid.h>
|
||||
#include <types/physaddr.h>
|
||||
#include <types/pid.h>
|
||||
#include <types/sigaction.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* @brief Executes a system call
|
||||
* @param id The ID of the syscall to execute
|
||||
* @return An error code, with 0 indicating success
|
||||
*/
|
||||
int _do_syscall(syscall_id_t id, ...);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param s The string to print
|
||||
* @return
|
||||
*/
|
||||
static inline int kprint(const char *s)
|
||||
{
|
||||
return _do_syscall(SYSCALL_TEST, s);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param addr
|
||||
* @param length
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int map_anon(void *addr, size_t length, long flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_MAP_ANON, addr, length, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param addr
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
static inline int unmap_anon(void *addr)
|
||||
{
|
||||
return _do_syscall(SYSCALL_UNMAP_ANON, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param addr
|
||||
* @param phys_addr
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
static inline int map_physical(void *addr, physaddr_t phys_addr, size_t length)
|
||||
{
|
||||
return _do_syscall(SYSCALL_MAP_PHYS, addr, phys_addr, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param addr
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
static inline int unmap_physical(void *addr, size_t length)
|
||||
{
|
||||
return _do_syscall(SYSCALL_UNMAP_PHYS, addr, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
static inline int open_port(int id)
|
||||
{
|
||||
return _do_syscall(SYSCALL_OPEN_PORT, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
static inline int close_port(int id)
|
||||
{
|
||||
return _do_syscall(SYSCALL_CLOSE_PORT, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param recipient
|
||||
* @param message
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int send_pid(pid_t recipient, void *message, int flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_SEND_PID, recipient, message, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param recipient
|
||||
* @param message
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int send_port(int recipient, void *message, int flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_SEND_PORT, recipient, message, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param buffer
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int receive(void *buffer, int flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_RECEIVE, buffer, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param location
|
||||
* @param size
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int create_object(void *location, size_t size, int flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_CREATE_OBJECT, location, size, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param id
|
||||
* @param location
|
||||
* @return
|
||||
*/
|
||||
static inline int aquire_object(int id, void *location)
|
||||
{
|
||||
return _do_syscall(SYSCALL_AQUIRE_OBJECT, id, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
static inline int release_object(int id)
|
||||
{
|
||||
return _do_syscall(SYSCALL_RELEASE_OBJECT, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @return
|
||||
*/
|
||||
static inline int get_pid()
|
||||
{
|
||||
return _do_syscall(SYSCALL_GET_PID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param entry
|
||||
* @param arg
|
||||
* @param stack
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int clone(void (*entry)(void*), void *arg, void *stack, int flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_CLONE, entry, arg, stack, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param id
|
||||
* @param action
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int signal_action(int id, struct signal_action_t *action, int flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_SIGNAL_ACTION, id, action, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @return
|
||||
*/
|
||||
static inline int signal_return()
|
||||
{
|
||||
return _do_syscall(SYSCALL_SIGNAL_RETURN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param pid
|
||||
* @param sigid
|
||||
* @return
|
||||
*/
|
||||
static inline int signal_raise(pid_t pid, int sigid)
|
||||
{
|
||||
return _do_syscall(SYSCALL_SIGNAL_RAISE, pid, sigid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param id
|
||||
* @param action
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
static inline int intr_action(int id, struct signal_action_t *action, int flags)
|
||||
{
|
||||
return _do_syscall(SYSCALL_INTR_ACTION, id, action, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @return
|
||||
*/
|
||||
static inline int intr_return()
|
||||
{
|
||||
return _do_syscall(SYSCALL_INTR_RETURN);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,22 +1,27 @@
|
||||
#pragma once
|
||||
#ifndef QUARK_SYSCALL_DEF_H
|
||||
#define QUARK_SYSCALL_DEF_H
|
||||
|
||||
#include "types/syscallarg.h"
|
||||
#include "types/sigaction.h"
|
||||
#include "types/pid.h"
|
||||
#include "types/oid.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef size_t (*syscall_ptr_0_t)();
|
||||
extern void *syscall_table[];
|
||||
|
||||
typedef size_t (*syscall_ptr_1_t)(syscall_arg_t);
|
||||
typedef int (*syscall_ptr_0_t)();
|
||||
|
||||
typedef size_t (*syscall_ptr_2_t)(syscall_arg_t, syscall_arg_t);
|
||||
typedef int (*syscall_ptr_1_t)(syscall_arg_t);
|
||||
|
||||
typedef size_t (*syscall_ptr_3_t)(syscall_arg_t, syscall_arg_t, syscall_arg_t);
|
||||
typedef int (*syscall_ptr_2_t)(syscall_arg_t, syscall_arg_t);
|
||||
|
||||
typedef int (*syscall_ptr_3_t)(syscall_arg_t, syscall_arg_t, syscall_arg_t);
|
||||
|
||||
struct syscall_t
|
||||
{
|
||||
bool defined;
|
||||
int arg_count;
|
||||
int process_id;
|
||||
union
|
||||
{
|
||||
syscall_ptr_0_t func_ptr_0;
|
||||
@@ -26,18 +31,106 @@ struct syscall_t
|
||||
};
|
||||
};
|
||||
|
||||
size_t test_syscall(syscall_arg_t str);
|
||||
/**
|
||||
* @brief Outputs the given string from kernel mode.
|
||||
*
|
||||
* @param str The string to output.
|
||||
*/
|
||||
int test_syscall(syscall_arg_t str);
|
||||
|
||||
size_t mmap(syscall_arg_t location, syscall_arg_t length, syscall_arg_t flags);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_map_anon(syscall_arg_t location, syscall_arg_t length, syscall_arg_t flags);
|
||||
|
||||
size_t munmap(syscall_arg_t location, syscall_arg_t length);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_unmap_anon(syscall_arg_t location);
|
||||
|
||||
size_t terminate_self();
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_map_physical(syscall_arg_t arg_addr, syscall_arg_t arg_phys_addr, syscall_arg_t arg_length);
|
||||
|
||||
size_t send(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_unmap_physical(syscall_arg_t arg_addr, syscall_arg_t arg_length);
|
||||
|
||||
size_t receive(syscall_arg_t buffer, syscall_arg_t flags);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_open_port(syscall_arg_t id);
|
||||
|
||||
size_t open_port(syscall_arg_t id);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_close_port(syscall_arg_t id);
|
||||
|
||||
size_t close_port(syscall_arg_t id);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_send_pid(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_send_port(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_receive(syscall_arg_t buffer, syscall_arg_t flags);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_create_object(void *location, size_t size, int flags);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_aquire_object(oid_t id, void *location);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_release_object(oid_t id);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_get_pid();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_clone(void (*entry)(void*), void *arg, void *stack, int flags);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_signal_action(int id, struct signal_action_t *action, int flags);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_signal_return();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_signal_raise(pid_t pid, int sigid);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_intr_action(int id, struct signal_action_t *action, int flags);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int syscall_intr_return();
|
||||
|
||||
#endif
|
||||
|
||||
13
include/types/message.h
Normal file
13
include/types/message.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef QUARK_MESSAGE_H
|
||||
#define QUARK_MESSAGE_H
|
||||
|
||||
#include <types/pid.h>
|
||||
|
||||
typedef struct message_t
|
||||
{
|
||||
pid_t sender;
|
||||
unsigned long code;
|
||||
unsigned long args[6];
|
||||
} message_t;
|
||||
|
||||
#endif
|
||||
6
include/types/oid.h
Normal file
6
include/types/oid.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef QUARK_OID_H
|
||||
#define QUARK_OID_H
|
||||
|
||||
typedef unsigned int oid_t;
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _QUARK_PHYSADDR_H
|
||||
#define _QUARK_PHYSADDR_H
|
||||
#ifndef QUARK_PHYSADDR_H
|
||||
#define QUARK_PHYSADDR_H
|
||||
|
||||
typedef unsigned long int physaddr_t;
|
||||
|
||||
|
||||
6
include/types/pid.h
Normal file
6
include/types/pid.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef QUARK_PID_H
|
||||
#define QUARK_PID_H
|
||||
|
||||
typedef unsigned int pid_t;
|
||||
|
||||
#endif
|
||||
16
include/types/sigaction.h
Normal file
16
include/types/sigaction.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef QUARK_SIGACTION_H
|
||||
#define QUARK_SIGACTION_H
|
||||
|
||||
#include "types/pid.h"
|
||||
|
||||
typedef unsigned long (*signal_handler_t)(void*, void*);
|
||||
|
||||
struct signal_action_t
|
||||
{
|
||||
pid_t pid;
|
||||
signal_handler_t func_ptr;
|
||||
void (*trampoline_ptr)();
|
||||
void *userdata;
|
||||
};
|
||||
|
||||
#endif
|
||||
10
include/types/signalid.h
Normal file
10
include/types/signalid.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef QUARK_SIGNALID_H
|
||||
#define QUARK_SIGNALID_H
|
||||
|
||||
typedef enum signalid_t
|
||||
{
|
||||
SIG_KILL = 1,
|
||||
SIG_STOP
|
||||
} signalid_t;
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _QUARK_ERROR_H
|
||||
#define _QUARK_ERROR_H
|
||||
#ifndef QUARK_ERROR_H
|
||||
#define QUARK_ERROR_H
|
||||
|
||||
typedef enum
|
||||
{
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#ifndef _QUARK_SYSCALLARG_H
|
||||
#define _QUARK_SYSCALLARG_H
|
||||
#ifndef QUARK_SYSCALLARG_H
|
||||
#define QUARK_SYSCALLARG_H
|
||||
|
||||
typedef union
|
||||
{
|
||||
long int signed_int;
|
||||
unsigned long int unsigned_int;
|
||||
void *ptr;
|
||||
char *str;
|
||||
} syscall_arg_t;
|
||||
|
||||
#endif
|
||||
@@ -4,14 +4,25 @@
|
||||
typedef enum
|
||||
{
|
||||
SYSCALL_TEST = 1,
|
||||
SYSCALL_MMAP,
|
||||
SYSCALL_MUNMAP,
|
||||
SYSCALL_YIELD,
|
||||
SYSCALL_EXIT,
|
||||
SYSCALL_SEND,
|
||||
SYSCALL_RECEIVE,
|
||||
SYSCALL_MAP_ANON,
|
||||
SYSCALL_UNMAP_ANON,
|
||||
SYSCALL_MAP_PHYS,
|
||||
SYSCALL_UNMAP_PHYS,
|
||||
SYSCALL_OPEN_PORT,
|
||||
SYSCALL_CLOSE_PORT
|
||||
SYSCALL_CLOSE_PORT,
|
||||
SYSCALL_SEND_PID,
|
||||
SYSCALL_SEND_PORT,
|
||||
SYSCALL_RECEIVE,
|
||||
SYSCALL_CREATE_OBJECT,
|
||||
SYSCALL_AQUIRE_OBJECT,
|
||||
SYSCALL_RELEASE_OBJECT,
|
||||
SYSCALL_GET_PID,
|
||||
SYSCALL_CLONE,
|
||||
SYSCALL_SIGNAL_ACTION,
|
||||
SYSCALL_SIGNAL_RETURN,
|
||||
SYSCALL_SIGNAL_RAISE,
|
||||
SYSCALL_INTR_ACTION,
|
||||
SYSCALL_INTR_RETURN
|
||||
} syscall_id_t;
|
||||
|
||||
#endif
|
||||
53
include/x86/mp.h
Normal file
53
include/x86/mp.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#define MP_SIGNATURE (uint32_t) 0x5F504D5F
|
||||
|
||||
struct mp_floating_pointer
|
||||
{
|
||||
/**
|
||||
* @brief The ASCII string "_MP_" indicates the presence of the pointer structure.
|
||||
*
|
||||
*/
|
||||
uint32_t signature;
|
||||
|
||||
/**
|
||||
* @brief The physical address of the MP configuration table. All zeroes if
|
||||
* the table does not exist.
|
||||
*
|
||||
*/
|
||||
uint32_t physical_addr;
|
||||
|
||||
/**
|
||||
* @brief The length of the floating pointer structure in 16-byte units.
|
||||
* Should contain the value '0x01'.
|
||||
*
|
||||
*/
|
||||
uint8_t length;
|
||||
|
||||
/**
|
||||
* @brief The version number of the MP specification supported.
|
||||
*
|
||||
*/
|
||||
uint8_t spec_rev;
|
||||
|
||||
/**
|
||||
* @brief A checksum of the pointer structure.
|
||||
*
|
||||
*/
|
||||
uint8_t checksum;
|
||||
|
||||
/**
|
||||
* @brief When zero, the configuration table is present. Otherwise, the
|
||||
* value indicates a default configuration.
|
||||
*
|
||||
*/
|
||||
uint8_t config_type;
|
||||
|
||||
uint8_t undef : 7;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
*/
|
||||
uint8_t imcrp : 1;
|
||||
} __attribute__ ((packed));
|
||||
@@ -1,9 +1,12 @@
|
||||
bin_PROGRAMS = quark-kernel
|
||||
quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c avltree.c queue.c math.c
|
||||
quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c avltree.c queue.c math.c process.c sharedobject.c addressspace.c
|
||||
quark_kernel_LDADD = -lgcc -lmalloc
|
||||
quark_kernel_CFLAGS = -I$(top_srcdir)/include -I$(prefix)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
|
||||
quark_kernel_LDFLAGS = -L$(prefix)/lib -nostdlib
|
||||
|
||||
lib_LIBRARIES = libquark-syscall.a
|
||||
libquark_syscall_a_CFLAGS = -I$(top_srcdir)/include -I$(prefix)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
|
||||
|
||||
if x86
|
||||
quark_kernel_SOURCES += x86/paging.c \
|
||||
x86/putc.c \
|
||||
@@ -18,6 +21,7 @@ quark_kernel_SOURCES += x86/paging.c \
|
||||
x86/preempt.S \
|
||||
x86/quark_x86.c \
|
||||
x86/entry.S
|
||||
libquark_syscall_a_SOURCES = x86/do_syscall.S
|
||||
quark_kernel_LDFLAGS += -T x86/linker.ld
|
||||
quark_kernel_DEPENDENCIES = x86/linker.ld
|
||||
endif
|
||||
|
||||
44
src/addressspace.c
Normal file
44
src/addressspace.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "addressspace.h"
|
||||
#include "heap.h"
|
||||
#include "mmgr.h"
|
||||
|
||||
address_space_t *address_space_construct()
|
||||
{
|
||||
address_space_t *as = kmalloc(sizeof(address_space_t));
|
||||
if(as == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
as->page_table = create_address_space();
|
||||
if(as->page_table == ENOMEM)
|
||||
{
|
||||
kfree(as);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
as->refcount = 0;
|
||||
return as;
|
||||
}
|
||||
|
||||
void address_space_switch(address_space_t *address_space)
|
||||
{
|
||||
paging_load_address_space(address_space->page_table);
|
||||
}
|
||||
|
||||
address_space_t *address_space_release(address_space_t *address_space)
|
||||
{
|
||||
address_space->refcount--;
|
||||
if(address_space->refcount <= 0)
|
||||
{
|
||||
address_space_destroy(address_space);
|
||||
return NULL;
|
||||
}
|
||||
return address_space;
|
||||
}
|
||||
|
||||
void address_space_destroy(address_space_t *address_space)
|
||||
{
|
||||
// TODO: Not implemented.
|
||||
kfree(address_space);
|
||||
}
|
||||
@@ -5,10 +5,13 @@
|
||||
struct avltree_t *avl_new(int key, void *value)
|
||||
{
|
||||
struct avltree_t *new_tree = kmalloc(sizeof(struct avltree_t));
|
||||
if(new_tree != NULL)
|
||||
{
|
||||
new_tree->height = 1;
|
||||
new_tree->left = new_tree->right = NULL;
|
||||
new_tree->key = key;
|
||||
new_tree->value = value;
|
||||
}
|
||||
return new_tree;
|
||||
}
|
||||
|
||||
@@ -196,3 +199,12 @@ void *avl_get(struct avltree_t *tree, int key)
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *avl_clear(struct avltree_t *tree)
|
||||
{
|
||||
while(tree != NULL)
|
||||
{
|
||||
tree = avl_remove(tree, tree->key);
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
68
src/heap.c
68
src/heap.c
@@ -1,34 +1,24 @@
|
||||
#include <stdbool.h>
|
||||
#include <libmalloc/bitmap_alloc.h>
|
||||
#include <libmalloc/list_alloc.h>
|
||||
#include "heap.h"
|
||||
#include "mmgr.h"
|
||||
#include "math.h"
|
||||
#include "stdio.h"
|
||||
#include "types/status.h"
|
||||
|
||||
bitmap_heap_descriptor_t system_heap;
|
||||
|
||||
static int map_block(void *location, unsigned long size)
|
||||
{
|
||||
for(int n = 0; n < size; n += page_size)
|
||||
{
|
||||
if(!(page_type(location + n) & PAGE_PRESENT))
|
||||
{
|
||||
int status = map_page(location + n, reserve_page(), PAGE_RW);
|
||||
if(status)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ENONE;
|
||||
}
|
||||
list_alloc_descriptor_t system_heap;
|
||||
|
||||
static int mmap_callback(void *location, unsigned long size)
|
||||
{
|
||||
size += (unsigned long)location % page_size;
|
||||
location -= (unsigned long)location % page_size;
|
||||
int status = ENONE;
|
||||
for(unsigned long i = 0; i < size; i += page_size)
|
||||
{
|
||||
if((page_type(location + i) & PAGE_PRESENT))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
physaddr_t frame = reserve_page();
|
||||
if(frame == ENOMEM)
|
||||
{
|
||||
@@ -42,44 +32,48 @@ static int mmap_callback(void *location, unsigned long size)
|
||||
return status;
|
||||
}
|
||||
|
||||
int kminit(void *base, void* start, size_t heap_size, size_t block_size)
|
||||
int kminit(void *base, size_t heap_size)
|
||||
{
|
||||
static unsigned long heap_cache[16];
|
||||
system_heap.bitmap = NULL;
|
||||
system_heap.cache = heap_cache;
|
||||
system_heap.cache_capacity = 16;
|
||||
system_heap.block_bits = 4;
|
||||
system_heap.block_size = block_size;
|
||||
system_heap.offset = (unsigned long)base;
|
||||
memory_region_t map_array[8];
|
||||
memory_map_t map = {
|
||||
.array = map_array,
|
||||
.capacity = 8,
|
||||
.size = 0
|
||||
};
|
||||
memmap_insert_region(&map, 0, heap_size, M_AVAILABLE);
|
||||
memmap_insert_region(&map, 0, start - base, M_UNAVAILABLE);
|
||||
return initialize_heap(&system_heap, &map, mmap_callback);
|
||||
memmap_insert_region(&map, base, heap_size, M_AVAILABLE);
|
||||
physaddr_t phys_addr = reserve_pages(heap_size);
|
||||
if(phys_addr == ENOMEM)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
for(unsigned long off = 0; off < heap_size; off += page_size)
|
||||
{
|
||||
if((page_type(base + off) & PAGE_PRESENT))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if(map_page(base + off, phys_addr + off, PAGE_RW))
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
}
|
||||
return list_alloc_init(&system_heap, &map);
|
||||
}
|
||||
|
||||
void *kmalloc(size_t size)
|
||||
{
|
||||
unsigned long loc = reserve_region(&system_heap, size);
|
||||
if(loc == NOMEM)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else if(map_block((void*)loc, size))
|
||||
void *p = list_alloc_reserve(&system_heap, size);
|
||||
if(p == NOMEM)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (void*)loc;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
void kfree(void *ptr)
|
||||
{
|
||||
free_region(&system_heap, (unsigned long)ptr, 0);
|
||||
list_alloc_free(&system_heap, (unsigned long)ptr);
|
||||
}
|
||||
|
||||
318
src/kernel.c
318
src/kernel.c
@@ -7,13 +7,58 @@
|
||||
#include "string.h"
|
||||
#include "config.h"
|
||||
#include "system.h"
|
||||
#include "process.h"
|
||||
#include "platform/interrupts.h"
|
||||
#include "platform/context.h"
|
||||
#include "platform/putc.h"
|
||||
#include "types/status.h"
|
||||
#include "types/syscallid.h"
|
||||
#include "types/sigaction.h"
|
||||
|
||||
struct kernel_t kernel;
|
||||
void *syscall_table[] = {
|
||||
NULL,
|
||||
(void*)test_syscall,
|
||||
(void*)syscall_map_anon,
|
||||
(void*)syscall_unmap_anon,
|
||||
(void*)syscall_map_physical,
|
||||
(void*)syscall_unmap_physical,
|
||||
(void*)syscall_open_port,
|
||||
(void*)syscall_close_port,
|
||||
(void*)syscall_send_pid,
|
||||
(void*)syscall_receive,
|
||||
(void*)syscall_create_object,
|
||||
(void*)syscall_aquire_object,
|
||||
(void*)syscall_release_object,
|
||||
(void*)syscall_get_pid,
|
||||
(void*)syscall_clone,
|
||||
(void*)syscall_signal_action,
|
||||
(void*)syscall_signal_return,
|
||||
(void*)syscall_signal_raise
|
||||
};
|
||||
|
||||
struct port_t
|
||||
{
|
||||
unsigned long id;
|
||||
pid_t owner_pid;
|
||||
};
|
||||
|
||||
struct signal_context_t
|
||||
{
|
||||
unsigned long signal_id;
|
||||
};
|
||||
|
||||
struct kernel_t
|
||||
{
|
||||
struct syscall_t syscall_table[MAX_SYSCALL_ID];
|
||||
struct priority_queue_t priority_queue;
|
||||
struct avltree_t *interrupt_handlers;
|
||||
struct avltree_t *port_table;
|
||||
struct avltree_t *object_table;
|
||||
struct avltree_t *process_table;
|
||||
struct process_t *active_process;
|
||||
pid_t next_pid;
|
||||
oid_t next_oid;
|
||||
} kernel;
|
||||
|
||||
void kernel_initialize(struct boot_info_t *boot_info)
|
||||
{
|
||||
@@ -38,48 +83,72 @@ void kernel_initialize(struct boot_info_t *boot_info)
|
||||
memmap_insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE);
|
||||
if(initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size))
|
||||
{
|
||||
panic("Failed to initialize page allocator.");
|
||||
kernel_panic("Failed to initialize page allocator.");
|
||||
}
|
||||
|
||||
if(kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64))
|
||||
if(kminit(page_map_end(), 0xFFC00000 - (size_t)page_map_end()))
|
||||
{
|
||||
panic("Failed to initialize heap.");
|
||||
kernel_panic("Failed to initialize heap.");
|
||||
}
|
||||
|
||||
kernel.active_process = NULL;
|
||||
kernel.next_pid = 1;
|
||||
kernel.next_oid = 1;
|
||||
kernel.process_table = NULL;
|
||||
kernel.port_table = NULL;
|
||||
kernel.object_table = NULL;
|
||||
if(construct_priority_queue(&kernel.priority_queue, 512) != ENONE)
|
||||
{
|
||||
panic("Failed to construct priority queue.");
|
||||
kernel_panic("Failed to construct priority queue.");
|
||||
}
|
||||
memset(kernel.syscall_table, 0, sizeof(struct syscall_t) * MAX_SYSCALL_ID);
|
||||
set_syscall(SYSCALL_TEST, 1, 0, test_syscall);
|
||||
set_syscall(SYSCALL_MMAP, 3, 0, mmap);
|
||||
set_syscall(SYSCALL_MUNMAP, 2, 0, munmap);
|
||||
set_syscall(SYSCALL_SEND, 3, 0, send);
|
||||
set_syscall(SYSCALL_RECEIVE, 2, 0, receive);
|
||||
set_syscall(SYSCALL_OPEN_PORT, 1, 0, open_port);
|
||||
set_syscall(SYSCALL_CLOSE_PORT, 1, 0, close_port);
|
||||
kernel_set_syscall(SYSCALL_TEST, 1, test_syscall);
|
||||
kernel_set_syscall(SYSCALL_MAP_ANON, 3, syscall_map_anon);
|
||||
kernel_set_syscall(SYSCALL_UNMAP_ANON, 1, syscall_unmap_anon);
|
||||
kernel_set_syscall(SYSCALL_MAP_PHYS, 3, syscall_map_physical);
|
||||
kernel_set_syscall(SYSCALL_UNMAP_PHYS, 2, syscall_unmap_physical);
|
||||
kernel_set_syscall(SYSCALL_OPEN_PORT, 1, syscall_open_port);
|
||||
kernel_set_syscall(SYSCALL_CLOSE_PORT, 1, syscall_close_port);
|
||||
kernel_set_syscall(SYSCALL_SEND_PID, 3, syscall_send_pid);
|
||||
kernel_set_syscall(SYSCALL_SEND_PORT, 3, syscall_send_port);
|
||||
kernel_set_syscall(SYSCALL_RECEIVE, 2, syscall_receive);
|
||||
kernel_set_syscall(SYSCALL_CREATE_OBJECT, 3, syscall_create_object);
|
||||
kernel_set_syscall(SYSCALL_AQUIRE_OBJECT, 2, syscall_aquire_object);
|
||||
kernel_set_syscall(SYSCALL_RELEASE_OBJECT, 1, syscall_release_object);
|
||||
kernel_set_syscall(SYSCALL_GET_PID, 0, syscall_get_pid);
|
||||
kernel_set_syscall(SYSCALL_SIGNAL_ACTION, 3, syscall_signal_action);
|
||||
kernel_set_syscall(SYSCALL_SIGNAL_RETURN, 0, syscall_signal_return);
|
||||
kernel_set_syscall(SYSCALL_SIGNAL_RAISE, 2, syscall_signal_raise);
|
||||
kernel_set_syscall(SYSCALL_INTR_ACTION, 3, syscall_intr_action);
|
||||
kernel_set_syscall(SYSCALL_INTR_RETURN, 0, syscall_intr_return);
|
||||
for(int i = 0; i < boot_info->module_count; i++)
|
||||
{
|
||||
if(kernel_load_module(&boot_info->modules[i]) != ENONE)
|
||||
{
|
||||
panic("Failed to load modules.");
|
||||
kernel_panic("Failed to load modules.");
|
||||
}
|
||||
}
|
||||
|
||||
if(initialize_interrupts() != ENONE)
|
||||
{
|
||||
panic("Failed to initialize interrupts.");
|
||||
kernel_panic("Failed to initialize interrupts.");
|
||||
}
|
||||
|
||||
irq_enable();
|
||||
load_context(kernel_advance_scheduler());
|
||||
}
|
||||
|
||||
error_t set_syscall(int id, int arg_count, int pid, void *func_ptr)
|
||||
process_t *kernel_get_process(pid_t pid)
|
||||
{
|
||||
return avl_get(kernel.process_table, pid);
|
||||
}
|
||||
|
||||
process_t *kernel_get_active_process()
|
||||
{
|
||||
return kernel.active_process;
|
||||
}
|
||||
|
||||
error_t kernel_set_syscall(int id, int arg_count, void *func_ptr)
|
||||
{
|
||||
if(id < 0 || id > MAX_SYSCALL_ID)
|
||||
{
|
||||
@@ -93,22 +162,17 @@ error_t set_syscall(int id, int arg_count, int pid, void *func_ptr)
|
||||
{
|
||||
return EINVALIDARG;
|
||||
}
|
||||
else if(pid != 0 && avl_get(kernel.process_table, pid) == NULL)
|
||||
{
|
||||
return EDOESNTEXIST;
|
||||
}
|
||||
else if(func_ptr == NULL)
|
||||
{
|
||||
return ENULLPTR;
|
||||
}
|
||||
kernel.syscall_table[id].defined = true;
|
||||
kernel.syscall_table[id].arg_count = arg_count;
|
||||
kernel.syscall_table[id].process_id = pid;
|
||||
kernel.syscall_table[id].func_ptr_0 = func_ptr;
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags)
|
||||
size_t kernel_do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags)
|
||||
{
|
||||
if(id < 0 || id > MAX_SYSCALL_ID)
|
||||
{
|
||||
@@ -118,18 +182,6 @@ size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, sysca
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
bool switched_address_space = false;
|
||||
if(kernel.syscall_table[id].process_id > 0)
|
||||
{
|
||||
struct process_t *callee = avl_get(kernel.process_table, kernel.syscall_table[id].process_id);
|
||||
if(callee == NULL)
|
||||
{
|
||||
kernel.syscall_table[id].defined = false;
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
paging_load_address_space(callee->page_table);
|
||||
switched_address_space = true;
|
||||
}
|
||||
set_context_pc(kernel.active_process->ctx, pc);
|
||||
set_context_stack(kernel.active_process->ctx, stack);
|
||||
set_context_flags(kernel.active_process->ctx, flags);
|
||||
@@ -149,59 +201,43 @@ size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, sysca
|
||||
result = kernel.syscall_table[id].func_ptr_3(arg1, arg2, arg3);
|
||||
break;
|
||||
}
|
||||
if(switched_address_space)
|
||||
{
|
||||
paging_load_address_space(kernel.active_process->page_table);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
error_t kernel_load_module(struct module_t *module)
|
||||
{
|
||||
physaddr_t module_address_space = create_address_space();
|
||||
if(module_address_space == ENOMEM) {
|
||||
panic("failed to create address space for module: out of memory");
|
||||
address_space_t *module_address_space = address_space_construct();
|
||||
if(module_address_space == NULL) {
|
||||
kernel_panic("failed to create address space for module: out of memory");
|
||||
}
|
||||
paging_load_address_space(module_address_space);
|
||||
address_space_switch(module_address_space);
|
||||
void *const load_base = (void*)0x80000000;
|
||||
size_t load_offset = 0;
|
||||
for(physaddr_t p = module->start & ~(page_size - 1); p < module->end; p += page_size)
|
||||
{
|
||||
int status = map_page(load_base + load_offset, p, PAGE_RW);
|
||||
switch(status)
|
||||
{
|
||||
case ENOMEM:
|
||||
panic("ran out of memory while mapping module");
|
||||
case EOUTOFBOUNDS:
|
||||
panic("got out-of-bounds error while mapping module");
|
||||
}
|
||||
load_offset += page_size;
|
||||
|
||||
}
|
||||
physaddr_t p = module->start & ~(page_size - 1);
|
||||
map_region(load_base, p, module->end - p, PAGE_RW);
|
||||
int status = load_program(load_base);
|
||||
switch(status)
|
||||
{
|
||||
case ENOMEM:
|
||||
panic("ran out of memory while reading ELF file");
|
||||
kernel_panic("ran out of memory while reading ELF file");
|
||||
case EOUTOFBOUNDS:
|
||||
panic("got out-of-bounds error while reading ELF file");
|
||||
kernel_panic("got out-of-bounds error while reading ELF file");
|
||||
}
|
||||
void *module_entry = ((struct elf_file_header_t*)load_base)->entry;
|
||||
printf("loaded module with entry point %08x\n", (unsigned int)module_entry);
|
||||
load_offset = 0;
|
||||
size_t load_offset = 0;
|
||||
for(physaddr_t p = module->start & ~(page_size - 1); p < module->end; p += page_size)
|
||||
{
|
||||
int status = unmap_page(load_base + load_offset);
|
||||
switch(status)
|
||||
{
|
||||
case ENOMEM:
|
||||
panic("ran out of memory while unmapping module");
|
||||
kernel_panic("ran out of memory while unmapping module");
|
||||
case EOUTOFBOUNDS:
|
||||
panic("got out-of-bounds error while unmapping module");
|
||||
kernel_panic("got out-of-bounds error while unmapping module");
|
||||
}
|
||||
load_offset += page_size;
|
||||
}
|
||||
if(kernel_spawn_process(module_entry, 1, current_address_space()) > 0)
|
||||
if(kernel_spawn_process(module_entry, 1, module_address_space) > 0)
|
||||
{
|
||||
return ENONE;
|
||||
}
|
||||
@@ -211,7 +247,7 @@ error_t kernel_load_module(struct module_t *module)
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long kernel_current_pid()
|
||||
pid_t kernel_current_pid()
|
||||
{
|
||||
if(kernel.active_process == NULL)
|
||||
{
|
||||
@@ -235,36 +271,22 @@ struct process_context_t *kernel_current_context()
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space)
|
||||
pid_t kernel_spawn_process(void *program_entry, int priority, address_space_t *address_space)
|
||||
{
|
||||
struct process_t *new_process = (struct process_t*) kmalloc(sizeof(struct process_t));
|
||||
if(new_process == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
struct process_context_t *initial_context = kmalloc(sizeof(struct process_context_t));
|
||||
if(initial_context == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
physaddr_t stack_page = reserve_page();
|
||||
if(stack_page % page_size)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
map_page((void*)&_kernel_start - page_size, stack_page, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW);
|
||||
memset(initial_context, 0, sizeof(struct process_context_t));
|
||||
set_context_pc(initial_context, program_entry);
|
||||
set_context_flags(initial_context, DEFAULT_FLAGS);
|
||||
set_context_stack(initial_context, &_kernel_start);
|
||||
new_process->priority = priority;
|
||||
new_process->pid = kernel.next_pid;
|
||||
new_process->page_table = address_space;
|
||||
new_process->state = PROCESS_ACTIVE;
|
||||
new_process->message_buffer = NULL;
|
||||
new_process->ctx = initial_context;
|
||||
queue_construct(&new_process->sending_queue);
|
||||
queue_construct(&new_process->message_queue);
|
||||
struct process_t *new_process = process_construct(kernel.next_pid, &_kernel_start, program_entry, priority, address_space);
|
||||
if(new_process == NULL)
|
||||
{
|
||||
free_page(stack_page);
|
||||
return 0;
|
||||
}
|
||||
|
||||
kernel.process_table = avl_insert(kernel.process_table, new_process->pid, new_process);
|
||||
priorityqueue_insert(&kernel.priority_queue, new_process, new_process->priority);
|
||||
kernel.next_pid++;
|
||||
@@ -280,16 +302,24 @@ struct process_context_t *kernel_advance_scheduler()
|
||||
kernel.active_process = priorityqueue_extract_min(&kernel.priority_queue);
|
||||
if(kernel.active_process != NULL)
|
||||
{
|
||||
paging_load_address_space(kernel.active_process->page_table);
|
||||
printf("entering process %08x cr3=%08x ctx=%08x sched=%i.\n", kernel.active_process->pid, kernel.active_process->page_table, kernel.active_process->ctx, kernel.priority_queue.size);
|
||||
address_space_switch(kernel.active_process->address_space);
|
||||
printf("entering process %08x cr3=%08x ctx=%08x sched=%i.\n", kernel.active_process->pid, kernel.active_process->address_space->page_table, kernel.active_process->ctx, kernel.priority_queue.size);
|
||||
return kernel.active_process->ctx;
|
||||
}
|
||||
panic("no processes available to enter!");
|
||||
kernel_panic("no processes available to enter!");
|
||||
}
|
||||
|
||||
error_t kernel_terminate_process(size_t process_id)
|
||||
void kernel_schedule_process(process_t *process)
|
||||
{
|
||||
struct process_t *process = avl_get(kernel.process_table, process_id);
|
||||
if(priorityqueue_insert(&kernel.priority_queue, process, process->priority))
|
||||
{
|
||||
kernel_panic("Failed to schedule process!");
|
||||
}
|
||||
}
|
||||
|
||||
error_t kernel_terminate_process(pid_t pid)
|
||||
{
|
||||
struct process_t *process = avl_get(kernel.process_table, pid);
|
||||
if(process == NULL)
|
||||
{
|
||||
return EDOESNTEXIST;
|
||||
@@ -298,18 +328,12 @@ error_t kernel_terminate_process(size_t process_id)
|
||||
{
|
||||
kernel.active_process = NULL;
|
||||
}
|
||||
kernel.process_table = avl_remove(kernel.process_table, process_id);
|
||||
kernel.process_table = avl_remove(kernel.process_table, pid);
|
||||
priorityqueue_remove(&kernel.priority_queue, process);
|
||||
for(struct message_t *msg = queue_get_next(&process->message_queue); msg != NULL; msg = queue_get_next(&process->message_queue))
|
||||
{
|
||||
kfree(msg);
|
||||
}
|
||||
for(struct process_t *sender = queue_get_next(&process->sending_queue); sender != NULL; sender = queue_get_next(&process->sending_queue))
|
||||
{
|
||||
sender->state = PROCESS_ACTIVE;
|
||||
set_context_return(sender->ctx, EEXITED);
|
||||
priorityqueue_insert(&kernel.priority_queue, sender, sender->priority);
|
||||
}
|
||||
kfree(process->ctx);
|
||||
kfree(process);
|
||||
return ENONE;
|
||||
@@ -359,7 +383,7 @@ error_t kernel_remove_port(unsigned long id)
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
unsigned long kernel_get_port_owner(unsigned long id)
|
||||
pid_t kernel_get_port_owner(unsigned long id)
|
||||
{
|
||||
struct port_t *port = avl_get(kernel.port_table, id);
|
||||
if(port == NULL)
|
||||
@@ -372,56 +396,7 @@ unsigned long kernel_get_port_owner(unsigned long id)
|
||||
}
|
||||
}
|
||||
|
||||
error_t kernel_send_message(unsigned long recipient, struct message_t *message)
|
||||
{
|
||||
struct process_t *dest = avl_get(kernel.process_table, recipient);
|
||||
if(dest == NULL)
|
||||
{
|
||||
return EDOESNTEXIST;
|
||||
}
|
||||
else if(dest->message_buffer != NULL && dest->state == PROCESS_REQUESTING)
|
||||
{
|
||||
printf("Sending message directly from %i to %i\n", kernel.active_process->pid, dest->pid);
|
||||
struct message_t kernel_buffer;
|
||||
memcpy(&kernel_buffer, message, sizeof(struct message_t));
|
||||
kernel_buffer.sender = kernel.active_process->pid;
|
||||
paging_load_address_space(dest->page_table);
|
||||
memcpy(dest->message_buffer, &kernel_buffer, sizeof(struct message_t));
|
||||
paging_load_address_space(kernel.active_process->page_table);
|
||||
dest->message_buffer = NULL;
|
||||
dest->state = PROCESS_ACTIVE;
|
||||
set_context_return(dest->ctx, ENONE);
|
||||
priorityqueue_insert(&kernel.priority_queue, dest, dest->priority);
|
||||
return ENONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EBUSY;
|
||||
}
|
||||
}
|
||||
|
||||
error_t kernel_queue_message(unsigned long recipient, struct message_t *message)
|
||||
{
|
||||
struct process_t *dest = avl_get(kernel.process_table, recipient);
|
||||
if(dest != NULL)
|
||||
{
|
||||
printf("Queueing message from %i to %i\n", kernel.active_process->pid, dest->pid);
|
||||
struct message_t *queued_msg = kmalloc(sizeof(struct message_t));
|
||||
if(queued_msg == NULL)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
memcpy(queued_msg, message, sizeof(struct message_t));
|
||||
queue_insert(&dest->message_queue, queued_msg);
|
||||
return ENONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EDOESNTEXIST;
|
||||
}
|
||||
}
|
||||
|
||||
int receive_message(struct message_t *buffer, int flags)
|
||||
int kernel_receive_message(struct message_t *buffer, int flags)
|
||||
{
|
||||
if(kernel.active_process->message_queue.count > 0)
|
||||
{
|
||||
@@ -484,7 +459,7 @@ error_t kernel_execute_interrupt_handler(unsigned long interrupt)
|
||||
return EDOESNTEXIST;
|
||||
}
|
||||
|
||||
paging_load_address_space(process->page_table);
|
||||
address_space_switch(process->address_space);
|
||||
|
||||
struct signal_context_t siginfo = {
|
||||
.signal_id = interrupt
|
||||
@@ -499,7 +474,7 @@ error_t kernel_execute_interrupt_handler(unsigned long interrupt)
|
||||
priorityqueue_insert(&kernel.priority_queue, process, process->priority);
|
||||
}
|
||||
|
||||
paging_load_address_space(kernel.active_process->page_table);
|
||||
address_space_switch(kernel.active_process->address_space);
|
||||
|
||||
return ENONE;
|
||||
}
|
||||
@@ -511,13 +486,52 @@ error_t kernel_signal_return()
|
||||
context_stack_pop_struct(kernel.active_process->ctx, kernel.active_process->ctx, sizeof(*kernel.active_process->ctx));
|
||||
if(kernel.active_process->state == PROCESS_REQUESTING)
|
||||
{
|
||||
receive_message(kernel.active_process->message_buffer, 0);
|
||||
kernel_receive_message(kernel.active_process->message_buffer, 0);
|
||||
load_context(kernel.active_process->ctx);
|
||||
}
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
void panic(const char *message)
|
||||
error_t kernel_create_object(size_t size, unsigned long flags, oid_t *id)
|
||||
{
|
||||
physaddr_t phys_addr = reserve_pages(size);
|
||||
if(phys_addr == ENOMEM)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
struct shared_object_t *obj = kmalloc(sizeof(struct shared_object_t));
|
||||
if(obj == NULL)
|
||||
{
|
||||
free_pages(phys_addr);
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
obj->phys_addr = phys_addr;
|
||||
obj->size = size;
|
||||
obj->access_flags = flags;
|
||||
obj->refcount = 0;
|
||||
kernel.object_table = avl_insert(kernel.object_table, kernel.next_oid, obj);
|
||||
*id = kernel.next_oid;
|
||||
kernel.next_oid++;
|
||||
if(kernel.next_oid <= 0)
|
||||
{
|
||||
kernel.next_oid = 1;
|
||||
}
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
error_t kernel_attach_object(oid_t id, void *virt_addr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
error_t kernel_release_object(oid_t id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void kernel_panic(const char *message)
|
||||
{
|
||||
printf("panic: %s", message);
|
||||
asm("cli");
|
||||
|
||||
84
src/mmgr.c
84
src/mmgr.c
@@ -4,17 +4,18 @@
|
||||
#include "platform/paging.h"
|
||||
#include "types/status.h"
|
||||
#include "stdio.h"
|
||||
#include <libmalloc/bitmap_alloc.h>
|
||||
#include <libmalloc/buddy_alloc.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_CACHE_SIZE 32
|
||||
#define AVAIL_LIST_SIZE 20
|
||||
|
||||
bitmap_heap_descriptor_t page_map;
|
||||
buddy_descriptor_t page_alloc;
|
||||
|
||||
physaddr_t reserve_pages(size_t size)
|
||||
{
|
||||
unsigned long location = reserve_region(&page_map, size);
|
||||
unsigned long location = buddy_reserve(&page_alloc, size);
|
||||
printf("Reserved >=%08x pages at %08x\n", size, location);
|
||||
if(location != NOMEM)
|
||||
{
|
||||
return location;
|
||||
@@ -25,63 +26,61 @@ physaddr_t reserve_pages(size_t size)
|
||||
}
|
||||
}
|
||||
|
||||
int free_pages(physaddr_t location, size_t size)
|
||||
size_t free_pages(physaddr_t location)
|
||||
{
|
||||
free_region(&page_map, location, size);
|
||||
return ENONE;
|
||||
return buddy_free(&page_alloc, location);
|
||||
}
|
||||
|
||||
physaddr_t reserve_page()
|
||||
{
|
||||
unsigned long loc = reserve_region(&page_map, page_size);
|
||||
unsigned long loc = buddy_reserve(&page_alloc, page_size);
|
||||
printf("Reserved %08x\n", loc);
|
||||
if(loc == NOMEM)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Reserved %08x\n", loc);
|
||||
return loc;
|
||||
}
|
||||
}
|
||||
|
||||
int free_page(physaddr_t location)
|
||||
{
|
||||
free_region(&page_map, location, page_size);
|
||||
buddy_free_size(&page_alloc, location, page_size);
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
size_t free_page_count()
|
||||
{
|
||||
return page_map.free_block_count;
|
||||
return page_alloc.free_block_count;
|
||||
}
|
||||
|
||||
void *page_map_base()
|
||||
{
|
||||
return (void*)page_map.bitmap;
|
||||
return (void*)page_alloc.block_map;
|
||||
}
|
||||
|
||||
void *page_map_end()
|
||||
{
|
||||
return (void*)page_map.bitmap + page_map.bitmap_size;
|
||||
return (void*)page_alloc.block_map + page_alloc.block_map_size;
|
||||
}
|
||||
|
||||
error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, unsigned long block_size)
|
||||
{
|
||||
static unsigned long page_map_cache[MAX_CACHE_SIZE];
|
||||
static unsigned long avail_list[AVAIL_LIST_SIZE];
|
||||
// Round memory_size up to nearest power of 2
|
||||
memory_size = 1 << llog2(memory_size);
|
||||
|
||||
page_map.block_size = block_size;
|
||||
page_map.block_bits = 1;
|
||||
page_map.offset = 0;
|
||||
page_map.cache = page_map_cache;
|
||||
page_map.cache_capacity = MAX_CACHE_SIZE;
|
||||
page_map.bitmap = (unsigned long*) base;
|
||||
page_alloc.avail = avail_list;
|
||||
page_alloc.block_map = (buddy_block_t*) base;
|
||||
page_alloc.block_size = block_size;
|
||||
page_alloc.mmap = NULL;
|
||||
page_alloc.offset = 0;
|
||||
|
||||
/* Allocate pages for bitmap */
|
||||
int pages_mapped = 0;
|
||||
int pages_needed = (bitmap_size(map, block_size, 1) + page_size - 1) / page_size;
|
||||
int pages_needed = (buddy_map_size(map, block_size) + page_size - 1) / page_size;
|
||||
for(int i = 0; i < map->size && (pages_mapped < pages_needed); i++)
|
||||
{
|
||||
if(map->array[i].type != M_AVAILABLE)
|
||||
@@ -92,7 +91,7 @@ error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, u
|
||||
physaddr_t region_end = map->array[i].location + map->array[i].size;
|
||||
while(location + page_size <= region_end && (pages_mapped < pages_needed))
|
||||
{
|
||||
void *page = (void*)page_map.bitmap + pages_mapped * page_size;
|
||||
void *page = (void*)page_alloc.block_map + pages_mapped * page_size;
|
||||
for(int level = 0; level < page_table_levels; level++)
|
||||
{
|
||||
if(!(get_pte_type(page, level) & PAGE_PRESENT))
|
||||
@@ -118,7 +117,7 @@ error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, u
|
||||
}
|
||||
}
|
||||
|
||||
if(initialize_heap(&page_map, map, NULL))
|
||||
if(buddy_alloc_init(&page_alloc, map))
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
@@ -151,26 +150,35 @@ physaddr_t current_address_space()
|
||||
return paging_current_address_space();
|
||||
}
|
||||
|
||||
int map_page(void *page, physaddr_t frame, int flags)
|
||||
error_t map_region(void *page, physaddr_t frame, size_t size, int flags)
|
||||
{
|
||||
if(frame % page_size != 0)
|
||||
{
|
||||
return EINVALIDARG;
|
||||
}
|
||||
for(size_t p = 0; p < size; p += page_size)
|
||||
{
|
||||
set_pte(page + p, page_table_levels - 1, PAGE_PRESENT | flags, frame + p);
|
||||
}
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
physaddr_t unmap_region(void *page, size_t size)
|
||||
{
|
||||
physaddr_t frame = get_pte_address(page, page_table_levels - 1);
|
||||
for(size_t p = 0; p < size; p += page_size)
|
||||
{
|
||||
set_pte(page + p, page_table_levels - 1, 0, 0);
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
error_t map_page(void *page, physaddr_t frame, int flags)
|
||||
{
|
||||
if (frame % page_size != 0)
|
||||
{
|
||||
return EINVALIDARG;
|
||||
}
|
||||
for(int level = 0; level < page_table_levels - 1; level++)
|
||||
{
|
||||
int present = get_pte_type(page, level) & PAGE_PRESENT;
|
||||
if(present == 0)
|
||||
{
|
||||
physaddr_t new_table = reserve_page();
|
||||
if(new_table == ENOMEM)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
set_pte(page, level, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW, new_table);
|
||||
wipe_page_table(page, level + 1);
|
||||
}
|
||||
}
|
||||
set_pte(page, page_table_levels - 1, PAGE_PRESENT | flags, frame);
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
70
src/process.c
Normal file
70
src/process.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "process.h"
|
||||
#include "heap.h"
|
||||
#include "mmgr.h"
|
||||
#include "platform/context.h"
|
||||
#include "string.h"
|
||||
#include "avltree.h"
|
||||
|
||||
process_t *process_construct(pid_t pid, void *entry, void *stack, int priority, address_space_t *address_space)
|
||||
{
|
||||
process_t *new_process = (process_t*) kmalloc(sizeof(process_t));
|
||||
if(new_process == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct process_context_t *initial_context = kmalloc(sizeof(struct process_context_t));
|
||||
if(initial_context == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(address_space == NULL
|
||||
&& (address_space = address_space_construct()) == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
context_construct(initial_context);
|
||||
set_context_pc(initial_context, entry);
|
||||
set_context_stack(initial_context, stack);
|
||||
new_process->priority = priority;
|
||||
new_process->pid = pid;
|
||||
new_process->address_space = address_space;
|
||||
new_process->address_space->refcount++;
|
||||
new_process->shared_objects = NULL;
|
||||
new_process->state = PROCESS_ACTIVE;
|
||||
new_process->message_buffer = NULL;
|
||||
new_process->ctx = initial_context;
|
||||
queue_construct(&new_process->message_queue);
|
||||
return new_process;
|
||||
}
|
||||
|
||||
error_t process_queue_message(process_t *process, message_t *msg)
|
||||
{
|
||||
return queue_insert(&process->message_queue, msg);
|
||||
}
|
||||
|
||||
error_t process_receive_message(process_t *process, message_t *buffer)
|
||||
{
|
||||
if(process->message_queue.count == 0)
|
||||
{
|
||||
return EDOESNTEXIST;
|
||||
}
|
||||
|
||||
message_t *queued_message = queue_get_next(&process->message_queue);
|
||||
memcpy(buffer, queued_message, sizeof(message_t));
|
||||
kfree(queued_message);
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
error_t process_store_object(process_t *process, int id, void *location)
|
||||
{
|
||||
process->shared_objects = avl_insert(process->shared_objects, id, location);
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
void *process_get_object(process_t *process, int id)
|
||||
{
|
||||
return avl_get(process->shared_objects, id);
|
||||
}
|
||||
35
src/sharedobject.c
Normal file
35
src/sharedobject.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "sharedobject.h"
|
||||
#include "avltree.h"
|
||||
#include "mmgr.h"
|
||||
#include "heap.h"
|
||||
|
||||
struct shared_object *create_shared_object(size_t size, unsigned long flags)
|
||||
{
|
||||
physaddr_t phys_addr = reserve_pages(size);
|
||||
if(phys_addr == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct shared_object_t *obj = kmalloc(sizeof(struct shared_object_t));
|
||||
if(obj == NULL)
|
||||
{
|
||||
free_pages(phys_addr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
obj->phys_addr = phys_addr;
|
||||
obj->size = size;
|
||||
obj->access_flags = flags;
|
||||
obj->refcount = 0;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void destroy_shared_object(struct shared_object_t *obj)
|
||||
{
|
||||
if(obj != NULL)
|
||||
{
|
||||
free_pages(obj->phys_addr);
|
||||
kfree(obj);
|
||||
}
|
||||
}
|
||||
13
src/stdio.c
13
src/stdio.c
@@ -18,7 +18,18 @@ enum format_flags_t
|
||||
|
||||
};
|
||||
|
||||
char *itoa(unsigned int n, unsigned int base, unsigned int width)
|
||||
typedef enum
|
||||
{
|
||||
LOG_PANIC,
|
||||
LOG_ERROR,
|
||||
LOG_WARNING,
|
||||
LOG_INFO,
|
||||
LOG_DEBUG
|
||||
} log_levels_t;
|
||||
|
||||
|
||||
|
||||
static char *itoa(unsigned int n, unsigned int base, unsigned int width)
|
||||
{
|
||||
if (base < 2 || base > 16)
|
||||
{
|
||||
|
||||
287
src/syscalls.c
287
src/syscalls.c
@@ -2,16 +2,23 @@
|
||||
#include "kernel.h"
|
||||
#include "mmgr.h"
|
||||
#include "stdio.h"
|
||||
#include "process.h"
|
||||
#include "sharedobject.h"
|
||||
#include "system.h"
|
||||
#include "heap.h"
|
||||
#include "platform/context.h"
|
||||
#include "types/status.h"
|
||||
|
||||
size_t test_syscall(syscall_arg_t str)
|
||||
int test_syscall(syscall_arg_t str)
|
||||
{
|
||||
if(str.ptr != NULL)
|
||||
{
|
||||
printf("%s", (char*)str.ptr);
|
||||
}
|
||||
return 17;
|
||||
}
|
||||
|
||||
size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t arg_flags)
|
||||
int syscall_map_anon(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t arg_flags)
|
||||
{
|
||||
unsigned long location = arg_location.unsigned_int;
|
||||
unsigned long length = arg_length.unsigned_int;
|
||||
@@ -24,6 +31,7 @@ size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t
|
||||
{
|
||||
return ENULLPTR;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < length; i += page_size)
|
||||
{
|
||||
if(page_type((void*)(location + i)) & PAGE_PRESENT)
|
||||
@@ -31,107 +39,226 @@ size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t
|
||||
return EEXISTS;
|
||||
}
|
||||
}
|
||||
size_t n = 0;
|
||||
|
||||
physaddr_t frame = reserve_pages(length);
|
||||
if(frame % page_size != 0)
|
||||
{
|
||||
return frame % page_size;
|
||||
}
|
||||
|
||||
error_t status = map_region(location, frame, length, PAGE_USERMODE | PAGE_RW);
|
||||
if(status != ENONE)
|
||||
{
|
||||
kernel_panic("Unexpected failure to map virtual memory.");
|
||||
}
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
int syscall_unmap_anon(syscall_arg_t arg_location)
|
||||
{
|
||||
void *addr = arg_location.ptr;
|
||||
if((size_t)addr % page_size != 0)
|
||||
{
|
||||
return EINVALIDARG;
|
||||
}
|
||||
else if(addr == NULL)
|
||||
{
|
||||
return ENULLPTR;
|
||||
}
|
||||
else if(addr >= _kernel_start)
|
||||
{
|
||||
return EPERM;
|
||||
}
|
||||
unmap_region(arg_location.ptr, free_pages(physical_address(arg_location.ptr)));
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
int syscall_map_physical(syscall_arg_t arg_addr, syscall_arg_t arg_phys_addr, syscall_arg_t arg_length)
|
||||
{
|
||||
void *addr = arg_addr.ptr;
|
||||
physaddr_t frame = arg_phys_addr.unsigned_int;
|
||||
unsigned long length = arg_length.unsigned_int;
|
||||
int status = ENONE;
|
||||
while(n < length && status == ENONE)
|
||||
for(unsigned long offset = 0; offset < length; offset += page_size)
|
||||
{
|
||||
physaddr_t frame = reserve_page();
|
||||
status = frame % page_size;
|
||||
if(status == ENONE)
|
||||
{
|
||||
status = map_page((void*)(location + n), frame, PAGE_USERMODE | PAGE_RW);
|
||||
if(status != ENONE && free_page(frame) != ENONE)
|
||||
{
|
||||
panic("critical error reached during mmap.");
|
||||
}
|
||||
n += page_size;
|
||||
}
|
||||
else
|
||||
status = map_page(addr + offset, frame + offset, PAGE_USERMODE | PAGE_RW);
|
||||
if(status)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(status != ENONE && munmap(arg_location, arg_length) != ENONE)
|
||||
{
|
||||
panic("critical error reached during mmap.");
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
size_t munmap(syscall_arg_t arg_location, syscall_arg_t arg_length)
|
||||
int syscall_unmap_physical(syscall_arg_t arg_addr, syscall_arg_t arg_length)
|
||||
{
|
||||
unsigned long location = arg_location.unsigned_int;
|
||||
void *addr = arg_addr.ptr;
|
||||
unsigned long length = arg_length.unsigned_int;
|
||||
if(location % page_size != 0 || length % page_size != 0)
|
||||
{
|
||||
return EINVALIDARG;
|
||||
}
|
||||
else if(location == 0)
|
||||
{
|
||||
return ENULLPTR;
|
||||
}
|
||||
size_t n = 0;
|
||||
int status = ENONE;
|
||||
while(n < length && status == ENONE)
|
||||
for(unsigned long offset = 0; offset < length; offset += page_size)
|
||||
{
|
||||
int type = page_type((void*)(location + n));
|
||||
physaddr_t frame;
|
||||
if(type & PAGE_PRESENT)
|
||||
status = unmap_page(addr + offset);
|
||||
if(status != ENONE)
|
||||
{
|
||||
frame = unmap_page((void*)(location + n));
|
||||
if(type & PAGE_ANON)
|
||||
{
|
||||
status = free_page(frame);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
size_t terminate_self()
|
||||
{
|
||||
return kernel_terminate_process(kernel_current_pid());
|
||||
}
|
||||
|
||||
size_t send(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags)
|
||||
{
|
||||
unsigned long op_type = flags.unsigned_int & IO_OP;
|
||||
unsigned long dest_type = flags.unsigned_int & IO_RECIPIENT_TYPE;
|
||||
if((flags.unsigned_int & ~(IO_OP | IO_RECIPIENT_TYPE)) != 0 || dest_type >= IO_MAILBOX)
|
||||
{
|
||||
printf("Invalid flags on send()\n");
|
||||
return EINVALIDARG;
|
||||
}
|
||||
if(dest_type == IO_PORT)
|
||||
{
|
||||
recipient.unsigned_int = kernel_get_port_owner(recipient.unsigned_int);
|
||||
if(recipient.unsigned_int == 0)
|
||||
{
|
||||
return EDOESNTEXIST;
|
||||
}
|
||||
}
|
||||
error_t status = kernel_send_message(recipient.unsigned_int, message.ptr);
|
||||
if(status == EBUSY/* && op_type == IO_ASYNC*/)
|
||||
{
|
||||
return kernel_queue_message(recipient.unsigned_int, message.ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
size_t receive(syscall_arg_t buffer, syscall_arg_t flags)
|
||||
{
|
||||
return receive_message(buffer.ptr, flags.unsigned_int);
|
||||
}
|
||||
|
||||
size_t open_port(syscall_arg_t id)
|
||||
int syscall_open_port(syscall_arg_t id)
|
||||
{
|
||||
return kernel_create_port(id.unsigned_int);
|
||||
}
|
||||
|
||||
size_t close_port(syscall_arg_t id)
|
||||
int syscall_close_port(syscall_arg_t id)
|
||||
{
|
||||
return kernel_remove_port(id.unsigned_int);
|
||||
}
|
||||
|
||||
int syscall_send_pid(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags)
|
||||
{
|
||||
if(message.ptr == NULL)
|
||||
{
|
||||
return ENULLPTR;
|
||||
}
|
||||
|
||||
process_t *process = kernel_get_process(recipient.unsigned_int);
|
||||
if(process == NULL)
|
||||
{
|
||||
return EEXITED;
|
||||
}
|
||||
|
||||
message_t *buffer = kmalloc(sizeof(message_t));
|
||||
if(buffer == NULL)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
memcpy(buffer, message.ptr, sizeof(message_t));
|
||||
buffer->sender = kernel_current_pid();
|
||||
|
||||
address_space_switch(process->address_space);
|
||||
int status = process_queue_message(process, buffer);
|
||||
address_space_switch(kernel_get_active_process()->address_space);
|
||||
if(status == ENOMEM)
|
||||
{
|
||||
kfree(buffer);
|
||||
kernel_terminate_process(recipient.unsigned_int);
|
||||
return EEXITED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ENONE;
|
||||
}
|
||||
}
|
||||
|
||||
int syscall_send_port(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags)
|
||||
{
|
||||
if(message.ptr == NULL)
|
||||
{
|
||||
return ENULLPTR;
|
||||
}
|
||||
|
||||
pid_t recipient_pid = kernel_get_port_owner(recipient.unsigned_int);
|
||||
if(recipient_pid == 0)
|
||||
{
|
||||
return EDOESNTEXIST;
|
||||
}
|
||||
|
||||
process_t *process = kernel_get_process(recipient_pid);
|
||||
if(process == NULL)
|
||||
{
|
||||
return EEXITED;
|
||||
}
|
||||
|
||||
message_t *buffer = kmalloc(sizeof(message_t));
|
||||
if(buffer == NULL)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
memcpy(buffer, message.ptr, sizeof(message_t));
|
||||
buffer->sender = kernel_current_pid();
|
||||
|
||||
address_space_switch(process->address_space);
|
||||
int status = process_queue_message(process, buffer);
|
||||
address_space_switch(kernel_get_active_process()->address_space);
|
||||
if(status == ENOMEM)
|
||||
{
|
||||
kfree(buffer);
|
||||
kernel_terminate_process(recipient_pid);
|
||||
return EEXITED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ENONE;
|
||||
}
|
||||
}
|
||||
|
||||
int syscall_receive(syscall_arg_t buffer, syscall_arg_t flags)
|
||||
{
|
||||
return kernel_receive_message(buffer.ptr, flags.unsigned_int);
|
||||
}
|
||||
|
||||
int syscall_create_object(void *location, size_t size, int flags)
|
||||
{
|
||||
shared_object_t *object = create_shared_object(size, flags);
|
||||
if(object == NULL)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
if(map_region(location, object->phys_addr, object->size, object->access_flags))
|
||||
{
|
||||
destroy_shared_object(object);
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
object->refcount++;
|
||||
process_store_object(kernel_get_active_process(), 0, location);
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
int syscall_aquire_object(oid_t id, void *location)
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
int syscall_release_object(oid_t id)
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
int syscall_get_pid()
|
||||
{
|
||||
return kernel_current_pid();
|
||||
}
|
||||
|
||||
int syscall_clone(void (*entry)(void*), void *arg, void *stack, int flags)
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
int syscall_signal_action(int id, struct signal_action_t *action, int flags)
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
int syscall_signal_return()
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
int syscall_signal_raise(pid_t pid, int sigid)
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
int syscall_intr_action(int id, struct signal_action_t *action, int flags)
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
int syscall_intr_return()
|
||||
{
|
||||
return ENOSYSCALL;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,12 @@
|
||||
#include "system.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
void context_construct(struct process_context_t *context)
|
||||
{
|
||||
memset(context, 0, sizeof(struct process_context_t));
|
||||
context->flags = DEFAULT_FLAGS;
|
||||
}
|
||||
|
||||
void *get_context_pc(struct process_context_t *context)
|
||||
{
|
||||
return (void*) context->eip;
|
||||
|
||||
19
src/x86/do_syscall.S
Normal file
19
src/x86/do_syscall.S
Normal file
@@ -0,0 +1,19 @@
|
||||
.global _do_syscall
|
||||
.type _do_syscall, @function
|
||||
_do_syscall:
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
push %esi
|
||||
push %edi
|
||||
push %ebx
|
||||
mov 0x08(%ebp), %eax
|
||||
mov 0x0C(%ebp), %ebx
|
||||
mov 0x10(%ebp), %ecx
|
||||
mov 0x14(%ebp), %edx
|
||||
int $0x80
|
||||
pop %ebx
|
||||
pop %edi
|
||||
pop %esi
|
||||
pop %ebp
|
||||
ret
|
||||
.size _do_syscall, . - _do_syscall
|
||||
@@ -2,6 +2,9 @@
|
||||
#include "stdio.h"
|
||||
#include "x86/apic.h"
|
||||
#include "platform/interrupts.h"
|
||||
#include "mmgr.h"
|
||||
#include "string.h"
|
||||
#include "kernel.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -14,6 +17,18 @@ struct interrupt_frame_t
|
||||
uint32_t ss;
|
||||
};
|
||||
|
||||
typedef struct page_fault_code_t
|
||||
{
|
||||
uint32_t present : 1;
|
||||
uint32_t write : 1;
|
||||
uint32_t usermode : 1;
|
||||
uint32_t reserved : 1;
|
||||
uint32_t inst_fetch : 1;
|
||||
uint32_t pk : 1;
|
||||
uint32_t shadow_stack : 1;
|
||||
uint32_t padding : 25;
|
||||
} page_fault_code_t;
|
||||
|
||||
void isr_generic(struct interrupt_frame_t *frame)
|
||||
{
|
||||
printf("Generic interrupt.\n");
|
||||
@@ -46,6 +61,7 @@ void isr_gp_fault(struct interrupt_frame_t *frame, unsigned int error)
|
||||
|
||||
void isr_page_fault(struct interrupt_frame_t *frame, unsigned int error)
|
||||
{
|
||||
page_fault_code_t *code = &error;
|
||||
uint32_t addr;
|
||||
asm("mov %%cr2, %0"
|
||||
: "=r"(addr));
|
||||
@@ -55,8 +71,28 @@ void isr_page_fault(struct interrupt_frame_t *frame, unsigned int error)
|
||||
"mov %%ax, %%fs; "
|
||||
"mov %%ax, %%gs; " ::
|
||||
: "ax");
|
||||
if(code->usermode == 0
|
||||
&& code->present == 0
|
||||
&& addr >= 0xFFC00000
|
||||
&& addr < 0xFFFFF000)
|
||||
{
|
||||
printf("Allocating new page table %08x within fault handler.\n", addr);
|
||||
physaddr_t new_table = reserve_page();
|
||||
if(new_table == ENOMEM)
|
||||
{
|
||||
kernel_panic("Out of memory while allocating page table.\n");
|
||||
}
|
||||
set_pte((void*)addr, 1, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW, new_table);
|
||||
asm volatile("mov %%cr3, %%eax;"
|
||||
"mov %%eax, %%cr3" ::
|
||||
: "eax", "memory");
|
||||
memset((void*)(addr & ~0xFFF), 0, page_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Exception: Page fault, code %08x, linear address %08x\n", error, addr);
|
||||
asm("hlt");
|
||||
kernel_panic("Unhandled page fault.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void isr_double_fault(struct interrupt_frame_t *frame, unsigned int error)
|
||||
|
||||
@@ -2,7 +2,7 @@ ENTRY(_start)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 0xFF400000;
|
||||
. = 0xFF000000;
|
||||
VIRTUAL_BASE = .;
|
||||
PHYSICAL_BASE = 0x100000;
|
||||
BASE_DIFF = VIRTUAL_BASE - PHYSICAL_BASE;
|
||||
|
||||
@@ -32,7 +32,7 @@ struct page_table_entry_t *page_tables = (struct page_table_entry_t *)0xFFC00000
|
||||
|
||||
struct page_table_entry_t *page_directory = (struct page_table_entry_t *)0xFFFFF000;
|
||||
|
||||
struct page_table_entry_t *get_pte_pointer(void *page, int level)
|
||||
struct page_table_entry_t *get_pte_pointer_chk(void *page, int level)
|
||||
{
|
||||
unsigned int directory_index = (unsigned int)page >> 22;
|
||||
struct page_table_entry_t *entry = NULL;
|
||||
@@ -48,6 +48,22 @@ struct page_table_entry_t *get_pte_pointer(void *page, int level)
|
||||
return entry;
|
||||
}
|
||||
|
||||
struct page_table_entry_t *get_pte_pointer(void *page, int level)
|
||||
{
|
||||
if(level == 0)
|
||||
{
|
||||
return &page_directory[(unsigned int)page >> 22];
|
||||
}
|
||||
else if(level == 1)
|
||||
{
|
||||
return &page_tables[(unsigned int)page >> page_bits];
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int start_paging(void *linear_addr, physaddr_t start, physaddr_t end, uint32_t *directory, uint32_t *table, uint32_t *identity_table)
|
||||
{
|
||||
unsigned int directory_index = (unsigned int) linear_addr >> 22;
|
||||
@@ -114,7 +130,7 @@ void paging_load_address_space(physaddr_t table)
|
||||
|
||||
int get_pte_type(void *page, int level)
|
||||
{
|
||||
struct page_table_entry_t *entry = get_pte_pointer(page, level);
|
||||
struct page_table_entry_t *entry = get_pte_pointer_chk(page, level);
|
||||
if(entry != NULL)
|
||||
{
|
||||
int flags = (entry->present ? PAGE_PRESENT | PAGE_EXECUTABLE : 0)
|
||||
@@ -146,7 +162,7 @@ int set_pte_type(void *page, int level, int flags)
|
||||
|
||||
physaddr_t get_pte_address(void *page, int level)
|
||||
{
|
||||
struct page_table_entry_t *entry = get_pte_pointer(page, level);
|
||||
struct page_table_entry_t *entry = get_pte_pointer_chk(page, level);
|
||||
if(entry != NULL)
|
||||
{
|
||||
return entry->physical_address << page_bits | ((size_t)page & 0xFFF);
|
||||
|
||||
@@ -20,7 +20,7 @@ isr_syscall:
|
||||
mov %ax, %es
|
||||
mov %ax, %fs
|
||||
mov %ax, %gs
|
||||
call do_syscall
|
||||
call kernel_do_syscall
|
||||
mov %ebp, %esp
|
||||
mov $0x23, %cx
|
||||
mov %cx, %ds
|
||||
|
||||
Reference in New Issue
Block a user