Compare commits

..

134 Commits

Author SHA1 Message Date
wash 1db6de6bc2 meta: rename kernel to Magenta 2026-04-30 20:30:36 +01:00
wash 560da0daa0 syscall: fix TLB not being flushed when necessary 2026-04-30 19:15:30 +01:00
wash 61f0aa1aba vm: address-space: update pmap API usage 2026-04-30 19:14:34 +01:00
wash ed54dca3ba syscall: handle: fix lock-related bugs 2026-04-30 19:12:47 +01:00
wash 4a6809e2df sched: task: fix leftover invalid pmap pointer 2026-04-30 19:10:51 +01:00
wash d63c2dbd12 vm: object: add prefetch function to fetch missing pages 2026-04-30 19:09:41 +01:00
wash 607efa961f sched: task: implement task_config_get and task_config_set 2026-04-30 19:08:02 +01:00
wash 02a44f67b9 sched: task: implement task creation flags 2026-04-30 19:06:46 +01:00
wash 562b856488 libmango: add a task config key to get the task ID 2026-04-30 19:05:14 +01:00
wash 8f77eb1ed0 libmango: add syscall to reset a particular task 2026-04-30 19:04:54 +01:00
wash 3ad479aa17 libmango: add flags parameter to task_create 2026-04-30 19:04:28 +01:00
wash 546fee7890 x86_64: adjust formatting 2026-04-30 19:02:56 +01:00
wash 5654c02f36 x86_64: pmap: implement pmap_flush and pmap_remove_range 2026-04-30 18:59:46 +01:00
wash d5c7a9f030 pmap: add functions for flushing all/parts of the TLB 2026-04-30 18:59:08 +01:00
wash 278fe39c0d vm: implement private and shared address space mappings
whether a mapping is private or shared determines how the mapping is handled
when a task is duplicated.
2026-04-21 21:12:00 +01:00
wash 8b7382fa13 libmango: add flags parameter to address_space_map 2026-04-21 21:11:42 +01:00
wash bc575aa1a1 x86_64: thread: remove log when cloning user thread 2026-04-20 22:20:43 +01:00
wash 982e518cf7 kernel: formatting tweaks 2026-04-19 20:17:31 +01:00
wash a30401d8b1 syscall: task: fix task_duplicate not taking a reference to the new task's address space 2026-04-19 20:17:05 +01:00
wash 4a9e907a75 vm: implement lazy-attach cow-duplication of vm-objects attached to a controller 2026-04-19 20:16:19 +01:00
wash b3be4c541b libmango: rename page requests to vm requests 2026-04-19 20:13:07 +01:00
wash 61a8e6fc40 libmango: add syscalls to handle cow-attached vm-objects 2026-04-19 20:12:11 +01:00
wash c105e17be9 kernel: printk: keep log_buffer_lock locked while flushing printk buffer 2026-04-19 20:08:29 +01:00
wash f1dd9d8564 kernel: handle: init handle table duplication 2026-04-19 20:07:51 +01:00
wash c87c29366d x86_64: irq: initialise tr_irqctx before running syscall handler 2026-04-19 20:05:11 +01:00
wash 9a9b0f63ba sched: task: optional handle_table pointer can now be passed to task_create 2026-04-19 20:04:18 +01:00
wash b52890d842 kernel: object: add missing parentheses to OBJECT_CAST 2026-04-19 20:00:16 +01:00
wash a2f370f326 cmake: change minimum cmake version to 3.31 2026-04-19 19:58:30 +01:00
wash 3584f6831b x86_64: thread: copy fs- and gs-base pointers to cloned thread context 2026-04-19 19:36:16 +01:00
wash c7c497cd66 x86_64: serial: write COM1 output to Bochs console 2026-04-19 19:34:49 +01:00
wash 05b1d39241 x86_64: pmap: fix pmap_get setting vm flags in wrong output variable 2026-04-19 19:34:19 +01:00
wash 5e66083355 x86_64: formatting tweaks 2026-04-19 19:33:10 +01:00
wash b92542c688 syscall: handle: add stub implementation of kern_handle_control 2026-04-01 18:41:33 +01:00
wash 3c494f0c4d libmango: add kern_handle_control syscall 2026-04-01 18:40:58 +01:00
wash b8d3125233 syscall: task: initial implementation of task_duplicate 2026-04-01 18:40:28 +01:00
wash 304ba8b254 libmango: add task_duplicate syscall 2026-04-01 18:39:38 +01:00
wash 06fe1e3704 vm: address-space: implement resolving accesses to copy-on-write pages 2026-04-01 18:38:17 +01:00
wash 6365154b75 syscall: add missing call to put_current_task 2026-04-01 18:37:10 +01:00
wash a0cab068da vm: address-space: implement copy-on-write address-space duplication 2026-04-01 18:36:25 +01:00
wash c8202c6741 vm: controller: implement detach of vm-object whose attachment was deferred 2026-04-01 18:35:08 +01:00
wash 2cbfa7d7d2 vm: object: implement creating copy-on-write duplicates of vm-objects 2026-04-01 18:33:24 +01:00
wash 4143e12a29 vm: add a copy-on-write ref count to vm_page 2026-04-01 18:32:32 +01:00
wash f28fab7afa vm: object: implement vm_object_put_page to add existing pages to a vmo 2026-04-01 18:31:05 +01:00
wash 6c5bf2344f sched: thread: implement cloning of userspace thread contexts 2026-04-01 18:29:54 +01:00
wash 61050fd54b kernel: atomic: change atomic_t to 32-bit int 2026-04-01 18:27:31 +01:00
wash 8595f705af vm: address-space: fix vm-object and address space not being unlocked when a demand-map fails 2026-04-01 18:26:27 +01:00
wash dd34b1d80f pmap: add pmap_get to retrieve existing page table entries 2026-04-01 18:24:13 +01:00
wash 876f91d8be sched: thread: add cpu context pointer usable during interrupts and syscalls 2026-04-01 18:22:23 +01:00
wash 7bcd1577be pmap: fix PFN() not clearing upper PTE control bits 2026-04-01 18:20:50 +01:00
wash db1a200eea vm: object: fix vm_object_cleanup referencing a vmo controller after the pointer is erased 2026-04-01 18:19:23 +01:00
wash f45b759a4c vm: object: fix vm_object_get_page ignoring VMO_REQUEST_MISSING_PAGE 2026-04-01 18:18:25 +01:00
wash 512356ac2d sched: enforce ref-counting on current task/thread pointers 2026-04-01 18:17:05 +01:00
wash 15c2207ab9 x86_64: pmap: enable kernel-mode write-protection 2026-04-01 18:06:50 +01:00
wash 8b41f5e681 kernel: remove unused functionality 2026-03-29 14:54:07 +01:00
wash 325699d64a kernel: fix sys_msg_send returning without unlocking self 2026-03-29 11:51:26 +01:00
wash 286016040c kernel: only show task name/id in log output if TRACE is enabled 2026-03-29 11:51:05 +01:00
wash 04617e81e3 kernel: add a syscall to query generic information about an object 2026-03-29 11:50:37 +01:00
wash 62770f4ab2 kernel: replace kern_handle_duplicate with the more powerful kern_handle_transfer
this syscall can move and copy handles within the current task, or from/to
other tasks
2026-03-29 11:48:59 +01:00
wash 9001f8e064 kernel: handle: rename handle transfer mode constants 2026-03-29 11:48:31 +01:00
wash 537242e606 kernel: handle: add support for allocating a specific handle value 2026-03-29 11:47:22 +01:00
wash dfffb45e66 libc: adjust formatting 2026-03-29 11:43:39 +01:00
wash f5a83af0d7 kernel: remove SEND_BLOCKED and REPLY_BLOCKED statuses from ports
this allows a port to be used by multiple threads at the same time
2026-03-29 11:42:23 +01:00
wash 7d25f1c31a x86_64: suppress serial port input logging 2026-03-29 11:32:03 +01:00
wash 95d33ddcb9 kernel: msg: async messages no longer hold a pointer to the thread/port that sent them
this prevents a race condition where an event is sent as a port is being destroyed.
when the server gets around to handling the event, it now refers to a different port
that was created in the mean-time.
2026-03-25 20:19:19 +00:00
wash a0a6a061a4 vm: controller: add an auto-detach flag for vm-objects 2026-03-24 20:24:12 +00:00
wash 4daffa804c vm: address-space: fix incorrect op calculation in unmap and release 2026-03-24 20:23:46 +00:00
wash 4be642f2e5 vm: controller: implement asynchronous DETACH page requests 2026-03-24 20:21:35 +00:00
wash 7dc0c742fa kernel: rebuild object ref-counting using atomic types 2026-03-24 19:10:36 +00:00
wash 9faa11cddc kernel: add atomic operations 2026-03-24 19:09:36 +00:00
wash 89d02c72ee kernel: msg: implement asynchronous event messages 2026-03-24 18:32:33 +00:00
wash 110f625f04 syscall: task: implement thread_self 2026-03-22 19:02:31 +00:00
wash 86f6c81781 vm: address-space: fix infinite loop in validate_access 2026-03-22 19:02:20 +00:00
wash 6350032e88 cmake: update for compatibility with CMake 4.0 2026-03-22 19:01:36 +00:00
wash a2c89df195 kernel: convert some verbose log messages to trace messages 2026-03-21 10:27:23 +00:00
wash 35949fa8db vm: object: ensure page request offets are page-aligned 2026-03-21 10:27:03 +00:00
wash d6c84565af x86_64: cmake: ensure the kernel is built as a static binary 2026-03-21 10:26:12 +00:00
wash 4551e7b2e6 sched: implement various ways to end tasks and threads 2026-03-18 21:07:43 +00:00
wash e03b2e07d0 vm: address-space: implement address space cleanup 2026-03-18 21:07:27 +00:00
wash 24f9ef85bf sched: implement user-configurable fs and gs segment base addresses 2026-03-18 21:07:05 +00:00
wash 63703a3d34 sched: don't reschedule a thread if its status is THREAD_STOPPED 2026-03-18 21:02:40 +00:00
wash d801203f04 syscall: vm-object: fix dangling reference to newly-created object 2026-03-18 21:02:19 +00:00
wash 2a1a0cf14d kernel: finish implementation of private and shared futexes 2026-03-18 21:02:09 +00:00
wash b774415f64 sched: wait: implement wakeup_n, waitqueue_empty 2026-03-18 20:56:15 +00:00
wash 04d05adbe8 kernel: handle: implement handle_table_destroy() 2026-03-18 20:55:35 +00:00
wash c0e212ac98 x86_64: panic: fix incorrect kernel stack traversal 2026-03-18 20:54:49 +00:00
wash 88405233a8 vm: object: implement object cleanup 2026-03-18 20:53:56 +00:00
wash 42a293e753 x86_64: pmap: implement pmap_destroy() 2026-03-18 20:53:24 +00:00
wash 1eef23ea98 thread: store struct msg on the stack instead of in the thread 2026-03-18 20:52:47 +00:00
wash 30c9c9db45 kernel: add futex definitions 2026-03-15 22:22:58 +00:00
wash c1e0b38952 vm: object: add missing include 2026-03-15 22:22:43 +00:00
wash 8a38d940cc vm: address-space: add function to translate virtual addresses to physical 2026-03-15 22:22:25 +00:00
wash a2e918c428 vm: evict PTE entries for transferred vm-object pages 2026-03-15 14:40:24 +00:00
wash 399742cabf x86_64: pmap: implement pmap_remove 2026-03-15 14:38:32 +00:00
wash cef4af53c9 x86_64: add pre-processor token to control hardware rng 2026-03-15 14:38:11 +00:00
wash 0af35c70ef vm: implement demand-paging via userspace services with vm-controller 2026-03-14 22:39:14 +00:00
wash f04c524bb5 vm: object: implement transferring pages between objects 2026-03-14 22:38:14 +00:00
wash 5d04dbb15a kerne: object: add lock_pair() functions to object lock template macro 2026-03-14 22:32:59 +00:00
wash a146f4a750 syscall: fix some missed-signal bugs in kern_object_wait 2026-03-14 22:32:26 +00:00
wash 2d267d2b51 kernel: add a syscall to duplicate a handle 2026-03-14 22:31:37 +00:00
wash b7f3bd77a7 libmango: update syscall definitions 2026-03-14 22:29:29 +00:00
wash a50826eb15 x86_64: implement stack traces for user-mode stacks 2026-03-14 22:28:24 +00:00
wash 62bdb51618 kernel: add functions to lock/unlock a pair of locks without saving irq flags 2026-03-14 22:25:15 +00:00
wash 115a2e7415 x86_64: enable interrupts during pmap_handle_fault
interrupts will need to be enable to allow for requesting missing pages from userspace
services.
2026-03-14 22:23:43 +00:00
wash e73a5c41ce sched: fix thread_awaken manipulating a runqueue without locking it 2026-03-14 22:23:07 +00:00
wash 89dac0c951 sched: add a thread flag to indicate when a thread is scheduled on a runqueue
this prevents runqueue corruption that can occur if rq_enqueue is called on
a thread that's already on a runqueue.
2026-03-14 22:22:05 +00:00
wash 7c630ece54 sched: add wait begin/end functions that don't change thread state
these functions can be used when waiting on multiple queues at once, to prevent
the thread state from being changed unexpectedly while initialising a set of wait items.
2026-03-14 22:20:10 +00:00
wash 72145257de x86_64: generate a seed for the RNG with RDRAND when available 2026-03-14 22:18:47 +00:00
wash d2203d9a65 kernel: replace random number generator with mersenne twister 2026-03-14 22:16:56 +00:00
wash 5e7a467dff kernel: printk: fix log buffer overflow 2026-03-14 22:16:01 +00:00
wash c628390f4a vm: replace vm-region with address-space
address-space is a non-recursive data structure, which contains a flat list of vm_areas representing
mapped vm-objects.

userspace programs can no longer create sub-address-spaces. instead, they can reserve portions of
the address space, and use that reserved space to create mappings.
2026-03-13 19:44:50 +00:00
wash c6b0bee827 kernel: wire dispatcher for kern_object_wait 2026-03-12 20:43:21 +00:00
wash f67d3a0cb9 libmango: update syscall definitions 2026-03-12 20:42:50 +00:00
wash 6ba236b2fe kernel: resolving a handle now increments the refcount of the corresponding object 2026-03-12 20:42:05 +00:00
wash 5a37b5e148 kernel: handle: handle_table_transfer now ignores items with KERN_HANDLE_INVALID 2026-03-12 20:41:01 +00:00
wash 2fb8f556b4 kernel: implement a generic object signalling system 2026-03-12 20:40:23 +00:00
wash 921c91c02a vm: add vm-controller object 2026-03-12 20:39:28 +00:00
wash 3fd608b623 kernel: add equeue object
equeue is a way for the kernel to deliver events to userspace programs.
2026-03-12 20:37:51 +00:00
wash 7d4cede788 misc: adjust formatting 2026-03-12 20:34:31 +00:00
wash 3f21e888d6 sched: split sched.h into separate header files 2026-03-12 20:30:36 +00:00
wash de520cdd2d libmango: types: add macro to define a kern_msg_handle_t 2026-03-10 19:08:49 +00:00
wash e84ed6057d channel: fix incorrect offset used in channel_write_msg 2026-03-10 19:08:20 +00:00
wash 1d4cb882a8 libmango: types: add ssize_t definition 2026-03-06 20:12:32 +00:00
wash 18b281debf kernel: bsp: add support for static bootstrap executables 2026-03-06 20:12:12 +00:00
wash 09d292fd09 kernel: msg: include details about who sent a message 2026-03-05 21:04:02 +00:00
wash 36c5ac7837 kernel: re-implement sending handles via port messages 2026-03-01 19:10:01 +00:00
wash b1bdb89ca4 vm: region: add a function to write data from a kernel buffer to a vm-region 2026-03-01 19:09:30 +00:00
wash f8a7a4285f syscall: msg: validate iovec array itself as well as the buffers it points to 2026-02-26 20:55:17 +00:00
wash f9bf4c618a syscall: log: add task id to log output 2026-02-26 20:54:14 +00:00
wash e4de3af00d kernel: remove support for sending kernel handles via port/channel 2026-02-26 20:53:47 +00:00
wash b59d0d8948 syscall: msg: locking of vm-region is now handled by channel_read_msg 2026-02-26 19:43:07 +00:00
wash 8cc877c251 kernel: port: dequeue kmsg struct once reply is received 2026-02-26 19:42:29 +00:00
wash 2073cad97b kernel: fix channel locking and status update issues 2026-02-26 19:42:12 +00:00
wash eb8758bc5e vm: region: fix some cases where regions weren't being unlocked after use. 2026-02-26 19:41:40 +00:00
wash 1cdde0d32e kernel: add functions for safely (un)locking pairs of objects
when locking a pair of objects, the object with the lesser memory address
is always locked first. the pair is unlocked in the opposite order.
2026-02-26 19:38:49 +00:00
136 changed files with 8400 additions and 4436 deletions
+5 -7
View File
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.13) cmake_minimum_required(VERSION 3.31)
project(mango C ASM) project(magenta C ASM)
if (NOT BUILD_TOOLS_DIR) if (NOT BUILD_TOOLS_DIR)
message(FATAL_ERROR "No build tools directory specified. Please run build.sh") message(FATAL_ERROR "No build tools directory specified. Please run build.sh")
@@ -9,8 +9,8 @@ set(CMAKE_C_STANDARD 17)
set(kernel_arch x86_64) set(kernel_arch x86_64)
set(kernel_name "Mango") set(kernel_name "Magenta")
set(kernel_exe_name "mango_kernel") set(kernel_exe_name "magenta_kernel")
set(generic_src_dirs ds init kernel libc sched util vm syscall) set(generic_src_dirs ds init kernel libc sched util vm syscall)
set(kernel_sources "") set(kernel_sources "")
@@ -28,8 +28,6 @@ file(GLOB_RECURSE arch_sources_c arch/${kernel_arch}/*.c)
file(GLOB_RECURSE arch_sources_asm arch/${kernel_arch}/*.S) file(GLOB_RECURSE arch_sources_asm arch/${kernel_arch}/*.S)
file(GLOB_RECURSE arch_headers arch/${kernel_arch}/*.h) file(GLOB_RECURSE arch_headers arch/${kernel_arch}/*.h)
set_property(SOURCE ${arch_sources_asm} PROPERTY LANGUAGE C)
add_executable(${kernel_exe_name} add_executable(${kernel_exe_name}
${kernel_sources} ${kernel_sources}
${kernel_headers} ${kernel_headers}
@@ -40,7 +38,7 @@ add_executable(${kernel_exe_name}
target_include_directories(${kernel_exe_name} PRIVATE target_include_directories(${kernel_exe_name} PRIVATE
include include
libc/include libc/include
libmango/include libmagenta/include
arch/${kernel_arch}/include) arch/${kernel_arch}/include)
target_compile_options(${kernel_exe_name} PRIVATE target_compile_options(${kernel_exe_name} PRIVATE
-nostdlib -ffreestanding -nostdlib -ffreestanding
+1 -1
View File
@@ -1,4 +1,4 @@
Mango Magenta
===== =====
It's a kernel! It's a kernel!
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef MANGO_USER_CPU_H_ #ifndef MAGENTA_USER_CPU_H_
#define MANGO_USER_CPU_H_ #define MAGENTA_USER_CPU_H_
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef MANGO_USER_HWLOCK_H_ #ifndef MAGENTA_USER_HWLOCK_H_
#define MANGO_USER_HWLOCK_H_ #define MAGENTA_USER_HWLOCK_H_
#define ML_HWLOCK_INIT (0) #define ML_HWLOCK_INIT (0)
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef MANGO_X86_64_INIT_H_ #ifndef MAGENTA_X86_64_INIT_H_
#define MANGO_X86_64_INIT_H_ #define MAGENTA_X86_64_INIT_H_
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef MANGO_X86_64_IRQ_H_ #ifndef MAGENTA_X86_64_IRQ_H_
#define MANGO_X86_64_IRQ_H_ #define MAGENTA_X86_64_IRQ_H_
#endif #endif
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef MANGO_USER_PMAP_H_ #ifndef MAGENTA_USER_PMAP_H_
#define MANGO_USER_PMAP_H_ #define MAGENTA_USER_PMAP_H_
#include <stdint.h> #include <stdint.h>
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef MANGO_USER_VM_H_ #ifndef MAGENTA_USER_VM_H_
#define MANGO_USER_VM_H_ #define MAGENTA_USER_VM_H_
#include <stdint.h> #include <stdint.h>
View File
+13 -7
View File
@@ -1,10 +1,11 @@
#include <kernel/sched.h>
#include <kernel/compiler.h> #include <kernel/compiler.h>
#include <kernel/sched.h>
#include <kernel/thread.h>
//size_t THREAD_sp = offsetof(struct thread, tr_sp); // size_t THREAD_sp = offsetof(struct thread, tr_sp);
/* Use %a0 instead of %0 to prevent gcc from emitting a $ before the symbol value /* Use %a0 instead of %0 to prevent gcc from emitting a $ before the symbol
in the generated assembly. value in the generated assembly.
emitting emitting
.set TASK_sp, $56 .set TASK_sp, $56
@@ -16,12 +17,17 @@
*/ */
#define DEFINE(sym, val) \ #define DEFINE(sym, val) \
asm volatile("\n.global " #sym "\n.type " #sym ", @object" "\n.set " #sym ", %a0" : : "i" (val)) asm volatile("\n.global " #sym "\n.type " #sym \
", @object" \
"\n.set " #sym ", %a0" \
: \
: "i"(val))
#define OFFSET(sym, str, mem) \ #define OFFSET(sym, str, mem) DEFINE(sym, offsetof(str, mem))
DEFINE(sym, offsetof(str, mem))
static void __used common(void) static void __used common(void)
{ {
OFFSET(THREAD_sp, struct thread, tr_sp); OFFSET(THREAD_sp, struct thread, tr_sp);
OFFSET(THREAD_fsbase, struct thread, tr_ml.tr_fsbase);
OFFSET(THREAD_gsbase, struct thread, tr_ml.tr_gsbase);
} }
+2 -1
View File
@@ -1,4 +1,5 @@
target_compile_options(${kernel_exe_name} PRIVATE target_compile_options(${kernel_exe_name} PRIVATE
-z max-page-size=0x1000 -m64 -mcmodel=large -mno-red-zone -mno-mmx -z max-page-size=0x1000 -m64 -mcmodel=large -mno-red-zone -mno-mmx
-mno-sse -mno-sse2 -D_64BIT -DBYTE_ORDER=1234) -mno-sse -mno-sse2 -D_64BIT -DBYTE_ORDER=1234)
target_link_libraries(${kernel_exe_name} "-z max-page-size=0x1000" "-T ${CMAKE_CURRENT_SOURCE_DIR}/arch/x86_64/layout.ld") target_link_libraries(${kernel_exe_name} "-static -z max-page-size=0x1000" "-T ${CMAKE_CURRENT_SOURCE_DIR}/arch/x86_64/layout.ld")
+1
View File
@@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#define MSR_FS_BASE 0xC0000100
#define MSR_GS_BASE 0xC0000101 #define MSR_GS_BASE 0xC0000101
#define MSR_KERNEL_GS_BASE 0xC0000102 #define MSR_KERNEL_GS_BASE 0xC0000102
+2 -1
View File
@@ -1,8 +1,8 @@
#ifndef ARCH_PAGING_H_ #ifndef ARCH_PAGING_H_
#define ARCH_PAGING_H_ #define ARCH_PAGING_H_
#include <kernel/types.h>
#include <kernel/compiler.h> #include <kernel/compiler.h>
#include <kernel/types.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -55,6 +55,7 @@ enum page_size {
defined in pmap_ctrl.S */ defined in pmap_ctrl.S */
extern int gigabyte_pages(void); extern int gigabyte_pages(void);
extern int enable_nx(void); extern int enable_nx(void);
extern int enable_wp(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
@@ -0,0 +1,10 @@
#ifndef KERNEL_X86_64_RANDOM_H_
#define KERNEL_X86_64_RANDOM_H_
#include <stdbool.h>
#include <stdint.h>
extern bool ml_hwrng_available(void);
extern uint64_t ml_hwrng_generate(void);
#endif
@@ -3,6 +3,10 @@
#include <kernel/sched.h> #include <kernel/sched.h>
struct ml_thread {
virt_addr_t tr_gsbase, tr_fsbase;
};
struct ml_cpu_context; struct ml_cpu_context;
/* switch from one thread to another. the stack of the `to` thread must have /* switch from one thread to another. the stack of the `to` thread must have
@@ -27,5 +31,24 @@ extern kern_status_t ml_thread_prepare_user_context(
virt_addr_t *kernel_sp, virt_addr_t *kernel_sp,
const uintptr_t *args, const uintptr_t *args,
size_t nr_args); size_t nr_args);
/* prepare the stack so that ml_thread_switch_user can jump to usermode
* with the specified register context */
extern kern_status_t ml_thread_clone_user_context(
const struct ml_cpu_context *src_regs,
const struct ml_thread *src_ml,
struct ml_thread *dest_ml,
uintptr_t return_value,
virt_addr_t *kernel_sp);
extern kern_status_t ml_thread_config_get(
struct thread *thread,
kern_config_key_t key,
void *out,
size_t max);
extern kern_status_t ml_thread_config_set(
struct thread *thread,
kern_config_key_t key,
const void *ptr,
size_t len);
#endif #endif
+20
View File
@@ -10,14 +10,18 @@
#include <kernel/init.h> #include <kernel/init.h>
#include <kernel/libc/stdio.h> #include <kernel/libc/stdio.h>
#include <kernel/machine/cpu.h> #include <kernel/machine/cpu.h>
#include <kernel/machine/random.h>
#include <kernel/memblock.h> #include <kernel/memblock.h>
#include <kernel/object.h> #include <kernel/object.h>
#include <kernel/percpu.h> #include <kernel/percpu.h>
#include <kernel/pmap.h> #include <kernel/pmap.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/util.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#define HARDWARE_RNG
#define PTR32(x) ((void *)((uintptr_t)(x))) #define PTR32(x) ((void *)((uintptr_t)(x)))
/* the physical address of the start of the memblock heap. /* the physical address of the start of the memblock heap.
@@ -123,6 +127,22 @@ int ml_init(uintptr_t arg)
reserve_end = bsp.mod_base + bsp.mod_size; reserve_end = bsp.mod_base + bsp.mod_size;
} }
#if defined(HARDWARE_RNG)
if (ml_hwrng_available()) {
printk("cpu: ardware RNG available");
uint64_t seed = ml_hwrng_generate();
printk("cpu: RNG seed=%zx", seed);
init_random(seed);
} else {
#endif
printk("cpu: hardware RNG unavailable");
uint64_t seed = 0xeddc4c8a679dc23f;
printk("cpu: RNG seed=%zx", seed);
init_random(seed);
#if defined(HARDWARE_RNG)
}
#endif
early_vm_init(reserve_end); early_vm_init(reserve_end);
e820_scan(PTR32(mb->mmap_addr), mb->mmap_length); e820_scan(PTR32(mb->mmap_addr), mb->mmap_length);
+22
View File
@@ -8,6 +8,7 @@
#include <kernel/panic.h> #include <kernel/panic.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/syscall.h> #include <kernel/syscall.h>
#include <kernel/thread.h>
#include <stddef.h> #include <stddef.h>
#define MAX_ISR_HANDLERS 16 #define MAX_ISR_HANDLERS 16
@@ -97,7 +98,9 @@ static void pf_handler(struct ml_cpu_context *regs)
virt_addr_t fault_ptr = pf_faultptr(); virt_addr_t fault_ptr = pf_faultptr();
ml_int_enable();
kern_status_t status = pmap_handle_fault(fault_ptr, fault_flags); kern_status_t status = pmap_handle_fault(fault_ptr, fault_flags);
ml_int_disable();
if (status == KERN_OK) { if (status == KERN_OK) {
return; return;
@@ -164,6 +167,12 @@ int idt_load(struct idt_ptr *ptr)
void isr_dispatch(struct ml_cpu_context *regs) void isr_dispatch(struct ml_cpu_context *regs)
{ {
struct thread *thr = get_current_thread();
if (thr) {
thr->tr_irqctx = regs;
put_current_thread(thr);
}
int_hook h = isr_handlers[regs->int_no]; int_hook h = isr_handlers[regs->int_no];
if (h) { if (h) {
h(regs); h(regs);
@@ -186,6 +195,13 @@ void irq_dispatch(struct ml_cpu_context *regs)
end_charge_period(); end_charge_period();
irq_ack(regs->int_no); irq_ack(regs->int_no);
struct thread *thr = get_current_thread();
if (thr) {
thr->tr_irqctx = regs;
put_current_thread(thr);
}
struct queue *hooks = &irq_hooks[regs->int_no - IRQ0]; struct queue *hooks = &irq_hooks[regs->int_no - IRQ0];
queue_foreach(struct irq_hook, hook, hooks, irq_entry) queue_foreach(struct irq_hook, hook, hooks, irq_entry)
{ {
@@ -201,6 +217,12 @@ void irq_dispatch(struct ml_cpu_context *regs)
void syscall_dispatch(struct ml_cpu_context *regs) void syscall_dispatch(struct ml_cpu_context *regs)
{ {
struct thread *thr = get_current_thread();
if (thr) {
thr->tr_irqctx = regs;
put_current_thread(thr);
}
unsigned int sysid = regs->rax; unsigned int sysid = regs->rax;
virt_addr_t syscall_impl = syscall_get_function(sysid); virt_addr_t syscall_impl = syscall_get_function(sysid);
+46 -13
View File
@@ -1,8 +1,11 @@
#include <arch/irq.h> #include <arch/irq.h>
#include <kernel/address-space.h>
#include <kernel/libc/stdio.h> #include <kernel/libc/stdio.h>
#include <kernel/machine/cpu.h> #include <kernel/machine/cpu.h>
#include <kernel/machine/panic.h> #include <kernel/machine/panic.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h>
#include <kernel/task.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#define R_CF 0 #define R_CF 0
@@ -166,36 +169,66 @@ static void print_stack_item(uintptr_t addr)
printk("%s", buf); printk("%s", buf);
} }
static void print_stack_trace(uintptr_t ip, uintptr_t *bp) static bool read_stack_frame(
struct address_space *space,
uintptr_t bp,
struct stack_frame *out)
{ {
struct stack_frame *stk = (struct stack_frame *)bp; if (bp >= VM_PAGEMAP_BASE) {
*out = *(struct stack_frame *)bp;
return true;
}
if (!space) {
return false;
}
size_t tmp;
kern_status_t status
= address_space_read(space, bp, sizeof *out, out, &tmp);
return status == KERN_OK;
}
static void print_stack_trace(
struct address_space *space,
uintptr_t ip,
uintptr_t bp)
{
struct stack_frame stk;
if (!read_stack_frame(space, bp, &stk)) {
return;
}
printk("call trace:"); printk("call trace:");
print_stack_item(ip); print_stack_item(ip);
int max_frames = 10, current_frame = 0; int max_frames = 10, current_frame = 0;
while (1) { while (current_frame < max_frames) {
if (!vm_virt_to_phys(stk) || bp == NULL uintptr_t addr = stk.rip;
|| current_frame > max_frames) {
break;
}
uintptr_t addr = stk->rip;
print_stack_item(addr); print_stack_item(addr);
stk = (struct stack_frame *)stk->rbp; bp = stk.rbp;
if (!read_stack_frame(space, bp, &stk)) {
break;
}
current_frame++; current_frame++;
} }
} }
void ml_print_stack_trace(uintptr_t ip) void ml_print_stack_trace(uintptr_t ip)
{ {
uintptr_t *bp; struct task *task = get_current_task();
struct address_space *space = task ? task->t_address_space : NULL;
uintptr_t bp;
asm volatile("mov %%rbp, %0" : "=r"(bp)); asm volatile("mov %%rbp, %0" : "=r"(bp));
print_stack_trace(ip, bp); print_stack_trace(space, ip, bp);
put_current_task(task);
} }
void ml_print_stack_trace_irq(struct ml_cpu_context *ctx) void ml_print_stack_trace_irq(struct ml_cpu_context *ctx)
{ {
print_stack_trace(ctx->rip, (uintptr_t *)ctx->rbp); struct task *task = get_current_task();
struct address_space *space = task ? task->t_address_space : NULL;
print_stack_trace(space, ctx->rip, ctx->rbp);
put_current_task(task);
} }
+242 -13
View File
@@ -1,14 +1,15 @@
#include <kernel/address-space.h>
#include <kernel/compiler.h> #include <kernel/compiler.h>
#include <kernel/libc/stdio.h> #include <kernel/libc/stdio.h>
#include <kernel/memblock.h> #include <kernel/memblock.h>
#include <kernel/pmap.h> #include <kernel/pmap.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/task.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/vm-object.h> #include <kernel/vm-object.h>
#include <kernel/vm-region.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#include <mango/status.h> #include <magenta/status.h>
/* some helpful datasize constants */ /* some helpful datasize constants */
#define C_1GiB 0x40000000ULL #define C_1GiB 0x40000000ULL
@@ -18,7 +19,7 @@
#define PTR_TO_ENTRY(x) (((x) & ~VM_PAGE_MASK) | PTE_PRESENT | PTE_RW | PTE_USR) #define PTR_TO_ENTRY(x) (((x) & ~VM_PAGE_MASK) | PTE_PRESENT | PTE_RW | PTE_USR)
#define ENTRY_TO_PTR(x) ((x) & ~VM_PAGE_MASK) #define ENTRY_TO_PTR(x) ((x) & ~VM_PAGE_MASK)
#define PFN(x) ((x) >> VM_PAGE_SHIFT) #define PFN(x) (((x) >> VM_PAGE_SHIFT) & 0xFFFFFFFFFF)
static int can_use_gbpages = 0; static int can_use_gbpages = 0;
static pmap_t kernel_pmap; static pmap_t kernel_pmap;
@@ -99,7 +100,7 @@ static void delete_ptab(phys_addr_t pt)
return; return;
} }
pt &= ~VM_PAGE_MASK; pt = ENTRY_TO_PTR(pt);
if (!pt) { if (!pt) {
/* physical address of 0x0, nothing to delete */ /* physical address of 0x0, nothing to delete */
return; return;
@@ -116,7 +117,7 @@ static void delete_pdir(phys_addr_t pd)
return; return;
} }
pd &= ~0x1FFFFFULL; pd &= ENTRY_TO_PTR(pd);
if (!pd) { if (!pd) {
/* physical address of 0x0, nothing to delete */ /* physical address of 0x0, nothing to delete */
return; return;
@@ -135,6 +136,81 @@ static void delete_pdir(phys_addr_t pd)
kfree(pdir); kfree(pdir);
} }
kern_status_t pmap_get(
pmap_t pmap,
virt_addr_t pv,
pfn_t *out_pfn,
vm_prot_t *out_prot)
{
unsigned int pml4t_index = BAD_INDEX, pdpt_index = BAD_INDEX,
pd_index = BAD_INDEX, pt_index = BAD_INDEX;
pml4t_index = (pv >> 39) & 0x1FF;
pdpt_index = (pv >> 30) & 0x1FF;
pd_index = (pv >> 21) & 0x1FF;
pt_index = (pv >> 12) & 0x1FF;
/* 1. get PML4T (mandatory) */
struct pml4t *pml4t = vm_phys_to_virt(ENTRY_TO_PTR(pmap));
if (!pml4t) {
return KERN_INVALID_ARGUMENT;
}
/* 2. traverse PML4T, get PDPT (mandatory) */
struct pdpt *pdpt = NULL;
if (!pml4t->p_entries[pml4t_index]) {
return KERN_NO_ENTRY;
} else {
pdpt = vm_phys_to_virt(
ENTRY_TO_PTR(pml4t->p_entries[pml4t_index]));
}
/* 3. traverse PDPT, get PDIR (optional, 4K and 2M only) */
struct pdir *pdir = NULL;
if (!pdpt->p_entries[pdpt_index]
|| pdpt->p_pages[pdpt_index] & PTE_PAGESIZE) {
return KERN_NO_ENTRY;
} else {
pdir = vm_phys_to_virt(
ENTRY_TO_PTR(pdpt->p_entries[pdpt_index]));
}
/* 4. traverse PDIR, get PTAB (optional, 4K only) */
struct ptab *ptab = NULL;
if (!pdir->p_entries[pd_index]
|| pdir->p_pages[pd_index] & PTE_PAGESIZE) {
/* entry is null, or points to a hugepage */
return KERN_NO_ENTRY;
} else {
ptab = vm_phys_to_virt(ENTRY_TO_PTR(pdir->p_entries[pd_index]));
}
uint64_t pte = ptab->p_pages[pt_index];
if (out_pfn) {
*out_pfn = PFN(pte);
}
if (out_prot) {
if (pte & PTE_PRESENT) {
*out_prot |= VM_PROT_USER;
}
if (pte & PTE_RW) {
*out_prot |= (VM_PROT_READ | VM_PROT_WRITE);
}
if (pte & PTE_USR) {
*out_prot |= VM_PROT_USER;
}
if (!(pte & PTE_NX)) {
*out_prot |= VM_PROT_EXEC;
}
}
return KERN_OK;
}
static kern_status_t do_pmap_add( static kern_status_t do_pmap_add(
pmap_t pmap, pmap_t pmap,
virt_addr_t pv, virt_addr_t pv,
@@ -240,6 +316,108 @@ static kern_status_t do_pmap_add(
return KERN_OK; return KERN_OK;
} }
static kern_status_t do_pmap_remove(
pmap_t pmap,
virt_addr_t pv,
enum page_size size)
{
if (pmap == PMAP_INVALID) {
return KERN_OK;
}
unsigned int pml4t_index = BAD_INDEX, pdpt_index = BAD_INDEX,
pd_index = BAD_INDEX, pt_index = BAD_INDEX;
switch (size) {
case PS_4K:
pml4t_index = (pv >> 39) & 0x1FF;
pdpt_index = (pv >> 30) & 0x1FF;
pd_index = (pv >> 21) & 0x1FF;
pt_index = (pv >> 12) & 0x1FF;
break;
case PS_2M:
pml4t_index = (pv >> 39) & 0x1FF;
pdpt_index = (pv >> 30) & 0x1FF;
pd_index = (pv >> 21) & 0x1FF;
break;
case PS_1G:
if (!can_use_gbpages) {
return KERN_UNSUPPORTED;
}
pml4t_index = (pv >> 39) & 0x1FF;
pdpt_index = (pv >> 30) & 0x1FF;
break;
default:
return KERN_INVALID_ARGUMENT;
}
/* 1. get PML4T (mandatory) */
struct pml4t *pml4t = vm_phys_to_virt(ENTRY_TO_PTR(pmap));
if (!pml4t) {
return KERN_OK;
}
/* 2. traverse PML4T, get PDPT (mandatory) */
struct pdpt *pdpt = NULL;
if (!pml4t->p_entries[pml4t_index]) {
return KERN_OK;
} else {
pdpt = vm_phys_to_virt(
ENTRY_TO_PTR(pml4t->p_entries[pml4t_index]));
}
/* if we're mapping a 1GiB page, we stop here */
if (size == PS_1G) {
if (pdpt->p_entries[pdpt_index] != 0) {
/* this slot points to a pdir, delete it.
if this slot points to a hugepage, this does nothing
*/
delete_pdir(pdpt->p_entries[pdpt_index]);
}
pdpt->p_pages[pdpt_index] = 0;
return KERN_OK;
}
/* 3. traverse PDPT, get PDIR (optional, 4K and 2M only) */
struct pdir *pdir = NULL;
if (!pdpt->p_entries[pdpt_index]
|| pdpt->p_pages[pdpt_index] & PTE_PAGESIZE) {
/* entry is null, or points to a hugepage */
return KERN_OK;
} else {
pdir = vm_phys_to_virt(
ENTRY_TO_PTR(pdpt->p_entries[pdpt_index]));
}
/* if we're mapping a 2MiB page, we stop here */
if (size == PS_2M) {
if (pdir->p_entries[pd_index] != 0) {
/* this slot points to a ptab, delete it.
if this slot points to a hugepage, this does nothing
*/
delete_ptab(pdir->p_entries[pd_index]);
}
pdir->p_pages[pd_index] = 0;
return KERN_OK;
}
/* 4. traverse PDIR, get PTAB (optional, 4K only) */
struct ptab *ptab = NULL;
if (!pdir->p_entries[pd_index]
|| pdir->p_pages[pd_index] & PTE_PAGESIZE) {
/* entry is null, or points to a hugepage */
return KERN_OK;
} else {
ptab = vm_phys_to_virt(ENTRY_TO_PTR(pdir->p_entries[pd_index]));
}
ptab->p_pages[pt_index] = 0;
return KERN_OK;
}
pmap_t get_kernel_pmap(void) pmap_t get_kernel_pmap(void)
{ {
return kernel_pmap; return kernel_pmap;
@@ -252,6 +430,8 @@ void pmap_bootstrap(void)
can_use_gbpages == 1 ? "en" : "dis"); can_use_gbpages == 1 ? "en" : "dis");
enable_nx(); enable_nx();
printk("pmap: NX protection enabled"); printk("pmap: NX protection enabled");
enable_wp();
printk("pmap: kernel-mode write protection enabled");
enum page_size hugepage = PS_2M; enum page_size hugepage = PS_2M;
if (can_use_gbpages) { if (can_use_gbpages) {
@@ -313,8 +493,46 @@ pmap_t pmap_create(void)
return pmap; return pmap;
} }
static void delete_pdpt(phys_addr_t pd)
{
if (pd & PTE_PAGESIZE) {
/* this entry points to a hugepage, nothing to delete */
return;
}
pd &= ENTRY_TO_PTR(pd);
if (!pd) {
/* physical address of 0x0, nothing to delete */
return;
}
struct pdpt *pdpt = vm_phys_to_virt(ENTRY_TO_PTR(pd));
for (int i = 0; i < 512; i++) {
if (pdpt->p_pages[i] & PTE_PAGESIZE) {
/* this is a hugepage, there is nothing to delete */
continue;
}
if (!pdpt->p_entries[i]) {
continue;
}
delete_ptab(pdpt->p_entries[i]);
}
kfree(pdpt);
}
void pmap_destroy(pmap_t pmap) void pmap_destroy(pmap_t pmap)
{ {
struct pml4t *pml4t = vm_phys_to_virt(ENTRY_TO_PTR(pmap));
for (unsigned int i = 0; i < 256; i++) {
if (pml4t->p_entries[i]) {
delete_pdpt(pml4t->p_entries[i]);
}
}
kfree(pml4t);
} }
static void log_fault(virt_addr_t fault_addr, enum pmap_fault_flags flags) static void log_fault(virt_addr_t fault_addr, enum pmap_fault_flags flags)
@@ -357,18 +575,21 @@ kern_status_t pmap_handle_fault(
{ {
// log_fault(fault_addr, flags); // log_fault(fault_addr, flags);
if (flags & PMAP_FAULT_PRESENT) { struct task *task = get_current_task();
if (!task) {
return KERN_FATAL_ERROR; return KERN_FATAL_ERROR;
} }
struct task *task = current_task(); struct address_space *space = task->t_address_space;
struct vm_region *space = task->t_address_space; if (!space) {
return KERN_FATAL_ERROR;
}
unsigned long lock_flags; /* this must be called with `space` unlocked. */
vm_region_lock_irqsave(space, &lock_flags); kern_status_t status
kern_status_t status = vm_region_demand_map(space, fault_addr, flags); = address_space_demand_map(space, fault_addr, flags);
vm_region_unlock_irqrestore(space, lock_flags);
put_current_task(task);
return status; return status;
} }
@@ -400,10 +621,18 @@ kern_status_t pmap_add_block(
kern_status_t pmap_remove(pmap_t pmap, virt_addr_t p) kern_status_t pmap_remove(pmap_t pmap, virt_addr_t p)
{ {
return KERN_OK; return do_pmap_remove(pmap, p, PS_4K);
} }
kern_status_t pmap_remove_range(pmap_t pmap, virt_addr_t p, size_t len) kern_status_t pmap_remove_range(pmap_t pmap, virt_addr_t p, size_t len)
{ {
if (pmap == PMAP_INVALID) {
return KERN_OK;
}
for (size_t i = p; i < p + len; i += VM_PAGE_SIZE) {
pmap_remove(pmap, i);
}
return KERN_OK; return KERN_OK;
} }
+21
View File
@@ -1,3 +1,11 @@
.global pmap_flush
.type pmap_flush, @function
pmap_flush:
mov %cr3, %rax
mov %rax, %cr3
ret
.global pmap_switch .global pmap_switch
.type pmap_switch, @function .type pmap_switch, @function
@@ -6,6 +14,7 @@ pmap_switch:
mov %rdi, %cr3 mov %rdi, %cr3
ret ret
.global gigabyte_pages .global gigabyte_pages
.type gigabyte_pages, @function .type gigabyte_pages, @function
@@ -30,6 +39,7 @@ gigabyte_pages:
pop %rbp pop %rbp
ret ret
.global enable_nx .global enable_nx
.type enable_nx, @function .type enable_nx, @function
@@ -40,3 +50,14 @@ enable_nx:
wrmsr wrmsr
ret ret
.global enable_wp
.type enable_wp, @function
enable_wp:
mov %cr0, %rax
or $0x10000, %rax
mov %rax, %cr0
ret
+43
View File
@@ -0,0 +1,43 @@
.code64
.global ml_hwrng_available
.type ml_hwrng_available, @function
ml_hwrng_available:
push %rbp
mov %rsp, %rbp
push %rbx
push %rdx
mov $1, %eax
mov $0, %ecx
cpuid
shr $30, %ecx
and $1, %ecx
mov %ecx, %eax
pop %rdx
pop %rbx
pop %rbp
ret
.global ml_hwrng_generate
.type ml_hwrng_generate, @function
ml_hwrng_generate:
push %rbp
mov %rsp, %rbp
mov $100, %rcx
.retry:
rdrand %rax
jc .done
loop .retry
.fail:
mov $0, %rax
.done:
pop %rbp
ret
+6
View File
@@ -28,6 +28,10 @@ void serial_send_byte(int device, char out)
outportb(device, out); outportb(device, out);
if (device == COM1) {
outportb(0xe9, out);
}
while (!transmit_empty(device)) { while (!transmit_empty(device)) {
_count++; _count++;
} }
@@ -138,6 +142,7 @@ static struct console serialcon = {
static int serial_irq1(void) static int serial_irq1(void)
{ {
#if 0
if (serial_received(COM1)) { if (serial_received(COM1)) {
unsigned char c = serial_recv_byte(COM1); unsigned char c = serial_recv_byte(COM1);
printk("serial: COM1 received %c", c); printk("serial: COM1 received %c", c);
@@ -147,6 +152,7 @@ static int serial_irq1(void)
unsigned char c = serial_recv_byte(COM3); unsigned char c = serial_recv_byte(COM3);
printk("serial: COM3 received %c", c); printk("serial: COM3 received %c", c);
} }
#endif
return 0; return 0;
} }
+78
View File
@@ -1,5 +1,7 @@
#include <arch/msr.h>
#include <kernel/machine/cpu.h> #include <kernel/machine/cpu.h>
#include <kernel/machine/thread.h> #include <kernel/machine/thread.h>
#include <kernel/thread.h>
#define MAX_REG_ARGS 6 #define MAX_REG_ARGS 6
#define REG_ARG_0 rdi #define REG_ARG_0 rdi
@@ -77,3 +79,79 @@ extern kern_status_t ml_thread_prepare_user_context(
return KERN_OK; return KERN_OK;
} }
kern_status_t ml_thread_clone_user_context(
const struct ml_cpu_context *src_regs,
const struct ml_thread *src_ml,
struct ml_thread *dest_ml,
uintptr_t return_value,
virt_addr_t *kernel_sp)
{
(*kernel_sp) -= sizeof(struct ml_cpu_context);
struct ml_cpu_context *regs = (struct ml_cpu_context *)(*kernel_sp);
memcpy(regs, src_regs, sizeof *regs);
regs->rax = return_value;
dest_ml->tr_fsbase = src_ml->tr_fsbase;
dest_ml->tr_gsbase = src_ml->tr_gsbase;
return KERN_OK;
}
kern_status_t ml_thread_config_get(
struct thread *thread,
kern_config_key_t key,
void *out,
size_t max)
{
return KERN_OK;
}
kern_status_t ml_thread_config_set(
struct thread *thread,
kern_config_key_t key,
const void *ptr,
size_t len)
{
struct thread *self = get_current_thread();
kern_status_t status = KERN_OK;
switch (key) {
case THREAD_CFG_FSBASE:
if (len != sizeof(thread->tr_ml.tr_fsbase)) {
status = KERN_INVALID_ARGUMENT;
break;
}
thread->tr_ml.tr_fsbase = *(virt_addr_t *)ptr;
if (thread == self) {
wrmsr(MSR_FS_BASE, thread->tr_ml.tr_fsbase);
}
break;
case THREAD_CFG_GSBASE:
if (len != sizeof(thread->tr_ml.tr_gsbase)) {
status = KERN_INVALID_ARGUMENT;
break;
}
thread->tr_ml.tr_gsbase = *(virt_addr_t *)ptr;
if (thread == self) {
/* we're in the kernel right now, so the user and kernel
* gs-base registers are swapped. when we return to
* usermode, this value will be swapped back into
* the user gs-base register */
wrmsr(MSR_KERNEL_GS_BASE, thread->tr_ml.tr_gsbase);
}
break;
default:
status = KERN_INVALID_ARGUMENT;
break;
}
put_current_thread(self);
return status;
}
+16
View File
@@ -13,6 +13,22 @@ ml_thread_switch:
push %rax push %rax
push %rcx push %rcx
push %rdx push %rdx
// set fs-base
mov $0xC0000100, %rcx
movq THREAD_fsbase(%rsi), %rax
movq THREAD_fsbase(%rsi), %rdx
shr $32, %rdx
wrmsr
// set (kernel) gs-base (it will be swapped back into user-gs-base at
// the end of this function)
mov $0xC0000102, %rcx
movq THREAD_gsbase(%rsi), %rax
movq THREAD_gsbase(%rsi), %rdx
shr $32, %rdx
wrmsr
push %rbx push %rbx
pushq $0 pushq $0
push %rbp push %rbp
+1 -1
View File
@@ -1,5 +1,5 @@
# the name of the target operating system # the name of the target operating system
set(CMAKE_SYSTEM_NAME Mango) set(CMAKE_SYSTEM_NAME Magenta)
# which compilers to use for C and C++ # which compilers to use for C and C++
set(CMAKE_C_COMPILER x86_64-elf-gcc) set(CMAKE_C_COMPILER x86_64-elf-gcc)
-173
View File
@@ -1,173 +0,0 @@
#include <kernel/ringbuffer.h>
#include <kernel/sched.h>
size_t ringbuffer_unread(struct ringbuffer *ring_buffer)
{
if (ring_buffer->r_read_ptr == ring_buffer->r_write_ptr) {
return 0;
}
if (ring_buffer->r_read_ptr > ring_buffer->r_write_ptr) {
return (ring_buffer->r_size - ring_buffer->r_read_ptr)
+ ring_buffer->r_write_ptr;
} else {
return (ring_buffer->r_write_ptr - ring_buffer->r_read_ptr);
}
}
size_t ringbuffer_avail(struct ringbuffer *ring_buffer)
{
if (ring_buffer->r_read_ptr == ring_buffer->r_write_ptr) {
return ring_buffer->r_size - 1;
}
if (ring_buffer->r_read_ptr > ring_buffer->r_write_ptr) {
return ring_buffer->r_read_ptr - ring_buffer->r_write_ptr - 1;
} else {
return (ring_buffer->r_size - ring_buffer->r_write_ptr)
+ ring_buffer->r_read_ptr - 1;
}
}
static inline void increment_read(struct ringbuffer *ring_buffer)
{
ring_buffer->r_read_ptr++;
if (ring_buffer->r_read_ptr == ring_buffer->r_size) {
ring_buffer->r_read_ptr = 0;
}
}
static inline void increment_write(struct ringbuffer *ring_buffer)
{
ring_buffer->r_write_ptr++;
if (ring_buffer->r_write_ptr == ring_buffer->r_size) {
ring_buffer->r_write_ptr = 0;
}
}
size_t ringbuffer_read(struct ringbuffer *ring_buffer, size_t size, void *p, mango_flags_t flags)
{
if (!ring_buffer) {
return 0;
}
unsigned char *buffer = p;
unsigned long lock_flags;
size_t collected = 0;
while (collected < size) {
spin_lock_irqsave(&ring_buffer->r_lock, &lock_flags);
while (ringbuffer_unread(ring_buffer) > 0 && collected < size) {
buffer[collected] = ring_buffer->r_buffer[ring_buffer->r_read_ptr];
increment_read(ring_buffer);
collected++;
}
wakeup_queue(&ring_buffer->r_wait_writers);
if (flags & S_NOBLOCK) {
spin_unlock_irqrestore(&ring_buffer->r_lock, lock_flags);
break;
}
struct wait_item waiter;
wait_item_init(&waiter, current_thread());
thread_wait_begin(&waiter, &ring_buffer->r_wait_readers);
spin_unlock_irqrestore(&ring_buffer->r_lock, lock_flags);
if (collected < size) {
schedule(SCHED_NORMAL);
}
thread_wait_end(&waiter, &ring_buffer->r_wait_readers);
}
wakeup_queue(&ring_buffer->r_wait_writers);
return collected;
}
size_t ringbuffer_write(struct ringbuffer *ring_buffer, size_t size, const void *p, mango_flags_t flags)
{
if (!ring_buffer || !size) {
return 0;
}
const unsigned char *buffer = p;
unsigned long lock_flags;
size_t written = 0;
while (written < size) {
spin_lock_irqsave(&ring_buffer->r_lock, &lock_flags);
while (ringbuffer_avail(ring_buffer) > 0 && written < size) {
ring_buffer->r_buffer[ring_buffer->r_write_ptr] = buffer[written];
increment_write(ring_buffer);
written++;
}
wakeup_queue(&ring_buffer->r_wait_readers);
if (flags & S_NOBLOCK) {
spin_unlock_irqrestore(&ring_buffer->r_lock, lock_flags);
break;
}
struct wait_item waiter;
wait_item_init(&waiter, current_thread());
thread_wait_begin(&waiter, &ring_buffer->r_wait_writers);
spin_unlock_irqrestore(&ring_buffer->r_lock, lock_flags);
if (written < size) {
schedule(SCHED_NORMAL);
}
thread_wait_end(&waiter, &ring_buffer->r_wait_writers);
}
wakeup_queue(&ring_buffer->r_wait_readers);
return written;
}
struct ringbuffer *ringbuffer_create(size_t size)
{
struct ringbuffer *out = kzalloc(sizeof(struct ringbuffer), VM_NORMAL);
if (!out) {
return NULL;
}
if (ringbuffer_init(out, size) != KERN_OK) {
kfree(out);
return NULL;
}
return out;
}
void ringbuffer_destroy(struct ringbuffer *ring_buffer)
{
ringbuffer_deinit(ring_buffer);
kfree(ring_buffer);
}
kern_status_t ringbuffer_init(struct ringbuffer *buf, size_t size)
{
buf->r_buffer = kmalloc(size, VM_NORMAL);
if (!buf->r_buffer) {
return KERN_NO_MEMORY;
}
buf->r_write_ptr = 0;
buf->r_read_ptr = 0;
buf->r_size = size;
buf->r_lock = SPIN_LOCK_INIT;
return KERN_OK;
}
kern_status_t ringbuffer_deinit(struct ringbuffer *buf)
{
kfree(buf->r_buffer);
buf->r_buffer = NULL;
return KERN_OK;
}
+181
View File
@@ -0,0 +1,181 @@
#ifndef KERNEL_ADDRESS_SPACE_H_
#define KERNEL_ADDRESS_SPACE_H_
#include <kernel/object.h>
#include <kernel/pmap.h>
#include <kernel/vm.h>
#define ADDRESS_SPACE_COPY_ALL ((size_t)-1)
#define ADDRESS_SPACE_F_
struct address_space;
struct vm_object;
struct vm_area {
/* the vm-object mapped into this area.
* if this is NULL, the vm_area represents an area of reserved memory.
* it cannot be accessed, and mapping operations with MAP_ADDRESS_ANY
* will avoid the area, but fixed address mappings in this area
* will succeed. */
struct vm_object *vma_object;
/* the address space that this vm_area is a part of */
struct address_space *vma_space;
/* used to link to vm_object->vo_mappings */
struct queue_entry vma_object_entry;
/* the memory control flags applied to this area */
vm_flags_t vma_flags;
/* the memory protection flags applied to this area */
vm_prot_t vma_prot;
/* offset in bytes to the start of the object data that was mapped */
off_t vma_object_offset;
/* used to link to address_space->s_mappings */
struct btree_node vma_node;
/* address of the first byte in this area */
virt_addr_t vma_base;
/* address of the last byte in this area */
virt_addr_t vma_limit;
};
struct address_space {
struct object s_base;
/* address of the first byte in this address space */
virt_addr_t s_base_address;
/* address of the last byte in this address space */
virt_addr_t s_limit_address;
/* btree of struct vm_area representing mapped vm-objects.
* sibling entries cannot overlap each other. */
struct btree s_mappings;
/* btree of struct vm_area representing reserved regions of the
* address space.
* reserved regions will not be automatically allocated by the kernel.
* sibling entries cannot overlap each other.
* overlap between s_mappings and s_reserved IS allowed. */
struct btree s_reserved;
/* the corresponding physical address space */
pmap_t s_pmap;
};
extern kern_status_t address_space_type_init(void);
extern struct address_space *address_space_cast(struct object *obj);
/* create a new vm-region, optionally within a parent region.
* `offset` is the byte offset within the parent region where the new region
* should start.
* if no parent is specified, `offset` is the absolute virtual address of the
* start of the region.
* in both cases, `len` is the length of the new region in bytes. */
extern kern_status_t address_space_create(
virt_addr_t base,
virt_addr_t limit,
struct address_space **out);
/* map a vm-object into a vm-region.
* [region_offset,length] must fall within exactly one region, and cannot span
* multiple sibling regions.
* if [region_offset,length] falls within a child region, the map operation
* will be transparently redirected to the relevant region.
* `prot` must be allowed both by the region into which the mapping is being
* created AND the vm-object being mapped. */
extern kern_status_t address_space_map(
struct address_space *space,
virt_addr_t map_address,
struct vm_object *object,
off_t object_offset,
size_t length,
vm_flags_t flags,
vm_prot_t prot,
virt_addr_t *out);
extern kern_status_t address_space_unmap(
struct address_space *region,
virt_addr_t base,
size_t length);
/* reserve an area of the address space. the kernel will not place any
* new mappings in this area unless explicitly told to (i.e. by not using
* MAP_ADDRESS_ANY). Use MAP_ADDRESS_ANY to have the kernel allocate a region
* of the address space for you */
extern kern_status_t address_space_reserve(
struct address_space *space,
virt_addr_t base,
size_t length,
virt_addr_t *out);
/* release a previously reserved area of the address space. */
extern kern_status_t address_space_release(
struct address_space *space,
virt_addr_t base,
size_t length);
/* duplicate all of the mappings in `src` within `dest. the duplication will use
* copy-on-write; page data will not be copied until it is written to. */
extern kern_status_t address_space_duplicate(
struct address_space *dest,
struct address_space *src);
extern bool address_space_validate_access(
struct address_space *region,
virt_addr_t base,
size_t len,
vm_prot_t prot);
/* find the mapping corresponding to the given virtual address, and page-in the
* necessary vm_page to allow the memory access to succeed. if the relevant
* vm-object page hasn't been allocated yet, it will be allocated here.
* this function must be called with `region` UNLOCKED and interrupts ENABLED.
*/
extern kern_status_t address_space_demand_map(
struct address_space *region,
virt_addr_t addr,
enum pmap_fault_flags flags);
/* read data from the user-space area of a vm-region into a kernel-mode buffer
*/
extern kern_status_t address_space_read(
struct address_space *src_region,
virt_addr_t src_ptr,
size_t count,
void *dest,
size_t *nr_read);
/* write data to the user-space area of a vm-region from a kernel-mode buffer
*/
extern kern_status_t address_space_write(
struct address_space *dst_region,
virt_addr_t dst_ptr,
size_t count,
const void *src,
size_t *nr_written);
extern kern_status_t address_space_memmove(
struct address_space *dest_space,
virt_addr_t dest_ptr,
struct address_space *src_space,
virt_addr_t src_ptr,
size_t count,
size_t *nr_moved);
extern kern_status_t address_space_memmove_v(
struct address_space *dest_space,
size_t dest_offset,
const kern_iovec_t *dest_iov,
size_t nr_dest_iov,
struct address_space *src_space,
size_t src_offset,
const kern_iovec_t *src_iov,
size_t nr_src_iov,
size_t bytes_to_move,
size_t *nr_bytes_moved);
extern kern_status_t address_space_translate(
struct address_space *space,
virt_addr_t in,
phys_addr_t *out,
unsigned long *irq_flags);
void address_space_dump(struct address_space *region);
DEFINE_OBJECT_LOCK_FUNCTION(address_space, s_base)
#endif
+1 -1
View File
@@ -1,8 +1,8 @@
#ifndef KERNEL_ARG_H_ #ifndef KERNEL_ARG_H_
#define KERNEL_ARG_H_ #define KERNEL_ARG_H_
#include <magenta/types.h>
#include <stdbool.h> #include <stdbool.h>
#include <mango/status.h>
#define CMDLINE_MAX 4096 #define CMDLINE_MAX 4096
+66
View File
@@ -0,0 +1,66 @@
#ifndef KERNEL_ATOMIC_H_
#define KERNEL_ATOMIC_H_
#include <stdbool.h>
#include <stdint.h>
typedef int32_t atomic_t;
/* load and return the value pointed to by `v` */
static inline atomic_t atomic_load(atomic_t *v)
{
return __atomic_load_n(v, __ATOMIC_ACQUIRE);
}
/* store the value `v` to the pointer `dest` */
static inline void atomic_store(atomic_t *dest, atomic_t v)
{
__atomic_store_n(dest, v, __ATOMIC_ACQUIRE);
}
/* store the value `v` to the pointer `dest`, and return the value previously
* stored at `dest` */
static inline atomic_t atomic_exchange(atomic_t *dest, atomic_t v)
{
return __atomic_exchange_n(dest, v, __ATOMIC_ACQUIRE);
}
/* compare the contents of `ptr` to the contents of `expected`.
* if they match, store the value `desired` to the pointer `ptr` and return
* true. if the do NOT match, store the value `*ptr` to the pointer `desired`
* and return false.
*/
static inline bool atomic_compare_exchange(
atomic_t *ptr,
atomic_t *expected,
atomic_t desired)
{
return __atomic_compare_exchange_n(
ptr,
expected,
desired,
false,
__ATOMIC_ACQUIRE,
__ATOMIC_ACQUIRE);
}
/* perform the operation *ptr += val, and return the result */
static inline atomic_t atomic_add_fetch(atomic_t *ptr, atomic_t val)
{
return __atomic_add_fetch(ptr, val, __ATOMIC_ACQUIRE);
}
/* perform the operation *ptr -= val, and return the result */
static inline atomic_t atomic_sub_fetch(atomic_t *ptr, atomic_t val)
{
return __atomic_sub_fetch(ptr, val, __ATOMIC_ACQUIRE);
}
/* perform the operation *ptr += val, and return the previous value of *ptr */
static inline atomic_t atomic_fetch_add(atomic_t *ptr, atomic_t val)
{
return __atomic_fetch_add(ptr, val, __ATOMIC_ACQUIRE);
}
/* perform the operation *ptr -= val, and return the previous value of *ptr */
static inline atomic_t atomic_fetch_sub(atomic_t *ptr, atomic_t val)
{
return __atomic_fetch_sub(ptr, val, __ATOMIC_ACQUIRE);
}
#endif
+1 -1
View File
@@ -2,7 +2,7 @@
#define KERNEL_BSP_H_ #define KERNEL_BSP_H_
#include <kernel/compiler.h> #include <kernel/compiler.h>
#include <mango/status.h> #include <magenta/status.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
+9 -10
View File
@@ -4,12 +4,12 @@
#include <kernel/object.h> #include <kernel/object.h>
#include <kernel/sched.h> #include <kernel/sched.h>
struct kmsg; struct msg;
struct channel { struct channel {
struct object c_base; struct object c_base;
unsigned int c_id; unsigned int c_id;
struct waitqueue c_wq; unsigned int c_msg_waiting;
struct btree c_msg; struct btree c_msg;
struct btree_node c_node; struct btree_node c_node;
}; };
@@ -21,33 +21,32 @@ extern struct channel *channel_create(void);
extern kern_status_t channel_enqueue_msg( extern kern_status_t channel_enqueue_msg(
struct channel *channel, struct channel *channel,
struct kmsg *msg); struct msg *msg);
extern kern_status_t channel_recv_msg( extern kern_status_t channel_recv_msg(
struct channel *channel, struct channel *channel,
struct msg *out_msg, kern_msg_t *out_msg,
msgid_t *out_id,
unsigned long *irq_flags); unsigned long *irq_flags);
extern kern_status_t channel_reply_msg( extern kern_status_t channel_reply_msg(
struct channel *channel, struct channel *channel,
msgid_t id, msgid_t id,
const struct msg *resp, const kern_msg_t *reply,
unsigned long *irq_flags); unsigned long *irq_flags);
extern kern_status_t channel_read_msg( extern kern_status_t channel_read_msg(
struct channel *channel, struct channel *channel,
msgid_t msg, msgid_t msg,
size_t offset, size_t offset,
struct vm_region *dest_region, struct address_space *dest_region,
const struct iovec *dest_iov, const kern_iovec_t *dest_iov,
size_t dest_iov_count, size_t dest_iov_count,
size_t *nr_read); size_t *nr_read);
extern kern_status_t channel_write_msg( extern kern_status_t channel_write_msg(
struct channel *channel, struct channel *channel,
msgid_t msg, msgid_t msg,
size_t offset, size_t offset,
struct vm_region *src_region, struct address_space *src_region,
const struct iovec *src_iov, const kern_iovec_t *src_iov,
size_t src_iov_count, size_t src_iov_count,
size_t *nr_written); size_t *nr_written);
+3 -2
View File
@@ -14,9 +14,10 @@
representing a serial port may allow both sending AND receiving over the representing a serial port may allow both sending AND receiving over the
port. port.
*/ */
#include <kernel/queue.h>
#include <kernel/locks.h> #include <kernel/locks.h>
#include <mango/status.h> #include <kernel/queue.h>
#include <magenta/status.h>
#include <magenta/types.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
+3 -2
View File
@@ -1,10 +1,11 @@
#ifndef KERNEL_CPU_H_ #ifndef KERNEL_CPU_H_
#define KERNEL_CPU_H_ #define KERNEL_CPU_H_
#include <kernel/types.h>
#include <kernel/machine/cpu.h> #include <kernel/machine/cpu.h>
#include <stdint.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/types.h>
#include <kernel/work.h>
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
+34
View File
@@ -0,0 +1,34 @@
#ifndef KERNEL_EQUEUE_H_
#define KERNEL_EQUEUE_H_
#include <kernel/locks.h>
#include <kernel/object.h>
#include <kernel/sched.h>
#include <magenta/types.h>
#define EQUEUE_PACKET_MAX 100
enum equeue_flags {
EQUEUE_WAIT,
EQUEUE_DISCARD,
};
struct equeue {
struct object eq_base;
unsigned short eq_read_ptr, eq_write_ptr;
equeue_packet_t eq_packets[EQUEUE_PACKET_MAX];
struct waitqueue eq_wq;
};
extern kern_status_t equeue_type_init(void);
extern struct equeue *equeue_cast(struct object *obj);
extern struct equeue *equeue_create(void);
extern kern_status_t equeue_enqueue(
struct equeue *q,
const equeue_packet_t *pkt,
enum equeue_flags flags);
extern kern_status_t equeue_dequeue(struct equeue *q, equeue_packet_t *out);
#endif
-49
View File
@@ -1,49 +0,0 @@
#ifndef KERNEL_FB_H_
#define KERNEL_FB_H_
#include <stdint.h>
enum framebuffer_flags {
FB_MODE_RGB = 0x01u,
FB_MODE_VGATEXT = 0x02u,
FB_MODE_PALETTE = 0x04u,
};
struct framebuffer_bitfield {
uint32_t b_offset;
uint16_t b_length;
};
struct framebuffer_varinfo {
enum framebuffer_flags fb_flags;
uint32_t fb_xres;
uint32_t fb_yres;
uint32_t fb_bpp;
uint32_t fb_stride;
union {
struct {
uint32_t fb_xcells;
uint32_t fb_ycells;
};
struct {
struct framebuffer_bitfield fb_red;
struct framebuffer_bitfield fb_green;
struct framebuffer_bitfield fb_blue;
struct framebuffer_bitfield fb_alpha;
};
struct {
uintptr_t fb_palette_addr;
uint16_t fb_palette_nr_colours;
};
};
};
struct framebuffer_fixedinfo {
uint64_t fb_baseptr;
};
#endif
-11
View File
@@ -1,11 +0,0 @@
#ifndef KERNEL_FLAGS_H_
#define KERNEL_FLAGS_H_
#include <stdint.h>
typedef enum {
S_NORMAL = 0x00u,
S_NOBLOCK = 0x01u,
} mango_flags_t;
#endif
+34
View File
@@ -0,0 +1,34 @@
#ifndef KERNEL_FUTEX_H_
#define KERNEL_FUTEX_H_
#include <kernel/btree.h>
#include <kernel/wait.h>
#include <magenta/types.h>
struct task;
struct address_space;
typedef uintptr_t futex_key_t;
struct futex {
struct btree_node f_node;
futex_key_t f_key;
struct waitqueue f_waiters;
};
extern kern_status_t futex_init(void);
extern kern_status_t futex_get(
kern_futex_t *futex,
futex_key_t *out,
unsigned int flags);
extern kern_status_t futex_wait(
futex_key_t futex,
kern_futex_t new_val,
unsigned int flags);
extern kern_status_t futex_wake(
futex_key_t futex,
size_t nwaiters,
unsigned int flags);
#endif
+15 -7
View File
@@ -2,7 +2,8 @@
#define KERNEL_HANDLE_H_ #define KERNEL_HANDLE_H_
#include <kernel/bitmap.h> #include <kernel/bitmap.h>
#include <mango/status.h> #include <magenta/status.h>
#include <magenta/types.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@@ -16,6 +17,7 @@ typedef uintptr_t handle_flags_t;
struct task; struct task;
struct object; struct object;
struct address_space;
struct handle_list; struct handle_list;
struct handle { struct handle {
@@ -42,9 +44,13 @@ struct handle_table {
extern struct handle_table *handle_table_create(void); extern struct handle_table *handle_table_create(void);
extern void handle_table_destroy(struct handle_table *tab); extern void handle_table_destroy(struct handle_table *tab);
extern kern_status_t handle_table_duplicate(
struct handle_table *src,
struct handle_table **dest);
extern kern_status_t handle_table_alloc_handle( extern kern_status_t handle_table_alloc_handle(
struct handle_table *tab, struct handle_table *tab,
kern_handle_t value,
struct handle **out_slot, struct handle **out_slot,
kern_handle_t *out_handle); kern_handle_t *out_handle);
extern kern_status_t handle_table_free_handle( extern kern_status_t handle_table_free_handle(
@@ -54,12 +60,14 @@ extern struct handle *handle_table_get_handle(
struct handle_table *tab, struct handle_table *tab,
kern_handle_t handle); kern_handle_t handle);
extern kern_status_t handle_list_transfer( extern kern_status_t handle_table_transfer(
struct handle_table *dest, struct address_space *dst_region,
struct handle_list *dest_list, struct handle_table *dst,
size_t dest_list_count, kern_msg_handle_t *dst_handles,
size_t dst_handles_max,
struct address_space *src_region,
struct handle_table *src, struct handle_table *src,
const struct handle_list *src_list, kern_msg_handle_t *src_handles,
size_t src_list_count); size_t src_handles_count);
#endif #endif
-184
View File
@@ -1,184 +0,0 @@
#ifndef KERNEL_INPUT_H_
#define KERNEL_INPUT_H_
#include <stdint.h>
#include <kernel/queue.h>
#include <mango/status.h>
enum input_event_hook_flags {
INPUT_HOOK_SQUASH_EVENT = 0x01u,
};
struct device;
enum input_event_type {
INPUT_TYPE_UNKNOWN = 0x00u,
INPUT_TYPE_KEY = 0x01u,
INPUT_TYPE_MOTION = 0x02u,
};
enum input_event_motion_type {
INPUT_MOTION_TYPE_MOUSE = 0x01u,
INPUT_MOTION_TYPE_SCROLL = 0x02u,
};
enum input_keycode {
KEY_UNKNOWN = 0x00u,
KEY_A = 0x01u,
KEY_B = 0x02u,
KEY_C = 0x03u,
KEY_D = 0x04u,
KEY_E = 0x05u,
KEY_F = 0x06u,
KEY_G = 0x07u,
KEY_H = 0x08u,
KEY_I = 0x09u,
KEY_J = 0x0Au,
KEY_K = 0x0Bu,
KEY_L = 0x0Cu,
KEY_M = 0x0Du,
KEY_N = 0x0Eu,
KEY_O = 0x0Fu,
KEY_P = 0x10u,
KEY_Q = 0x11u,
KEY_R = 0x12u,
KEY_S = 0x13u,
KEY_T = 0x14u,
KEY_U = 0x15u,
KEY_V = 0x16u,
KEY_W = 0x17u,
KEY_X = 0x18u,
KEY_Y = 0x19u,
KEY_Z = 0x1Au,
KEY_KEY_1 = 0x1Bu,
KEY_KEY_2 = 0x1Cu,
KEY_KEY_3 = 0x1Du,
KEY_KEY_4 = 0x1Eu,
KEY_KEY_5 = 0x1Fu,
KEY_KEY_6 = 0x20u,
KEY_KEY_7 = 0x21u,
KEY_KEY_8 = 0x22u,
KEY_KEY_9 = 0x23u,
KEY_KEY_0 = 0x24u,
KEY_ENTER = 0x25u,
KEY_ESCAPE = 0x26u,
KEY_BACKSPACE = 0x27u,
KEY_TAB = 0x28u,
KEY_SPACE = 0x29u,
KEY_MINUS = 0x2Au,
KEY_EQUALS = 0x2Bu,
KEY_LEFT_BRACE = 0x2Cu,
KEY_RIGHT_BRACE = 0x2Du,
KEY_BACKSLASH = 0x2Eu,
KEY_NON_US_HASH = 0x2Fu,
KEY_SEMICOLON = 0x30u,
KEY_APOSTROPHE = 0x31u,
KEY_GRAVE_ACCENT = 0x32u,
KEY_COMMA = 0x33u,
KEY_DOT = 0x34u,
KEY_SLASH = 0x35u,
KEY_CAPS_LOCK = 0x36u,
KEY_F1 = 0x37u,
KEY_F2 = 0x38u,
KEY_F3 = 0x39u,
KEY_F4 = 0x3Au,
KEY_F5 = 0x3Bu,
KEY_F6 = 0x3Cu,
KEY_F7 = 0x3Du,
KEY_F8 = 0x3Eu,
KEY_F9 = 0x3Fu,
KEY_F10 = 0x40u,
KEY_F11 = 0x41u,
KEY_F12 = 0x42u,
KEY_PRINT_SCREEN = 0x43u,
KEY_SCROLL_LOCK = 0x44u,
KEY_PAUSE = 0x45u,
KEY_INSERT = 0x46u,
KEY_HOME = 0x47u,
KEY_PAGE_UP = 0x48u,
KEY_DELETE = 0x49u,
KEY_END = 0x4Au,
KEY_PAGE_DOWN = 0x4Bu,
KEY_RIGHT = 0x4Cu,
KEY_LEFT = 0x4Du,
KEY_DOWN = 0x4Eu,
KEY_UP = 0x4Fu,
KEY_NUM_LOCK = 0x50u,
KEY_KEYPAD_SLASH = 0x51u,
KEY_KEYPAD_ASTERISK = 0x52u,
KEY_KEYPAD_MINUS = 0x53u,
KEY_KEYPAD_PLUS = 0x54u,
KEY_KEYPAD_ENTER = 0x55u,
KEY_KEYPAD_1 = 0x56u,
KEY_KEYPAD_2 = 0x57u,
KEY_KEYPAD_3 = 0x58u,
KEY_KEYPAD_4 = 0x59u,
KEY_KEYPAD_5 = 0x5Au,
KEY_KEYPAD_6 = 0x5Bu,
KEY_KEYPAD_7 = 0x5Cu,
KEY_KEYPAD_8 = 0x5Du,
KEY_KEYPAD_9 = 0x5Eu,
KEY_KEYPAD_0 = 0x5Fu,
KEY_KEYPAD_DOT = 0x60u,
KEY_NON_US_BACKSLASH = 0x61u,
KEY_KEYPAD_EQUALS = 0x62u,
KEY_MENU = 0x63u,
KEY_LEFT_CTRL = 0x64u,
KEY_LEFT_SHIFT = 0x65u,
KEY_LEFT_ALT = 0x66u,
KEY_LEFT_META = 0x67u,
KEY_RIGHT_CTRL = 0x68u,
KEY_RIGHT_SHIFT = 0x69u,
KEY_RIGHT_ALT = 0x6Au,
KEY_RIGHT_META = 0x6Bu,
KEY_MEDIA_MUTE = 0x6Cu,
KEY_MEDIA_VOLUME_INCREMENT = 0x6Du,
KEY_MEDIA_VOLUME_DECREMENT = 0x6Eu,
};
enum input_key_state {
INPUT_KEYSTATE_DOWN = 0x00u,
INPUT_KEYSTATE_UP = 0x01u,
};
enum input_button {
INPUT_BUTTON_MOUSE_LEFT = 0x00u,
INPUT_BUTTON_MOUSE_MIDDLE = 0x01u,
INPUT_BUTTON_MOUSE_RIGHT = 0x02u,
INPUT_BUTTON_MOUSE_BACK = 0x03u,
INPUT_BUTTON_MOUSE_FORWARD = 0x04u,
};
enum input_button_state {
INPUT_BUTTON_DOWN = 0x00u,
INPUT_BUTTON_UP = 0x01u,
};
struct input_event {
enum input_event_type ev_type;
union {
struct {
enum input_event_motion_type type;
int16_t movement_x;
int16_t movement_y;
} ev_motion;
struct {
enum input_button button;
enum input_button_state state;
} ev_button;
struct {
enum input_keycode key;
enum input_key_state state;
} ev_key;
};
};
struct input_event_hook {
void(*hook_callback)(struct device *, struct input_event *, enum input_event_hook_flags *, void *);
void *hook_arg;
struct queue_entry hook_head;
};
#endif
+8 -6
View File
@@ -1,14 +1,16 @@
#ifndef KERNEL_IOVEC_H_ #ifndef KERNEL_IOVEC_H_
#define KERNEL_IOVEC_H_ #define KERNEL_IOVEC_H_
#include <mango/types.h> #include <magenta/types.h>
#include <stddef.h> #include <stddef.h>
struct address_space;
struct iovec_iterator { struct iovec_iterator {
/* if this is set, we are iterating over a list of iovecs stored in /* if this is set, we are iterating over a list of iovecs stored in
* userspace, and must go through this region to retrieve the data. */ * userspace, and must go through this region to retrieve the data. */
struct vm_region *it_region; struct address_space *it_region;
const struct iovec *it_vecs; const kern_iovec_t *it_vecs;
size_t it_nr_vecs; size_t it_nr_vecs;
size_t it_vec_ptr; size_t it_vec_ptr;
@@ -18,12 +20,12 @@ struct iovec_iterator {
extern void iovec_iterator_begin( extern void iovec_iterator_begin(
struct iovec_iterator *it, struct iovec_iterator *it,
const struct iovec *vecs, const kern_iovec_t *vecs,
size_t nr_vecs); size_t nr_vecs);
extern void iovec_iterator_begin_user( extern void iovec_iterator_begin_user(
struct iovec_iterator *it, struct iovec_iterator *it,
struct vm_region *address_space, struct address_space *address_space,
const struct iovec *vecs, const kern_iovec_t *vecs,
size_t nr_vecs); size_t nr_vecs);
extern void iovec_iterator_seek(struct iovec_iterator *it, size_t nr_bytes); extern void iovec_iterator_seek(struct iovec_iterator *it, size_t nr_bytes);
+58
View File
@@ -22,6 +22,64 @@ typedef __aligned(8) ml_hwlock_t spin_lock_t;
#define spin_unlock_irqrestore(lck, flags) \ #define spin_unlock_irqrestore(lck, flags) \
ml_hwlock_unlock_irqrestore(lck, flags); ml_hwlock_unlock_irqrestore(lck, flags);
static inline void spin_lock_pair(spin_lock_t *a, spin_lock_t *b)
{
if (a == b) {
spin_lock(a);
} else if (a < b) {
spin_lock(a);
spin_lock(b);
} else {
spin_lock(b);
spin_lock(a);
}
}
static inline void spin_unlock_pair(spin_lock_t *a, spin_lock_t *b)
{
if (a == b) {
spin_unlock(a);
} else if (a < b) {
spin_unlock(b);
spin_unlock(a);
} else {
spin_unlock(a);
spin_unlock(b);
}
}
static inline void spin_lock_pair_irqsave(
spin_lock_t *a,
spin_lock_t *b,
unsigned long *flags)
{
if (a == b) {
spin_lock_irqsave(a, flags);
} else if (a < b) {
spin_lock_irqsave(a, flags);
spin_lock(b);
} else {
spin_lock_irqsave(b, flags);
spin_lock(a);
}
}
static inline void spin_unlock_pair_irqrestore(
spin_lock_t *a,
spin_lock_t *b,
unsigned long flags)
{
if (a == b) {
spin_unlock_irqrestore(a, flags);
} else if (a < b) {
spin_unlock(b);
spin_unlock_irqrestore(a, flags);
} else {
spin_unlock(a);
spin_unlock_irqrestore(b, flags);
}
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+34 -6
View File
@@ -3,8 +3,8 @@
#include <kernel/btree.h> #include <kernel/btree.h>
#include <kernel/locks.h> #include <kernel/locks.h>
#include <mango/status.h> #include <magenta/status.h>
#include <mango/types.h> #include <magenta/types.h>
struct port; struct port;
struct thread; struct thread;
@@ -13,18 +13,46 @@ enum kmsg_status {
KMSG_WAIT_RECEIVE, KMSG_WAIT_RECEIVE,
KMSG_WAIT_REPLY, KMSG_WAIT_REPLY,
KMSG_REPLY_SENT, KMSG_REPLY_SENT,
KMSG_ASYNC,
}; };
struct kmsg { struct msg {
spin_lock_t msg_lock; spin_lock_t msg_lock;
enum kmsg_status msg_status; enum kmsg_status msg_status;
struct btree_node msg_node; struct btree_node msg_node;
msgid_t msg_id; msgid_t msg_id;
kern_status_t msg_result; union {
/* only valid for asynchronous messages (msg_status ==
* KMSG_ASYNC) */
struct {
koid_t msg_sender_port_id;
tid_t msg_sender_thread_id;
};
/* only valid for synchronous messages (msg_status !=
* KMSG_ASYNC) */
struct {
struct port *msg_sender_port; struct port *msg_sender_port;
struct thread *msg_sender_thread; struct thread *msg_sender_thread;
struct msg msg_req; };
struct msg msg_resp; };
kern_status_t msg_result;
kern_msg_type_t msg_type;
union {
/* msg_type = KERN_MSG_TYPE_DATA */
struct {
kern_msg_t msg_req, msg_resp;
};
/* msg_type = KERN_MSG_TYPE_EVENT */
struct {
kern_msg_event_type_t msg_event;
};
};
}; };
extern void msg_init(void);
extern struct msg *msg_alloc(void);
extern void msg_free(struct msg *msg);
#endif #endif
+53 -11
View File
@@ -1,10 +1,11 @@
#ifndef KERNEL_OBJECT_H_ #ifndef KERNEL_OBJECT_H_
#define KERNEL_OBJECT_H_ #define KERNEL_OBJECT_H_
#include <kernel/flags.h> #include <kernel/atomic.h>
#include <kernel/locks.h> #include <kernel/locks.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#include <mango/status.h> #include <kernel/wait.h>
#include <magenta/status.h>
#include <stddef.h> #include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
@@ -31,6 +32,32 @@ extern "C" {
unsigned long flags) \ unsigned long flags) \
{ \ { \
object_unlock_irqrestore(&p->base, flags); \ object_unlock_irqrestore(&p->base, flags); \
} \
static inline void object_name##_lock_pair( \
struct object_name *a, \
struct object_name *b) \
{ \
object_lock_pair(&a->base, &b->base); \
} \
static inline void object_name##_unlock_pair( \
struct object_name *a, \
struct object_name *b) \
{ \
object_unlock_pair(&a->base, &b->base); \
} \
static inline void object_name##_lock_pair_irqsave( \
struct object_name *a, \
struct object_name *b, \
unsigned long *flags) \
{ \
object_lock_pair_irqsave(&a->base, &b->base, flags); \
} \
static inline void object_name##_unlock_pair_irqrestore( \
struct object_name *a, \
struct object_name *b, \
unsigned long flags) \
{ \
object_unlock_pair_irqrestore(&a->base, &b->base, flags); \
} }
#define OBJECT_MAGIC 0xBADDCAFE #define OBJECT_MAGIC 0xBADDCAFE
@@ -38,7 +65,7 @@ extern "C" {
#define OBJECT_PATH_MAX 256 #define OBJECT_PATH_MAX 256
#define OBJECT_CAST(to_type, to_type_member, p) \ #define OBJECT_CAST(to_type, to_type_member, p) \
((to_type *)((uintptr_t)p) - offsetof(to_type, to_type_member)) ((to_type *)(((uintptr_t)p) - offsetof(to_type, to_type_member)))
#define OBJECT_C_CAST(c_type, c_type_member, obj_type, objp) \ #define OBJECT_C_CAST(c_type, c_type_member, obj_type, objp) \
OBJECT_IS_TYPE(objp, obj_type) \ OBJECT_IS_TYPE(objp, obj_type) \
? OBJECT_CAST(c_type, c_type_member, (objp)) : NULL ? OBJECT_CAST(c_type, c_type_member, (objp)) : NULL
@@ -52,10 +79,7 @@ enum object_type_flags {
}; };
struct object_ops { struct object_ops {
kern_status_t (*destroy)(struct object *obj, struct queue *q); kern_status_t (*destroy)(struct object *obj);
kern_status_t (*destroy_recurse)(
struct queue_entry *entry,
struct object **out);
}; };
struct object_type { struct object_type {
@@ -73,9 +97,10 @@ struct object {
koid_t ob_id; koid_t ob_id;
struct object_type *ob_type; struct object_type *ob_type;
spin_lock_t ob_lock; spin_lock_t ob_lock;
unsigned int ob_refcount; uint32_t ob_signals;
unsigned int ob_handles; atomic_t ob_refcount;
struct queue_entry ob_list; struct queue_entry ob_list;
struct waitqueue ob_wq;
} __aligned(sizeof(long)); } __aligned(sizeof(long));
extern kern_status_t object_bootstrap(void); extern kern_status_t object_bootstrap(void);
@@ -85,13 +110,30 @@ extern kern_status_t object_type_unregister(struct object_type *p);
extern struct object *object_create(struct object_type *type); extern struct object *object_create(struct object_type *type);
extern struct object *object_ref(struct object *obj); extern struct object *object_ref(struct object *obj);
extern void object_unref(struct object *obj); extern void object_unref(struct object *obj);
extern void object_add_handle(struct object *obj);
extern void object_remove_handle(struct object *obj);
extern void object_lock(struct object *obj); extern void object_lock(struct object *obj);
extern void object_unlock(struct object *obj); extern void object_unlock(struct object *obj);
extern void object_lock_irqsave(struct object *obj, unsigned long *flags); extern void object_lock_irqsave(struct object *obj, unsigned long *flags);
extern void object_unlock_irqrestore(struct object *obj, unsigned long flags); extern void object_unlock_irqrestore(struct object *obj, unsigned long flags);
extern void object_lock_pair(struct object *a, struct object *b);
extern void object_unlock_pair(struct object *a, struct object *b);
extern void object_lock_pair_irqsave(
struct object *a,
struct object *b,
unsigned long *flags);
extern void object_unlock_pair_irqrestore(
struct object *a,
struct object *b,
unsigned long flags);
extern void object_assert_signal(struct object *obj, uint32_t signals);
extern void object_clear_signal(struct object *obj, uint32_t signals);
extern void object_wait_signal(
struct object *obj,
uint32_t signals,
unsigned long *irq_flags);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef KERNEL_PERCPU_H_ #ifndef KERNEL_PERCPU_H_
#define KERNEL_PERCPU_H_ #define KERNEL_PERCPU_H_
#include <mango/status.h> #include <magenta/status.h>
#include <kernel/compiler.h> #include <kernel/compiler.h>
#include <kernel/sched.h> #include <kernel/sched.h>
+10 -2
View File
@@ -5,11 +5,10 @@
#include <kernel/machine/pmap.h> #include <kernel/machine/pmap.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#include <mango/status.h> #include <magenta/status.h>
#include <stddef.h> #include <stddef.h>
#define PMAP_INVALID ML_PMAP_INVALID #define PMAP_INVALID ML_PMAP_INVALID
#define PFN(x) ((x) >> VM_PAGE_SHIFT)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -53,10 +52,19 @@ extern pmap_t pmap_create(void);
extern void pmap_destroy(pmap_t pmap); extern void pmap_destroy(pmap_t pmap);
extern void pmap_switch(pmap_t pmap); extern void pmap_switch(pmap_t pmap);
extern void pmap_flush(void);
extern void pmap_flush_page(virt_addr_t p);
extern void pmap_flush_range(virt_addr_t start, size_t length);
extern kern_status_t pmap_handle_fault( extern kern_status_t pmap_handle_fault(
virt_addr_t fault_addr, virt_addr_t fault_addr,
enum pmap_fault_flags flags); enum pmap_fault_flags flags);
extern kern_status_t pmap_get(
pmap_t pmap,
virt_addr_t p,
pfn_t *out_pfn,
vm_prot_t *out_prot);
extern kern_status_t pmap_add( extern kern_status_t pmap_add(
pmap_t pmap, pmap_t pmap,
virt_addr_t p, virt_addr_t p,
+2 -8
View File
@@ -9,12 +9,6 @@ enum port_status {
PORT_OFFLINE = 0, PORT_OFFLINE = 0,
/* port is connected and ready to send messages */ /* port is connected and ready to send messages */
PORT_READY, PORT_READY,
/* port has sent a message, and is waiting for the remote to receive it
*/
PORT_SEND_BLOCKED,
/* port has sent a message, and the remote has received it. waiting for
* the remote to reply. */
PORT_REPLY_BLOCKED,
}; };
struct port { struct port {
@@ -32,8 +26,8 @@ extern kern_status_t port_connect(struct port *port, struct channel *remote);
extern kern_status_t port_disconnect(struct port *port); extern kern_status_t port_disconnect(struct port *port);
extern kern_status_t port_send_msg( extern kern_status_t port_send_msg(
struct port *port, struct port *port,
const struct msg *req, const kern_msg_t *msg,
struct msg *resp, kern_msg_t *out_response,
unsigned long *lock_flags); unsigned long *lock_flags);
DEFINE_OBJECT_LOCK_FUNCTION(port, p_base) DEFINE_OBJECT_LOCK_FUNCTION(port, p_base)
-34
View File
@@ -1,34 +0,0 @@
#ifndef KERNEL_RINGBUFFER_H_
#define KERNEL_RINGBUFFER_H_
#include <kernel/locks.h>
#include <kernel/sched.h>
struct ringbuffer {
unsigned char *r_buffer;
size_t r_write_ptr;
size_t r_read_ptr;
size_t r_size;
spin_lock_t r_lock;
struct waitqueue r_wait_readers;
struct waitqueue r_wait_writers;
};
extern struct ringbuffer *ringbuffer_create(size_t size);
extern void ringbuffer_destroy(struct ringbuffer *buf);
extern kern_status_t ringbuffer_init(struct ringbuffer *buf, size_t size);
extern kern_status_t ringbuffer_deinit(struct ringbuffer *buf);
extern size_t ringbuffer_unread(struct ringbuffer *buf);
extern size_t ringbuffer_avail(struct ringbuffer *buf);
extern size_t ringbuffer_read(struct ringbuffer *buf, size_t size, void *buffer, mango_flags_t flags);
extern size_t ringbuffer_write(struct ringbuffer *buf, size_t size, const void *buffer, mango_flags_t flags);
/* TODO */
//extern size_t ringbuffer_peek(struct ringbuffer *buf, size_t at, size_t size, void *buffer);
//extern size_t ringbuffer_skip(struct ringbuffer *buf, size_t count);
extern int ringbuffer_write_would_block(struct ringbuffer *buf);
#endif
+6 -183
View File
@@ -4,32 +4,11 @@
#include <kernel/btree.h> #include <kernel/btree.h>
#include <kernel/handle.h> #include <kernel/handle.h>
#include <kernel/locks.h> #include <kernel/locks.h>
#include <kernel/msg.h>
#include <kernel/object.h>
#include <kernel/pmap.h>
#include <kernel/queue.h> #include <kernel/queue.h>
#include <mango/status.h> #include <kernel/types.h>
#include <magenta/status.h>
#define TASK_NAME_MAX 64
#define PRIO_MAX 32 #define PRIO_MAX 32
#define PID_MAX 99999
#define THREAD_KSTACK_ORDER VM_PAGE_4K
#define THREAD_MAX 65536
#define wait_event(wq, cond) \
({ \
struct thread *self = current_thread(); \
struct wait_item waiter; \
wait_item_init(&waiter, self); \
for (;;) { \
thread_wait_begin(&waiter, wq); \
if (cond) { \
break; \
} \
schedule(SCHED_NORMAL); \
} \
thread_wait_end(&waiter, wq); \
})
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -37,23 +16,6 @@ extern "C" {
struct channel; struct channel;
struct runqueue; struct runqueue;
struct work_item;
enum task_state {
TASK_RUNNING,
TASK_STOPPED,
};
enum thread_state {
THREAD_READY = 1,
THREAD_SLEEPING = 2,
THREAD_STOPPED = 3,
};
enum thread_flags {
THREAD_F_NEED_RESCHED = 0x01u,
THREAD_F_NO_PREEMPT = 0x02u,
};
enum sched_priority { enum sched_priority {
PRIO_IDLE = 4, PRIO_IDLE = 4,
@@ -75,54 +37,6 @@ enum sched_mode {
SCHED_IRQ = 1, SCHED_IRQ = 1,
}; };
struct task {
struct object t_base;
struct task *t_parent;
long t_id;
enum task_state t_state;
char t_name[TASK_NAME_MAX];
pmap_t t_pmap;
struct vm_region *t_address_space;
struct handle_table *t_handles;
struct btree b_channels;
struct btree_node t_tasklist;
struct queue_entry t_child_entry;
size_t t_next_thread_id;
struct queue t_threads;
struct queue t_children;
};
struct thread {
struct object thr_base;
enum thread_state tr_state;
enum thread_flags tr_flags;
struct task *tr_parent;
unsigned int tr_id;
unsigned int tr_prio;
cycles_t tr_charge_period_start;
cycles_t tr_quantum_cycles, tr_quantum_target;
cycles_t tr_total_cycles;
virt_addr_t tr_ip, tr_sp, tr_bp;
virt_addr_t tr_cpu_user_sp, tr_cpu_kernel_sp;
struct runqueue *tr_rq;
struct kmsg tr_msg;
struct queue_entry tr_parent_entry;
struct queue_entry tr_rqentry;
struct vm_page *tr_kstack;
struct vm_object *tr_ustack;
};
struct runqueue { struct runqueue {
struct queue rq_queues[PRIO_MAX]; struct queue rq_queues[PRIO_MAX];
uint32_t rq_readybits; uint32_t rq_readybits;
@@ -140,34 +54,6 @@ struct timer {
void (*t_callback)(struct timer *); void (*t_callback)(struct timer *);
}; };
struct wait_item {
struct thread *w_thread;
struct queue_entry w_entry;
};
struct waitqueue {
struct queue wq_waiters;
spin_lock_t wq_lock;
};
typedef void (*work_func_t)(struct work_item *);
struct work_item {
void *w_data;
work_func_t w_func;
struct queue_entry w_head;
};
struct worker_pool {
struct thread **wp_workers;
size_t wp_nworkers;
};
struct workqueue {
spin_lock_t wq_lock;
struct queue wq_queue; /* list of struct work_item */
};
extern kern_status_t sched_init(void); extern kern_status_t sched_init(void);
extern void schedule(enum sched_mode mode); extern void schedule(enum sched_mode mode);
extern void preempt_disable(void); extern void preempt_disable(void);
@@ -187,43 +73,13 @@ static inline void rq_unlock(struct runqueue *rq, unsigned long flags)
extern void rq_remove_thread(struct runqueue *rq, struct thread *thr); extern void rq_remove_thread(struct runqueue *rq, struct thread *thr);
extern struct runqueue *cpu_rq(unsigned int cpu); extern struct runqueue *cpu_rq(unsigned int cpu);
extern struct task *task_alloc(void);
extern struct task *task_cast(struct object *obj);
extern struct task *task_create(const char *name, size_t name_len);
static inline struct task *task_ref(struct task *task)
{
return OBJECT_CAST(struct task, t_base, object_ref(&task->t_base));
}
static inline void task_unref(struct task *task)
{
object_unref(&task->t_base);
}
extern kern_status_t task_add_child(struct task *parent, struct task *child);
extern kern_status_t task_add_channel(
struct task *task,
struct channel *channel,
unsigned int id);
extern struct channel *task_get_channel(struct task *task, unsigned int id);
extern struct task *task_from_tid(tid_t id);
extern kern_status_t task_open_handle(
struct task *task,
struct object *obj,
handle_flags_t flags,
kern_handle_t *out);
extern kern_status_t task_resolve_handle(
struct task *task,
kern_handle_t handle,
struct object **out_obj,
handle_flags_t *out_flags);
extern kern_status_t task_close_handle(struct task *task, kern_handle_t handle);
extern struct thread *task_create_thread(struct task *parent);
extern struct task *kernel_task(void);
extern struct task *idle_task(void);
extern cycles_t default_quantum(void); extern cycles_t default_quantum(void);
extern bool need_resched(void); extern bool need_resched(void);
extern struct task *current_task(void); extern struct task *get_current_task(void);
extern struct thread *current_thread(void); extern struct thread *get_current_thread(void);
extern void put_current_task(struct task *task);
extern void put_current_thread(struct thread *thread);
extern struct runqueue *select_rq_for_thread(struct thread *thr); extern struct runqueue *select_rq_for_thread(struct thread *thr);
extern void schedule_thread_on_cpu(struct thread *thr); extern void schedule_thread_on_cpu(struct thread *thr);
@@ -231,45 +87,12 @@ extern void schedule_thread_on_cpu(struct thread *thr);
extern void start_charge_period(void); extern void start_charge_period(void);
extern void end_charge_period(void); extern void end_charge_period(void);
DEFINE_OBJECT_LOCK_FUNCTION(task, t_base)
extern struct thread *thread_alloc(void);
extern struct thread *thread_cast(struct object *obj);
extern kern_status_t thread_init_kernel(struct thread *thr, virt_addr_t ip);
extern kern_status_t thread_init_user(
struct thread *thr,
virt_addr_t ip,
virt_addr_t sp,
const uintptr_t *args,
size_t nr_args);
extern int thread_priority(struct thread *thr);
extern void thread_awaken(struct thread *thr);
extern void idle(void);
extern struct thread *create_kernel_thread(void (*fn)(void));
extern struct thread *create_idle_thread(void);
extern void add_timer(struct timer *timer); extern void add_timer(struct timer *timer);
extern void remove_timer(struct timer *timer); extern void remove_timer(struct timer *timer);
extern unsigned long schedule_timeout(unsigned long clock_ticks); extern unsigned long schedule_timeout(unsigned long clock_ticks);
extern unsigned long milli_sleep(unsigned long ms); extern unsigned long milli_sleep(unsigned long ms);
extern void sleep_forever(void); extern void sleep_forever(void);
extern void wait_item_init(struct wait_item *item, struct thread *thr);
extern void thread_wait_begin(struct wait_item *waiter, struct waitqueue *q);
extern void thread_wait_end(struct wait_item *waiter, struct waitqueue *q);
extern void wait_on_queue(struct waitqueue *q);
extern void wakeup_queue(struct waitqueue *q);
extern void wakeup_one(struct waitqueue *q);
extern void work_item_init(work_func_t func, void *data, struct work_item *out);
extern void workqueue_init(struct workqueue *wq);
extern struct worker_pool *worker_pool_create(size_t nworkers);
extern struct worker_pool *global_worker_pool(void);
extern bool schedule_work_on(struct workqueue *wq, struct work_item *work);
extern bool schedule_work(struct work_item *work);
extern void wake_workers(struct workqueue *wq, struct worker_pool *pool);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+136 -66
View File
@@ -1,17 +1,15 @@
#ifndef KERNEL_SYSCALL_H_ #ifndef KERNEL_SYSCALL_H_
#define KERNEL_SYSCALL_H_ #define KERNEL_SYSCALL_H_
#include <kernel/address-space.h>
#include <kernel/handle.h> #include <kernel/handle.h>
#include <kernel/task.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#include <mango/status.h> #include <magenta/status.h>
#include <mango/syscall.h> #include <magenta/syscall.h>
#define validate_access(task, ptr, len, flags) \ #define validate_access(task, ptr, len, flags) \
vm_region_validate_access( \ __validate_access(task, (const void *)ptr, len, flags)
task->t_address_space, \
(virt_addr_t)ptr, \
len, \
flags | VM_PROT_USER)
#define validate_access_r(task, ptr, len) \ #define validate_access_r(task, ptr, len) \
validate_access(task, ptr, len, VM_PROT_READ | VM_PROT_USER) validate_access(task, ptr, len, VM_PROT_READ | VM_PROT_USER)
#define validate_access_w(task, ptr, len) \ #define validate_access_w(task, ptr, len) \
@@ -23,10 +21,28 @@
len, \ len, \
VM_PROT_READ | VM_PROT_WRITE | VM_PROT_USER) VM_PROT_READ | VM_PROT_WRITE | VM_PROT_USER)
static inline bool __validate_access(
struct task *task,
const void *ptr,
size_t len,
vm_prot_t flags)
{
unsigned long irq_flags;
address_space_lock_irqsave(task->t_address_space, &irq_flags);
bool result = address_space_validate_access(
task->t_address_space,
(virt_addr_t)ptr,
len,
flags | VM_PROT_USER);
address_space_unlock_irqrestore(task->t_address_space, irq_flags);
return result;
}
extern kern_status_t sys_task_exit(int status); extern kern_status_t sys_task_exit(int status);
extern kern_status_t sys_task_self(kern_handle_t *out); extern kern_status_t sys_task_self(kern_handle_t *out);
extern kern_status_t sys_task_create( extern kern_status_t sys_task_create(
kern_handle_t parent_handle, kern_handle_t parent_handle,
task_flags_t flags,
const char *name, const char *name,
size_t name_len, size_t name_len,
kern_handle_t *out_task, kern_handle_t *out_task,
@@ -41,8 +57,33 @@ extern kern_status_t sys_task_create_thread(
extern kern_status_t sys_task_get_address_space( extern kern_status_t sys_task_get_address_space(
kern_handle_t task, kern_handle_t task,
kern_handle_t *out); kern_handle_t *out);
extern kern_status_t sys_task_config_get(
kern_handle_t task,
kern_config_key_t key,
void *ptr,
size_t len);
extern kern_status_t sys_task_config_set(
kern_handle_t task,
kern_config_key_t key,
const void *ptr,
size_t len);
extern kern_status_t sys_task_duplicate(
kern_handle_t *out_task,
kern_handle_t *out_address_space);
extern kern_status_t sys_thread_self(kern_handle_t *out);
extern kern_status_t sys_thread_start(kern_handle_t thread); extern kern_status_t sys_thread_start(kern_handle_t thread);
extern kern_status_t sys_thread_exit(void);
extern kern_status_t sys_thread_config_get(
kern_handle_t thread,
kern_config_key_t key,
void *ptr,
size_t len);
extern kern_status_t sys_thread_config_set(
kern_handle_t thread,
kern_config_key_t key,
const void *ptr,
size_t len);
extern kern_status_t sys_vm_object_create( extern kern_status_t sys_vm_object_create(
const char *name, const char *name,
@@ -70,55 +111,56 @@ extern kern_status_t sys_vm_object_copy(
size_t count, size_t count,
size_t *nr_copied); size_t *nr_copied);
extern kern_status_t sys_vm_region_create( extern kern_status_t sys_address_space_read(
kern_handle_t parent,
const char *name,
size_t name_len,
off_t offset,
size_t region_len,
vm_prot_t prot,
kern_handle_t *out,
virt_addr_t *out_base_address);
extern kern_status_t sys_vm_region_kill(kern_handle_t region);
extern kern_status_t sys_vm_region_read(
kern_handle_t region, kern_handle_t region,
void *dst, void *dst,
off_t offset, virt_addr_t base,
size_t count, size_t count,
size_t *nr_read); size_t *nr_read);
extern kern_status_t sys_vm_region_write( extern kern_status_t sys_address_space_write(
kern_handle_t region, kern_handle_t region,
const void *src, const void *src,
off_t offset, virt_addr_t base,
size_t count, size_t count,
size_t *nr_read); size_t *nr_read);
extern kern_status_t sys_vm_region_map_absolute( extern kern_status_t sys_address_space_map(
kern_handle_t region, kern_handle_t region,
virt_addr_t map_address, virt_addr_t map_address,
kern_handle_t object, kern_handle_t object,
off_t object_offset, off_t object_offset,
size_t length, size_t length,
vm_flags_t flags,
vm_prot_t prot, vm_prot_t prot,
virt_addr_t *out_base_address); virt_addr_t *out_base_address);
extern kern_status_t sys_vm_region_map_relative( extern kern_status_t sys_address_space_unmap(
kern_handle_t region, kern_handle_t region,
off_t region_offset, virt_addr_t base,
kern_handle_t object,
off_t object_offset,
size_t length,
vm_prot_t prot,
virt_addr_t *out_base_address);
extern kern_status_t sys_vm_region_unmap_absolute(
kern_handle_t region,
virt_addr_t address,
size_t length); size_t length);
extern kern_status_t sys_vm_region_unmap_relative( extern kern_status_t sys_address_space_reserve(
kern_handle_t region, kern_handle_t region,
off_t offset, virt_addr_t base,
size_t length,
virt_addr_t *out_base_address);
extern kern_status_t sys_address_space_release(
kern_handle_t region,
virt_addr_t base,
size_t length); size_t length);
extern kern_status_t sys_kern_log(const char *s); extern kern_status_t sys_kern_log(const char *s);
extern kern_status_t sys_kern_handle_close(kern_handle_t handle); extern kern_status_t sys_kern_handle_close(kern_handle_t handle);
extern kern_status_t sys_kern_handle_transfer(
kern_handle_t src_task_handle,
kern_handle_t src_handle,
kern_handle_t dest_task_handle,
kern_handle_t dest_handle,
unsigned int mode,
kern_handle_t *out_handle);
extern kern_status_t sys_kern_handle_control(
kern_handle_t task,
kern_handle_t handle,
uint32_t set_mask,
uint32_t clear_mask,
uint32_t *out_flags);
extern kern_status_t sys_kern_config_get( extern kern_status_t sys_kern_config_get(
kern_config_key_t key, kern_config_key_t key,
void *ptr, void *ptr,
@@ -128,10 +170,7 @@ extern kern_status_t sys_kern_config_set(
const void *ptr, const void *ptr,
size_t len); size_t len);
extern kern_status_t sys_channel_create( extern kern_status_t sys_channel_create(unsigned int id, kern_handle_t *out);
unsigned int id,
channel_flags_t flags,
kern_handle_t *out);
extern kern_status_t sys_port_create(kern_handle_t *out); extern kern_status_t sys_port_create(kern_handle_t *out);
extern kern_status_t sys_port_connect( extern kern_status_t sys_port_connect(
kern_handle_t port, kern_handle_t port,
@@ -141,48 +180,79 @@ extern kern_status_t sys_port_disconnect(kern_handle_t port);
extern kern_status_t sys_msg_send( extern kern_status_t sys_msg_send(
kern_handle_t port, kern_handle_t port,
msg_flags_t flags, const kern_msg_t *msg,
const struct msg *req, kern_msg_t *out_reply);
struct msg *resp); extern kern_status_t sys_msg_recv(kern_handle_t channel, kern_msg_t *out_msg);
extern kern_status_t sys_msg_recv(
kern_handle_t channel,
msg_flags_t flags,
msgid_t *out_id,
struct msg *out_msg);
extern kern_status_t sys_msg_reply( extern kern_status_t sys_msg_reply(
kern_handle_t channel, kern_handle_t channel,
msg_flags_t flags,
msgid_t id, msgid_t id,
const struct msg *reply); const kern_msg_t *msg);
extern kern_status_t sys_msg_read( extern kern_status_t sys_msg_read(
kern_handle_t channel_handle, kern_handle_t channel_handle,
msgid_t id, msgid_t id,
size_t offset, size_t offset,
const struct iovec *iov, const kern_iovec_t *iov,
size_t iov_count, size_t iov_count,
size_t *nr_read); size_t *nr_read);
extern kern_status_t sys_msg_read_handles(
kern_handle_t channel,
msgid_t id,
size_t offset,
struct handle_list *out,
size_t nr_out);
extern kern_status_t sys_msg_write( extern kern_status_t sys_msg_write(
kern_handle_t channel, kern_handle_t channel,
msgid_t id, msgid_t id,
size_t offset, size_t offset,
const struct iovec *in, const kern_iovec_t *in,
size_t nr_in); size_t nr_in,
extern kern_status_t sys_msg_write_handles( size_t *nr_written);
kern_handle_t channel,
msgid_t id, extern kern_status_t sys_kern_object_wait(
size_t offset, kern_wait_item_t *items,
const struct handle_list *in, size_t nr_items);
size_t nr_in); extern kern_status_t sys_kern_object_query(
kern_handle_t object,
kern_object_info_t *out);
extern kern_status_t sys_vm_controller_create(kern_handle_t *out);
extern kern_status_t sys_vm_controller_recv(
kern_handle_t ctrl,
equeue_packet_vm_request_t *out);
extern kern_status_t sys_vm_controller_recv_async(
kern_handle_t ctrl,
kern_handle_t eq,
equeue_key_t key);
extern kern_status_t sys_vm_controller_create_object(
kern_handle_t ctrl,
const char *name,
size_t name_len,
equeue_key_t key,
size_t data_len,
vm_prot_t prot,
kern_handle_t *out);
extern kern_status_t sys_vm_controller_prepare_attach(
kern_handle_t ctrl,
uint64_t req_id,
kern_handle_t *out_vmo);
extern kern_status_t sys_vm_controller_finish_attach(
kern_handle_t ctrl,
uint64_t req_id,
equeue_key_t new_key);
extern kern_status_t sys_vm_controller_detach_object(
kern_handle_t ctrl,
kern_handle_t vmo);
extern kern_status_t sys_vm_controller_supply_pages(
kern_handle_t ctrl,
kern_handle_t dst_vmo,
off_t dst_offset,
kern_handle_t src_vmo,
off_t src_offset,
size_t count);
extern kern_status_t sys_futex_wait(
kern_futex_t *futex,
kern_futex_t new_val,
unsigned int flags);
extern kern_status_t sys_futex_wake(
kern_futex_t *futex,
unsigned int nr_waiters,
unsigned int flags);
extern virt_addr_t syscall_get_function(unsigned int sysid); extern virt_addr_t syscall_get_function(unsigned int sysid);
+90
View File
@@ -0,0 +1,90 @@
#ifndef KERNEL_TASK_H_
#define KERNEL_TASK_H_
#include <kernel/handle.h>
#include <kernel/object.h>
#include <kernel/pmap.h>
#define TASK_NAME_MAX 64
#define PID_MAX 99999
struct channel;
enum task_state {
TASK_RUNNING,
TASK_STOPPED,
};
struct task {
struct object t_base;
struct task *t_parent;
long t_id;
enum task_state t_state;
char t_name[TASK_NAME_MAX];
pmap_t t_pmap;
struct address_space *t_address_space;
spin_lock_t t_handles_lock;
struct handle_table *t_handles;
struct btree t_channels, t_futex;
struct btree_node t_tasklist;
struct queue_entry t_child_entry;
size_t t_next_thread_id;
struct queue t_threads;
struct queue t_children;
};
extern struct task *task_alloc(void);
extern struct task *task_cast(struct object *obj);
extern struct task *task_create(
struct task *parent,
task_flags_t flags,
const char *name,
size_t name_len);
static inline struct task *task_ref(struct task *task)
{
return OBJECT_CAST(struct task, t_base, object_ref(&task->t_base));
}
static inline void task_unref(struct task *task)
{
object_unref(&task->t_base);
}
extern void task_exit(int status);
extern kern_status_t task_add_child(struct task *parent, struct task *child);
extern kern_status_t task_add_channel(
struct task *task,
struct channel *channel,
unsigned int id);
extern struct channel *task_get_channel(struct task *task, unsigned int id);
extern struct task *task_from_tid(tid_t id);
extern kern_status_t task_open_handle(
struct task *task,
struct object *obj,
handle_flags_t flags,
kern_handle_t *out);
extern kern_status_t task_resolve_handle(
struct task *task,
kern_handle_t handle,
struct object **out_obj,
handle_flags_t *out_flags);
extern kern_status_t task_config_get(
struct task *task,
kern_config_key_t key,
void *out,
size_t max);
extern kern_status_t task_config_set(
struct task *task,
kern_config_key_t key,
const void *ptr,
size_t len);
extern kern_status_t task_close_handle(struct task *task, kern_handle_t handle);
extern struct thread *task_create_thread(struct task *parent);
extern struct task *kernel_task(void);
extern struct task *idle_task(void);
DEFINE_OBJECT_LOCK_FUNCTION(task, t_base)
#endif
-14
View File
@@ -1,14 +0,0 @@
#ifndef KERNEL_TEST_H_
#define KERNEL_TEST_H_
#ifdef __cplusplus
extern "C" {
#endif
extern int run_all_tests(void);
#ifdef __cplusplus
}
#endif
#endif
+91
View File
@@ -0,0 +1,91 @@
#ifndef KERNEL_THREAD_H_
#define KERNEL_THREAD_H_
#include <kernel/machine/thread.h>
#include <kernel/msg.h>
#include <kernel/object.h>
#include <kernel/vm-controller.h>
#define THREAD_KSTACK_ORDER VM_PAGE_4K
struct ml_cpu_context;
enum thread_state {
THREAD_READY = 1,
THREAD_SLEEPING = 2,
THREAD_STOPPED = 3,
};
enum thread_flags {
/* this thread has exhausted its quantum and is due to be re-scheduled.
*/
THREAD_F_NEED_RESCHED = 0x01u,
/* this thread is currently scheduled (i.e. is present on a runqueue) */
THREAD_F_SCHEDULED = 0x04u,
};
struct thread {
struct object tr_base;
enum thread_state tr_state;
enum thread_flags tr_flags;
struct task *tr_parent;
unsigned int tr_id;
unsigned int tr_prio;
cycles_t tr_charge_period_start;
cycles_t tr_quantum_cycles, tr_quantum_target;
cycles_t tr_total_cycles;
virt_addr_t tr_ip, tr_sp, tr_bp;
virt_addr_t tr_cpu_user_sp, tr_cpu_kernel_sp;
/* only valid within an interrupt or syscall context */
struct ml_cpu_context *tr_irqctx;
struct ml_thread tr_ml;
struct runqueue *tr_rq;
struct queue_entry tr_parent_entry;
struct queue_entry tr_rqentry;
struct vm_page *tr_kstack;
};
extern struct thread *thread_alloc(void);
extern struct thread *thread_cast(struct object *obj);
extern kern_status_t thread_init_kernel(struct thread *thr, virt_addr_t ip);
extern kern_status_t thread_init_user(
struct thread *thr,
virt_addr_t ip,
virt_addr_t sp,
const uintptr_t *args,
size_t nr_args);
extern kern_status_t thread_init_user_clone(
struct thread *thr,
const struct thread *src,
uintptr_t return_value);
extern int thread_priority(struct thread *thr);
extern void thread_awaken(struct thread *thr);
extern void thread_exit(void);
extern void thread_join(struct thread *thread, unsigned long *irq_flags);
extern void thread_kill(struct thread *thread);
extern void idle(void);
extern struct thread *create_kernel_thread(void (*fn)(void));
extern struct thread *create_idle_thread(void);
extern kern_status_t thread_config_get(
struct thread *thread,
kern_config_key_t key,
void *out,
size_t max);
extern kern_status_t thread_config_set(
struct thread *thread,
kern_config_key_t key,
const void *ptr,
size_t len);
DEFINE_OBJECT_LOCK_FUNCTION(thread, tr_base)
#endif
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef KERNEL_TYPES_H_ #ifndef KERNEL_TYPES_H_
#define KERNEL_TYPES_H_ #define KERNEL_TYPES_H_
#include <mango/types.h> #include <magenta/types.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
+2 -1
View File
@@ -1,7 +1,7 @@
#ifndef KERNEL_UTIL_H_ #ifndef KERNEL_UTIL_H_
#define KERNEL_UTIL_H_ #define KERNEL_UTIL_H_
#include <mango/types.h> #include <magenta/types.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@@ -61,6 +61,7 @@ extern uint64_t host_to_little_u64(uint64_t v);
extern uint64_t big_to_host_u64(uint64_t v); extern uint64_t big_to_host_u64(uint64_t v);
extern uint64_t little_to_host_u64(uint64_t v); extern uint64_t little_to_host_u64(uint64_t v);
extern void init_random(uint64_t seed);
extern bool fill_random(void *buffer, unsigned int size); extern bool fill_random(void *buffer, unsigned int size);
#ifdef __cplusplus #ifdef __cplusplus
+98
View File
@@ -0,0 +1,98 @@
#ifndef KERNEL_VM_CONTROLLER_H_
#define KERNEL_VM_CONTROLLER_H_
#include <kernel/locks.h>
#include <kernel/object.h>
#include <magenta/types.h>
struct thread;
struct equeue;
struct vm_object;
enum vm_request_status {
VM_REQUEST_PENDING = 0,
VM_REQUEST_IN_PROGRESS,
VM_REQUEST_COMPLETE,
VM_REQUEST_ASYNC,
};
struct vm_controller {
struct object vc_base;
/* tree of pending vm requests */
struct btree vc_requests;
struct equeue *vc_eq;
equeue_key_t vc_eq_key;
/* the number of page requests queued with status VM_REQUEST_PENDING.
* used to assert/clear VM_CONTROLLER_SIGNAL_REQUEST_RECEIVED */
size_t vc_requests_waiting;
};
struct vm_request {
uint64_t req_id;
unsigned int req_type;
enum vm_request_status req_status;
kern_status_t req_result;
spin_lock_t req_lock;
struct vm_object *req_object;
struct thread *req_sender;
/* this node is added to vm-controller vc_requests list */
struct btree_node req_node;
/* these values are used for VM_REQUEST_READ and VM_REQUEST_DIRTY */
off_t req_offset;
size_t req_length;
};
extern kern_status_t vm_controller_type_init(void);
extern struct vm_controller *vm_controller_cast(struct object *obj);
extern struct vm_controller *vm_controller_create(void);
extern kern_status_t vm_controller_recv(
struct vm_controller *ctrl,
equeue_packet_vm_request_t *out);
extern kern_status_t vm_controller_recv_async(
struct vm_controller *ctrl,
struct equeue *eq,
equeue_key_t key);
extern kern_status_t vm_controller_create_object(
struct vm_controller *ctrl,
const char *name,
size_t name_len,
equeue_key_t key,
size_t data_len,
vm_prot_t prot,
struct vm_object **out);
extern kern_status_t vm_controller_prepare_attach(
struct vm_controller *ctrl,
uint64_t req_id,
struct vm_object **out_vmo);
extern kern_status_t vm_controller_finish_attach(
struct vm_controller *ctrl,
uint64_t req_id,
equeue_key_t new_key);
extern kern_status_t vm_controller_detach_object(
struct vm_controller *ctrl,
struct vm_object *vmo);
extern kern_status_t vm_controller_supply_pages(
struct vm_controller *ctrl,
struct vm_object *dst,
off_t dst_offset,
struct vm_object *src,
off_t src_offset,
size_t count);
extern void vm_controller_fulfill_requests(
struct vm_controller *ctrl,
equeue_key_t object,
off_t offset,
size_t length,
kern_status_t result);
extern kern_status_t vm_controller_send_request(
struct vm_controller *ctrl,
struct vm_request *req,
unsigned long *irq_flags);
DEFINE_OBJECT_LOCK_FUNCTION(vm_controller, vc_base)
#endif
+66 -7
View File
@@ -6,10 +6,39 @@
#define VM_OBJECT_NAME_MAX 64 #define VM_OBJECT_NAME_MAX 64
struct vm_controller;
enum vm_object_flags { enum vm_object_flags {
/* the memory behind this vm-object wasn't allocated by us, and /* the memory behind this vm-object wasn't allocated by us, and
* therefore shouldn't be freed by us */ * therefore shouldn't be freed by us */
VMO_IN_PLACE = 0x01u, VMO_IN_PLACE = 0x01u,
/* this vm-object is/was attached to a vm-controller */
VMO_CONTROLLER = 0x02u,
/* when a vm-object is attached to a controller, and the ref-count of
* the object falls to one (i.e. the last reference is the handle to
* the object held by the server that created it), the object will
* be detached, allowing the server to close the last handle to the
* object and dispose of it. */
VMO_AUTO_DETACH = 0x04u,
/* this vmo is a duplicate of a vmo that is attached to a vm-controller.
* the duplicate vmo is scheduled to be attached to the same controller,
* but this won't actually happen until the controller is needed to
* fulfill a page request. once the duplicate vmo has been attached to
* the controller, this flag will be cleared. */
VMO_LAZY_ATTACH = 0x08u,
/* these flags are for use with vm_object_get_page */
/**************************************************/
/* if the relevant page hasn't been allocated yet, it will be allocated
* and returned. if this flag isn't specified, NULL will be returned. */
VMO_ALLOCATE_MISSING_PAGE = 0x0100u,
/* if the vm-object is attached to a vm-controller, and the relevant
* page is uncommitted, send a request to the vm-controller to provide
* the missing page. will result in the vm-object being unlocked and
* the current thread sleeping until the request is fulfilled. the
* vm-object will be re-locked before the function returns. */
VMO_REQUEST_MISSING_PAGE = 0x0200u,
}; };
struct vm_object { struct vm_object {
@@ -21,8 +50,12 @@ struct vm_object {
/* queue of struct vm_region_mapping */ /* queue of struct vm_region_mapping */
struct queue vo_mappings; struct queue vo_mappings;
/* memory protection flags. mappings of this vm_object can only use struct vm_controller *vo_ctrl;
* a subset of the flags set in this mask. */ equeue_key_t vo_key;
struct btree_node vo_ctrl_node;
/* memory protection flags. mappings of this vm_object can only
* use a subset of the flags set in this mask. */
vm_prot_t vo_prot; vm_prot_t vo_prot;
/* btree of vm_pages that have been allocated to this vm_object. /* btree of vm_pages that have been allocated to this vm_object.
@@ -57,14 +90,33 @@ extern struct vm_object *vm_object_create_in_place(
size_t data_len, size_t data_len,
vm_prot_t prot); vm_prot_t prot);
extern struct vm_page *vm_object_get_page( /* create a copy-on-write duplicate of a vm-object */
const struct vm_object *vo, extern struct vm_object *vm_object_duplicate_cow(struct vm_object *vmo);
off_t offset); /* attach a copy-on-write duplicate of a vm-object to the vm-controller that
* controlled the original vm-object */
extern kern_status_t vm_object_attach_cow(
struct vm_object *vmo,
unsigned long *irq_flags);
extern struct vm_page *vm_object_alloc_page( /* prefetch any missing pages in the specified range of a vm-object.
* any lazy-allocated pages will be allocated.
* any missing pages will be requested from the vm-controller, if one is
* attached. */
extern kern_status_t vm_object_prefetch(
struct vm_object *vo, struct vm_object *vo,
off_t offset, off_t offset,
enum vm_page_order size); size_t length,
unsigned long *irq_flags);
extern struct vm_page *vm_object_get_page(
struct vm_object *vo,
off_t offset,
enum vm_object_flags flags,
unsigned long *irq_flags);
extern kern_status_t vm_object_put_page(
struct vm_object *vo,
off_t offset,
struct vm_page *pg);
extern kern_status_t vm_object_read( extern kern_status_t vm_object_read(
struct vm_object *vo, struct vm_object *vo,
@@ -85,6 +137,13 @@ extern kern_status_t vm_object_copy(
off_t src_offset, off_t src_offset,
size_t count, size_t count,
size_t *nr_copied); size_t *nr_copied);
extern kern_status_t vm_object_transfer(
struct vm_object *dst,
off_t dst_offset,
struct vm_object *src,
off_t src_offset,
size_t count,
size_t *nr_moved);
DEFINE_OBJECT_LOCK_FUNCTION(vm_object, vo_base) DEFINE_OBJECT_LOCK_FUNCTION(vm_object, vo_base)
-182
View File
@@ -1,182 +0,0 @@
#ifndef KERNEL_VM_REGION_H_
#define KERNEL_VM_REGION_H_
#include <kernel/object.h>
#include <kernel/pmap.h>
#include <kernel/vm.h>
#define VM_REGION_NAME_MAX 64
#define VM_REGION_COPY_ALL ((size_t) - 1)
struct vm_region;
struct vm_object;
enum vm_region_status {
VM_REGION_DEAD = 0,
VM_REGION_ONLINE,
};
enum vm_region_entry_type {
VM_REGION_ENTRY_NONE = 0,
VM_REGION_ENTRY_REGION,
VM_REGION_ENTRY_MAPPING,
};
struct vm_region_entry {
union {
struct btree_node e_node;
/* this entry is only used to queue vm-region objects for
* recursive cleanup */
struct queue_entry e_entry;
};
struct vm_region_entry *e_parent;
enum vm_region_entry_type e_type;
/* absolute address of this entry */
virt_addr_t e_address;
/* offset in bytes of this entry within its immediate parent. */
off_t e_offset;
/* size of the entry in bytes */
size_t e_size;
};
struct vm_region_mapping {
struct vm_region_entry m_entry;
struct vm_object *m_object;
/* used to link to vm_object->vo_mappings */
struct queue_entry m_object_entry;
vm_prot_t m_prot;
/* offset in bytes to the start of the object data that was mapped */
off_t m_object_offset;
};
struct vm_region {
struct object vr_base;
enum vm_region_status vr_status;
struct vm_region_entry vr_entry;
char vr_name[VM_REGION_NAME_MAX];
/* btree of struct vm_region_entry.
* sibling entries cannot overlap each other, and child entries must
* be entirely contained within their immediate parent entry. */
struct btree vr_entries;
/* memory protection restriction mask.
* any mapping in this region, or any of its children, cannot use
* protection flags that are not set in this mask.
* for example, if VM_PROT_EXEC is /not/ set here, no mapping
* can be created in this region or any child region with VM_PROT_EXEC
* set. */
vm_prot_t vr_prot;
/* the physical address space in which mappings in this region (and
* its children) are created */
pmap_t vr_pmap;
};
extern kern_status_t vm_region_type_init(void);
extern struct vm_region *vm_region_cast(struct object *obj);
/* create a new vm-region, optionally within a parent region.
* `offset` is the byte offset within the parent region where the new region
* should start.
* if no parent is specified, `offset` is the absolute virtual address of the
* start of the region.
* in both cases, `len` is the length of the new region in bytes. */
extern kern_status_t vm_region_create(
struct vm_region *parent,
const char *name,
size_t name_len,
off_t offset,
size_t region_len,
vm_prot_t prot,
struct vm_region **out);
/* recursively kills a given region and all of its sub-regions.
* when a region is killed, all of its mappings are unmapped, and any further
* operations on the region are denied. however, all handles and references to
* the region (any any sub-region) remain valid, and no kernel memory is
* de-allocated.
* the memory used by the vm-region object itself is de-allocated when the last
* handle/reference to the object is released.
* this function should be called with `region` locked.
*/
extern kern_status_t vm_region_kill(
struct vm_region *region,
unsigned long *lock_flags);
/* map a vm-object into a vm-region.
* [region_offset,length] must fall within exactly one region, and cannot span
* multiple sibling regions.
* if [region_offset,length] falls within a child region, the map operation
* will be transparently redirected to the relevant region.
* `prot` must be allowed both by the region into which the mapping is being
* created AND the vm-object being mapped. */
extern kern_status_t vm_region_map_object(
struct vm_region *region,
off_t region_offset,
struct vm_object *object,
off_t object_offset,
size_t length,
vm_prot_t prot,
virt_addr_t *out);
extern kern_status_t vm_region_unmap(
struct vm_region *region,
off_t region_offset,
size_t length);
extern bool vm_region_validate_access(
struct vm_region *region,
off_t offset,
size_t len,
vm_prot_t prot);
/* find the mapping corresponding to the given virtual address, and page-in the
* necessary vm_page to allow the memory access to succeed. if the relevant
* vm-object page hasn't been allocated yet, it will be allocated here. */
extern kern_status_t vm_region_demand_map(
struct vm_region *region,
virt_addr_t addr,
enum pmap_fault_flags flags);
/* get the absolute base virtual address of a region within its
* parent/ancestors. */
extern virt_addr_t vm_region_get_base_address(const struct vm_region *region);
extern void vm_region_dump(struct vm_region *region);
/* read data from the user-space area of a vm-region into a kernel-mode buffer
*/
extern kern_status_t vm_region_read_kernel(
struct vm_region *src_region,
virt_addr_t src_ptr,
size_t count,
void *dest,
size_t *nr_read);
extern kern_status_t vm_region_memmove(
struct vm_region *dest_region,
virt_addr_t dest_ptr,
struct vm_region *src_region,
virt_addr_t src_ptr,
size_t count,
size_t *nr_moved);
extern kern_status_t vm_region_memmove_v(
struct vm_region *dest_region,
size_t dest_offset,
const struct iovec *dest,
size_t nr_dest,
struct vm_region *src_region,
size_t src_offset,
const struct iovec *src,
size_t nr_src,
size_t bytes_to_move,
size_t *nr_bytes_moved);
DEFINE_OBJECT_LOCK_FUNCTION(vm_region, vr_base)
#endif
+5 -16
View File
@@ -1,21 +1,20 @@
#ifndef KERNEL_VM_H_ #ifndef KERNEL_VM_H_
#define KERNEL_VM_H_ #define KERNEL_VM_H_
#include <kernel/atomic.h>
#include <kernel/bitmap.h> #include <kernel/bitmap.h>
#include <kernel/btree.h> #include <kernel/btree.h>
#include <kernel/locks.h> #include <kernel/locks.h>
#include <kernel/machine/vm.h> #include <kernel/machine/vm.h>
#include <kernel/queue.h> #include <kernel/queue.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <mango/status.h> #include <magenta/status.h>
#include <stddef.h> #include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct bcache;
/* maximum number of NUMA nodes */ /* maximum number of NUMA nodes */
#define VM_MAX_NODES 64 #define VM_MAX_NODES 64
/* maximum number of memory zones per node */ /* maximum number of memory zones per node */
@@ -25,11 +24,6 @@ struct bcache;
/* maximum number of sparse memory sectors */ /* maximum number of sparse memory sectors */
#define VM_MAX_SECTORS 8192 #define VM_MAX_SECTORS 8192
/* maximum number of disk sectors that can be stored in a single
page. AKA the number of bits in the sector bitmap.
used by the block cache */
#define VM_MAX_SECTORS_PER_PAGE 32
#define VM_CHECK_ALIGN(p, mask) ((((p) & (mask)) == (p)) ? 1 : 0) #define VM_CHECK_ALIGN(p, mask) ((((p) & (mask)) == (p)) ? 1 : 0)
#define VM_CACHE_INITIALISED(c) ((c)->c_obj_count != 0) #define VM_CACHE_INITIALISED(c) ((c)->c_obj_count != 0)
@@ -207,7 +201,6 @@ struct vm_page {
/* owner-specific pointer */ /* owner-specific pointer */
union { union {
struct vm_slab *p_slab; struct vm_slab *p_slab;
struct bcache *p_bcache;
void *p_priv0; void *p_priv0;
}; };
@@ -218,8 +211,6 @@ struct vm_page {
lists. lists.
- vm_object uses it to maintain a btree of allocated pages keyed - vm_object uses it to maintain a btree of allocated pages keyed
by offset/size. by offset/size.
- the block cache uses this to maintain a tree of pages keyed by
block number.
*/ */
union { union {
struct queue_entry p_list; struct queue_entry p_list;
@@ -231,15 +222,13 @@ struct vm_page {
}; };
union { union {
/* used by bcache when sector size is < page size. bitmap of /* how many vm_areas reference this vm_page. if >0, the page is
* present/missing sectors */ * subject to copy-on-write. */
DECLARE_BITMAP(p_blockbits, VM_MAX_SECTORS_PER_PAGE); atomic_t p_cow_ref;
uint32_t p_priv2; uint32_t p_priv2;
}; };
union { union {
/* sector address, used by bcache */
sectors_t p_blockid;
/* offset of this page within the vm_object it is a part of */ /* offset of this page within the vm_object it is a part of */
off_t p_vmo_offset; off_t p_vmo_offset;
+54
View File
@@ -0,0 +1,54 @@
#ifndef KERNEL_WAIT_H_
#define KERNEL_WAIT_H_
#include <kernel/locks.h>
#include <kernel/queue.h>
#define wait_event(wq, cond) \
({ \
struct thread *self = current_thread(); \
struct wait_item waiter; \
wait_item_init(&waiter, self); \
for (;;) { \
thread_wait_begin(&waiter, wq); \
if (cond) { \
break; \
} \
schedule(SCHED_NORMAL); \
} \
thread_wait_end(&waiter, wq); \
})
struct wait_item {
struct thread *w_thread;
struct queue_entry w_entry;
};
struct waitqueue {
struct queue wq_waiters;
spin_lock_t wq_lock;
};
extern void wait_item_init(struct wait_item *item, struct thread *thr);
extern void thread_wait_begin(struct wait_item *waiter, struct waitqueue *q);
extern void thread_wait_end(struct wait_item *waiter, struct waitqueue *q);
extern void thread_wait_begin_nosleep(
struct wait_item *waiter,
struct waitqueue *q);
extern void thread_wait_end_nosleep(
struct wait_item *waiter,
struct waitqueue *q);
extern void wait_on_queue(struct waitqueue *q);
extern void wakeup_queue(struct waitqueue *q);
extern void wakeup_n(struct waitqueue *q, size_t n);
extern void wakeup_one(struct waitqueue *q);
static inline bool waitqueue_empty(struct waitqueue *wq)
{
unsigned long flags;
spin_lock_irqsave(&wq->wq_lock, &flags);
bool result = queue_empty(&wq->wq_waiters);
spin_unlock_irqrestore(&wq->wq_lock, flags);
return result;
}
#endif
+37
View File
@@ -0,0 +1,37 @@
#ifndef KERNEL_WORK_H_
#define KERNEL_WORK_H_
#include <kernel/locks.h>
#include <kernel/queue.h>
#include <stddef.h>
struct work_item;
typedef void (*work_func_t)(struct work_item *);
struct work_item {
void *w_data;
work_func_t w_func;
struct queue_entry w_head;
};
struct worker_pool {
struct thread **wp_workers;
size_t wp_nworkers;
};
struct workqueue {
spin_lock_t wq_lock;
struct queue wq_queue; /* list of struct work_item */
};
extern void work_item_init(work_func_t func, void *data, struct work_item *out);
extern void workqueue_init(struct workqueue *wq);
extern struct worker_pool *worker_pool_create(size_t nworkers);
extern struct worker_pool *global_worker_pool(void);
extern bool schedule_work_on(struct workqueue *wq, struct work_item *work);
extern bool schedule_work(struct work_item *work);
extern void wake_workers(struct workqueue *wq, struct worker_pool *pool);
#endif
+12 -7
View File
@@ -5,7 +5,6 @@
#include <kernel/cpu.h> #include <kernel/cpu.h>
#include <kernel/handle.h> #include <kernel/handle.h>
#include <kernel/init.h> #include <kernel/init.h>
#include <kernel/input.h>
#include <kernel/libc/stdio.h> #include <kernel/libc/stdio.h>
#include <kernel/machine/init.h> #include <kernel/machine/init.h>
#include <kernel/object.h> #include <kernel/object.h>
@@ -13,7 +12,8 @@
#include <kernel/port.h> #include <kernel/port.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/test.h> #include <kernel/task.h>
#include <kernel/thread.h>
#include <kernel/vm-object.h> #include <kernel/vm-object.h>
#include <stdint.h> #include <stdint.h>
@@ -23,7 +23,7 @@ extern char __pstart[], __pend[];
void print_kernel_banner(void) void print_kernel_banner(void)
{ {
printk("Mango kernel version " BUILD_ID); printk("Magenta kernel version " BUILD_ID);
} }
static void hang(void) static void hang(void)
@@ -44,8 +44,8 @@ static void hang(void)
void background_thread(void) void background_thread(void)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
struct thread *thread = current_thread(); struct thread *thread = get_current_thread();
printk("background_thread() running on processor %u", this_cpu()); printk("background_thread() running on processor %u", this_cpu());
milli_sleep(1000); milli_sleep(1000);
@@ -76,6 +76,7 @@ void kernel_init(uintptr_t arg)
port_type_init(); port_type_init();
channel_type_init(); channel_type_init();
msg_init();
struct boot_module bsp_image = {0}; struct boot_module bsp_image = {0};
bsp_get_location(&bsp_image); bsp_get_location(&bsp_image);
@@ -107,10 +108,14 @@ void kernel_init(uintptr_t arg)
bsp.bsp_trailer.bsp_exec_entry, bsp.bsp_trailer.bsp_exec_entry,
bsp.bsp_vmo); bsp.bsp_vmo);
struct task *bootstrap_task = task_create("bootstrap", 9); struct task *bootstrap_task
= task_create(NULL, TASK_F_DEFAULT, "bootstrap", 9);
tracek("created bootstrap task (pid=%u)", bootstrap_task->t_id); tracek("created bootstrap task (pid=%u)", bootstrap_task->t_id);
bsp_launch_async(&bsp, bootstrap_task); status = bsp_launch_async(&bsp, bootstrap_task);
if (status != KERN_OK) {
printk("bsp launch failed with status %d", status);
}
hang(); hang();
} }
+2 -2
View File
@@ -1,6 +1,7 @@
#include <kernel/arg.h> #include <kernel/arg.h>
#include <kernel/libc/string.h>
#include <kernel/libc/ctype.h> #include <kernel/libc/ctype.h>
#include <kernel/libc/string.h>
#include <magenta/status.h>
static char g_cmdline[CMDLINE_MAX + 1] = {0}; static char g_cmdline[CMDLINE_MAX + 1] = {0};
@@ -81,7 +82,6 @@ static char *advance_to_next_arg(char *s, char *max)
return s; return s;
} }
const char *arg_value(const char *arg_name) const char *arg_value(const char *arg_name)
{ {
char *s = g_cmdline; char *s = g_cmdline;
+20 -36
View File
@@ -1,10 +1,12 @@
#include <kernel/address-space.h>
#include <kernel/bsp.h> #include <kernel/bsp.h>
#include <kernel/handle.h> #include <kernel/handle.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/task.h>
#include <kernel/thread.h>
#include <kernel/util.h> #include <kernel/util.h>
#include <kernel/vm-object.h> #include <kernel/vm-object.h>
#include <kernel/vm-region.h>
#define BOOTSTRAP_STACK_SIZE 0x10000 #define BOOTSTRAP_STACK_SIZE 0x10000
@@ -69,34 +71,12 @@ kern_status_t bsp_load(struct bsp *bsp, const struct boot_module *mod)
return KERN_OK; return KERN_OK;
} }
static kern_status_t map_executable( static kern_status_t map_executable_exec(
struct bsp *bsp, struct bsp *bsp,
struct task *task, struct task *task,
virt_addr_t *entry) virt_addr_t *entry)
{ {
kern_status_t status = KERN_OK; kern_status_t status = KERN_OK;
size_t exec_size = 0;
if (bsp->bsp_trailer.bsp_text_vaddr > bsp->bsp_trailer.bsp_data_vaddr) {
exec_size = bsp->bsp_trailer.bsp_text_vaddr
+ bsp->bsp_trailer.bsp_text_size;
} else {
exec_size = bsp->bsp_trailer.bsp_data_vaddr
+ bsp->bsp_trailer.bsp_data_size;
}
struct vm_region *region;
status = vm_region_create(
task->t_address_space,
"exec",
4,
VM_REGION_ANY_OFFSET,
exec_size,
VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXEC | VM_PROT_USER,
&region);
if (status != KERN_OK) {
return status;
}
struct vm_object *data = vm_object_create( struct vm_object *data = vm_object_create(
".data", ".data",
5, 5,
@@ -134,24 +114,26 @@ static kern_status_t map_executable(
text_voffset, text_voffset,
data_voffset); data_voffset);
status = vm_region_map_object( status = address_space_map(
region, task->t_address_space,
text_voffset, text_voffset,
bsp->bsp_vmo, bsp->bsp_vmo,
text_foffset, text_foffset,
bsp->bsp_trailer.bsp_text_size, bsp->bsp_trailer.bsp_text_size,
VM_SHARED,
VM_PROT_READ | VM_PROT_EXEC | VM_PROT_USER, VM_PROT_READ | VM_PROT_EXEC | VM_PROT_USER,
&text_base); &text_base);
if (status != KERN_OK) { if (status != KERN_OK) {
return status; return status;
} }
status = vm_region_map_object( status = address_space_map(
region, task->t_address_space,
data_voffset, data_voffset,
data, data,
data_foffset, data_foffset,
bsp->bsp_trailer.bsp_data_size, bsp->bsp_trailer.bsp_data_size,
VM_PRIVATE,
VM_PROT_READ | VM_PROT_WRITE | VM_PROT_USER, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_USER,
&data_base); &data_base);
if (status != KERN_OK) { if (status != KERN_OK) {
@@ -160,7 +142,7 @@ static kern_status_t map_executable(
tracek("text_base=%08llx, data_base=%08llx", text_base, data_base); tracek("text_base=%08llx, data_base=%08llx", text_base, data_base);
*entry = text_base + bsp->bsp_trailer.bsp_exec_entry; *entry = bsp->bsp_trailer.bsp_exec_entry;
return KERN_OK; return KERN_OK;
} }
@@ -179,12 +161,13 @@ kern_status_t bsp_launch_async(struct bsp *bsp, struct task *task)
return KERN_NO_ENTRY; return KERN_NO_ENTRY;
} }
status = vm_region_map_object( status = address_space_map(
task->t_address_space, task->t_address_space,
VM_REGION_ANY_OFFSET, MAP_ADDRESS_ANY,
user_stack, user_stack,
0, 0,
BOOTSTRAP_STACK_SIZE, BOOTSTRAP_STACK_SIZE,
VM_PRIVATE,
VM_PROT_READ | VM_PROT_WRITE | VM_PROT_USER, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_USER,
&stack_buffer); &stack_buffer);
@@ -192,12 +175,13 @@ kern_status_t bsp_launch_async(struct bsp *bsp, struct task *task)
return status; return status;
} }
status = vm_region_map_object( status = address_space_map(
task->t_address_space, task->t_address_space,
VM_REGION_ANY_OFFSET, MAP_ADDRESS_ANY,
bsp->bsp_vmo, bsp->bsp_vmo,
0, 0,
bsp->bsp_trailer.bsp_exec_offset, bsp->bsp_trailer.bsp_exec_offset,
VM_PRIVATE,
VM_PROT_READ | VM_PROT_USER, VM_PROT_READ | VM_PROT_USER,
&bsp_data_base); &bsp_data_base);
@@ -205,12 +189,12 @@ kern_status_t bsp_launch_async(struct bsp *bsp, struct task *task)
return status; return status;
} }
status = map_executable(bsp, task, &entry); status = map_executable_exec(bsp, task, &entry);
if (status != KERN_OK) { if (status != KERN_OK) {
return status; return status;
} }
#ifdef TRACE #ifdef TRACE
vm_region_dump(task->t_address_space); address_space_dump(task->t_address_space);
#endif #endif
sp = stack_buffer + BOOTSTRAP_STACK_SIZE; sp = stack_buffer + BOOTSTRAP_STACK_SIZE;
@@ -220,7 +204,7 @@ kern_status_t bsp_launch_async(struct bsp *bsp, struct task *task)
task_open_handle(task, &task->t_base, 0, &self); task_open_handle(task, &task->t_base, 0, &self);
task_open_handle( task_open_handle(
task, task,
&task->t_address_space->vr_base, &task->t_address_space->s_base,
0, 0,
&self_address_space); &self_address_space);
+171 -60
View File
@@ -1,7 +1,12 @@
#include <kernel/address-space.h>
#include <kernel/channel.h> #include <kernel/channel.h>
#include <kernel/msg.h> #include <kernel/msg.h>
#include <kernel/port.h>
#include <kernel/printk.h>
#include <kernel/task.h>
#include <kernel/thread.h>
#include <kernel/util.h> #include <kernel/util.h>
#include <kernel/vm-region.h> #include <magenta/signal.h>
#define CHANNEL_CAST(p) OBJECT_C_CAST(struct channel, c_base, &channel_type, p) #define CHANNEL_CAST(p) OBJECT_C_CAST(struct channel, c_base, &channel_type, p)
@@ -11,7 +16,7 @@ static struct object_type channel_type = {
.ob_header_offset = offsetof(struct channel, c_base), .ob_header_offset = offsetof(struct channel, c_base),
}; };
BTREE_DEFINE_SIMPLE_GET(struct kmsg, msgid_t, msg_node, msg_id, get_msg_with_id) BTREE_DEFINE_SIMPLE_GET(struct msg, msgid_t, msg_node, msg_id, get_msg_with_id)
kern_status_t channel_type_init(void) kern_status_t channel_type_init(void)
{ {
@@ -35,7 +40,7 @@ extern struct channel *channel_create(void)
return channel; return channel;
} }
static bool try_enqueue(struct btree *tree, struct kmsg *msg) static bool try_enqueue(struct btree *tree, struct msg *msg)
{ {
if (!tree->b_root) { if (!tree->b_root) {
tree->b_root = &msg->msg_node; tree->b_root = &msg->msg_node;
@@ -45,8 +50,8 @@ static bool try_enqueue(struct btree *tree, struct kmsg *msg)
struct btree_node *cur = tree->b_root; struct btree_node *cur = tree->b_root;
while (1) { while (1) {
struct kmsg *cur_node struct msg *cur_node
= BTREE_CONTAINER(struct kmsg, msg_node, cur); = BTREE_CONTAINER(struct msg, msg_node, cur);
struct btree_node *next = NULL; struct btree_node *next = NULL;
if (msg->msg_id > cur_node->msg_id) { if (msg->msg_id > cur_node->msg_id) {
@@ -75,27 +80,36 @@ static bool try_enqueue(struct btree *tree, struct kmsg *msg)
} }
static void kmsg_reply_error( static void kmsg_reply_error(
struct kmsg *msg, struct msg *msg,
kern_status_t status, kern_status_t status,
unsigned long *lock_flags) unsigned long *lock_flags)
{ {
msg->msg_status = KMSG_REPLY_SENT; msg->msg_status = KMSG_REPLY_SENT;
msg->msg_sender_port->p_status = PORT_READY;
msg->msg_result = status; msg->msg_result = status;
thread_awaken(msg->msg_sender_thread); thread_awaken(msg->msg_sender_thread);
spin_unlock_irqrestore(&msg->msg_lock, *lock_flags); spin_unlock_irqrestore(&msg->msg_lock, *lock_flags);
} }
static struct kmsg *get_next_msg( static struct msg *get_next_msg(
struct channel *channel, struct channel *channel,
unsigned long *lock_flags) unsigned long *lock_flags)
{ {
struct btree_node *cur = btree_first(&channel->c_msg); struct btree_node *cur = btree_first(&channel->c_msg);
while (cur) { while (cur) {
struct kmsg *msg = BTREE_CONTAINER(struct kmsg, msg_node, cur); struct msg *msg = BTREE_CONTAINER(struct msg, msg_node, cur);
spin_lock_irqsave(&msg->msg_lock, lock_flags); spin_lock_irqsave(&msg->msg_lock, lock_flags);
if (msg->msg_status == KMSG_WAIT_RECEIVE) { switch (msg->msg_status) {
case KMSG_WAIT_RECEIVE:
msg->msg_status = KMSG_WAIT_REPLY; msg->msg_status = KMSG_WAIT_REPLY;
channel->c_msg_waiting--;
return msg; return msg;
case KMSG_ASYNC:
btree_delete(&channel->c_msg, &msg->msg_node);
channel->c_msg_waiting--;
return msg;
default:
break;
} }
spin_unlock_irqrestore(&msg->msg_lock, *lock_flags); spin_unlock_irqrestore(&msg->msg_lock, *lock_flags);
@@ -107,77 +121,112 @@ static struct kmsg *get_next_msg(
extern kern_status_t channel_enqueue_msg( extern kern_status_t channel_enqueue_msg(
struct channel *channel, struct channel *channel,
struct kmsg *msg) struct msg *msg)
{ {
fill_random(&msg->msg_id, sizeof msg->msg_id); fill_random(&msg->msg_id, sizeof msg->msg_id);
while (!try_enqueue(&channel->c_msg, msg)) { while (!try_enqueue(&channel->c_msg, msg)) {
msg->msg_id++; msg->msg_id++;
} }
wakeup_one(&channel->c_wq); channel->c_msg_waiting++;
object_assert_signal(&channel->c_base, CHANNEL_SIGNAL_MSG_RECEIVED);
return KERN_OK; return KERN_OK;
} }
extern kern_status_t channel_recv_msg( extern kern_status_t channel_recv_msg(
struct channel *channel, struct channel *channel,
struct msg *out_msg, kern_msg_t *out_msg,
msgid_t *out_id,
unsigned long *irq_flags) unsigned long *irq_flags)
{ {
struct wait_item waiter; struct msg *msg = NULL;
struct thread *self = current_thread();
struct kmsg *msg = NULL;
unsigned long msg_lock_flags; unsigned long msg_lock_flags;
wait_item_init(&waiter, self);
for (;;) {
thread_wait_begin(&waiter, &channel->c_wq);
msg = get_next_msg(channel, &msg_lock_flags); msg = get_next_msg(channel, &msg_lock_flags);
if (msg) { if (!msg) {
break; return KERN_NO_ENTRY;
} }
object_unlock_irqrestore(&channel->c_base, *irq_flags); if (channel->c_msg_waiting == 0) {
schedule(SCHED_NORMAL); object_clear_signal(
object_lock_irqsave(&channel->c_base, irq_flags); &channel->c_base,
CHANNEL_SIGNAL_MSG_RECEIVED);
} }
thread_wait_end(&waiter, &channel->c_wq);
/* msg is now set to the next message to process */ /* msg is now set to the next message to process */
struct task *sender = msg->msg_sender_thread->tr_parent; if (msg->msg_type != KERN_MSG_TYPE_DATA) {
struct task *receiver = self->tr_parent; /* event messages are asynchronous */
out_msg->msg_id = msg->msg_id;
out_msg->msg_type = msg->msg_type;
out_msg->msg_event = msg->msg_event;
out_msg->msg_sender = msg->msg_sender_thread_id;
out_msg->msg_endpoint = msg->msg_sender_port_id;
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
msg_free(msg);
kern_status_t status = vm_region_memmove_v( return KERN_OK;
receiver->t_address_space, }
struct task *sender = msg->msg_sender_thread->tr_parent;
struct task *receiver = get_current_task();
struct address_space *src = sender->t_address_space,
*dst = receiver->t_address_space;
unsigned long f;
address_space_lock_pair_irqsave(src, dst, &f);
kern_status_t status = address_space_memmove_v(
dst,
0, 0,
out_msg->msg_data, out_msg->msg_data,
out_msg->msg_data_count, out_msg->msg_data_count,
sender->t_address_space, src,
0, 0,
msg->msg_req.msg_data, msg->msg_req.msg_data,
msg->msg_req.msg_data_count, msg->msg_req.msg_data_count,
VM_REGION_COPY_ALL, ADDRESS_SPACE_COPY_ALL,
NULL); NULL);
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
put_current_task(receiver);
return status; return status;
} }
status = handle_list_transfer( struct handle_table *src_table = sender->t_handles,
receiver->t_handles, *dst_table = receiver->t_handles;
spin_lock_pair_irqsave(
&sender->t_handles_lock,
&receiver->t_handles_lock,
&f);
status = handle_table_transfer(
dst,
dst_table,
out_msg->msg_handles, out_msg->msg_handles,
out_msg->msg_handles_count, out_msg->msg_handles_count,
sender->t_handles, src,
src_table,
msg->msg_req.msg_handles, msg->msg_req.msg_handles,
msg->msg_req.msg_handles_count); msg->msg_req.msg_handles_count);
spin_unlock_pair_irqrestore(
&sender->t_handles_lock,
&receiver->t_handles_lock,
f);
address_space_unlock_pair_irqrestore(src, dst, f);
put_current_task(receiver);
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
return status; return status;
} }
*out_id = msg->msg_id; out_msg->msg_id = msg->msg_id;
out_msg->msg_type = msg->msg_type;
out_msg->msg_sender = msg->msg_sender_thread->tr_parent->t_id;
out_msg->msg_endpoint = msg->msg_sender_port->p_base.ob_id;
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags); spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
@@ -187,11 +236,11 @@ extern kern_status_t channel_recv_msg(
extern kern_status_t channel_reply_msg( extern kern_status_t channel_reply_msg(
struct channel *channel, struct channel *channel,
msgid_t id, msgid_t id,
const struct msg *resp, const kern_msg_t *reply,
unsigned long *irq_flags) unsigned long *irq_flags)
{ {
unsigned long msg_lock_flags; unsigned long msg_lock_flags;
struct kmsg *msg = get_msg_with_id(&channel->c_msg, id); struct msg *msg = get_msg_with_id(&channel->c_msg, id);
if (!msg) { if (!msg) {
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -202,35 +251,57 @@ extern kern_status_t channel_reply_msg(
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
struct thread *self = current_thread();
/* the task that is about to receive the response */ /* the task that is about to receive the response */
struct task *receiver = msg->msg_sender_thread->tr_parent; struct task *receiver = msg->msg_sender_thread->tr_parent;
/* the task that is about to send the response */ /* the task that is about to send the response */
struct task *sender = self->tr_parent; struct task *sender = get_current_task();
kern_status_t status = vm_region_memmove_v( struct address_space *src = sender->t_address_space,
receiver->t_address_space, *dst = receiver->t_address_space;
unsigned long f;
address_space_lock_pair_irqsave(src, dst, &f);
kern_status_t status = address_space_memmove_v(
dst,
0, 0,
msg->msg_resp.msg_data, msg->msg_resp.msg_data,
msg->msg_resp.msg_data_count, msg->msg_resp.msg_data_count,
sender->t_address_space, src,
0, 0,
resp->msg_data, reply->msg_data,
resp->msg_data_count, reply->msg_data_count,
VM_REGION_COPY_ALL, ADDRESS_SPACE_COPY_ALL,
NULL); NULL);
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
put_current_task(sender);
return status; return status;
} }
status = handle_list_transfer( struct handle_table *src_table = sender->t_handles,
receiver->t_handles, *dst_table = receiver->t_handles;
spin_lock_pair_irqsave(
&sender->t_handles_lock,
&receiver->t_handles_lock,
&f);
status = handle_table_transfer(
dst,
dst_table,
msg->msg_resp.msg_handles, msg->msg_resp.msg_handles,
msg->msg_resp.msg_handles_count, msg->msg_resp.msg_handles_count,
sender->t_handles, src,
resp->msg_handles, src_table,
resp->msg_handles_count); reply->msg_handles,
reply->msg_handles_count);
spin_unlock_pair_irqrestore(
&sender->t_handles_lock,
&receiver->t_handles_lock,
f);
address_space_unlock_pair_irqrestore(src, dst, f);
put_current_task(sender);
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
return status; return status;
@@ -245,13 +316,13 @@ extern kern_status_t channel_read_msg(
struct channel *channel, struct channel *channel,
msgid_t id, msgid_t id,
size_t offset, size_t offset,
struct vm_region *dest_region, struct address_space *dest_region,
const struct iovec *dest_iov, const kern_iovec_t *dest_iov,
size_t dest_iov_count, size_t dest_iov_count,
size_t *nr_read) size_t *nr_read)
{ {
unsigned long msg_lock_flags; unsigned long msg_lock_flags;
struct kmsg *msg = get_msg_with_id(&channel->c_msg, id); struct msg *msg = get_msg_with_id(&channel->c_msg, id);
if (!msg) { if (!msg) {
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -262,17 +333,24 @@ extern kern_status_t channel_read_msg(
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
kern_status_t status = vm_region_memmove_v( struct address_space *src_region
= msg->msg_sender_thread->tr_parent->t_address_space;
unsigned long f;
address_space_lock_pair_irqsave(src_region, dest_region, &f);
kern_status_t status = address_space_memmove_v(
dest_region, dest_region,
0, 0,
dest_iov, dest_iov,
dest_iov_count, dest_iov_count,
msg->msg_sender_thread->tr_parent->t_address_space, src_region,
offset, offset,
msg->msg_req.msg_data, msg->msg_req.msg_data,
msg->msg_req.msg_data_count, msg->msg_req.msg_data_count,
VM_REGION_COPY_ALL, ADDRESS_SPACE_COPY_ALL,
nr_read); nr_read);
address_space_unlock_pair_irqrestore(src_region, dest_region, f);
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags); spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
@@ -281,12 +359,45 @@ extern kern_status_t channel_read_msg(
extern kern_status_t channel_write_msg( extern kern_status_t channel_write_msg(
struct channel *channel, struct channel *channel,
msgid_t msg, msgid_t id,
size_t offset, size_t offset,
struct vm_region *src_region, struct address_space *src_region,
const struct iovec *src_iov, const kern_iovec_t *src_iov,
size_t src_iov_count, size_t src_iov_count,
size_t *nr_written) size_t *nr_written)
{ {
return KERN_UNIMPLEMENTED; unsigned long msg_lock_flags;
struct msg *msg = get_msg_with_id(&channel->c_msg, id);
if (!msg) {
return KERN_INVALID_ARGUMENT;
}
spin_lock_irqsave(&msg->msg_lock, &msg_lock_flags);
if (msg->msg_status != KMSG_WAIT_REPLY) {
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
return KERN_INVALID_ARGUMENT;
}
struct address_space *dest_region
= msg->msg_sender_thread->tr_parent->t_address_space;
unsigned long f;
address_space_lock_pair_irqsave(src_region, dest_region, &f);
kern_status_t status = address_space_memmove_v(
dest_region,
offset,
msg->msg_resp.msg_data,
msg->msg_resp.msg_data_count,
src_region,
0,
src_iov,
src_iov_count,
ADDRESS_SPACE_COPY_ALL,
nr_written);
address_space_unlock_pair_irqrestore(src_region, dest_region, f);
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
return status;
} }
+7 -5
View File
@@ -1,9 +1,9 @@
#include <kernel/console.h> #include <kernel/console.h>
#include <kernel/queue.h>
#include <kernel/locks.h>
#include <kernel/libc/string.h> #include <kernel/libc/string.h>
#include <kernel/locks.h>
#include <kernel/queue.h>
static struct queue consoles; static struct queue consoles = {0};
static spin_lock_t consoles_lock = SPIN_LOCK_INIT; static spin_lock_t consoles_lock = SPIN_LOCK_INIT;
static void unregister_boot_consoles(void) static void unregister_boot_consoles(void)
@@ -11,7 +11,8 @@ static void unregister_boot_consoles(void)
struct queue_entry *cur = queue_first(&consoles); struct queue_entry *cur = queue_first(&consoles);
while (cur) { while (cur) {
struct queue_entry *next = cur->qe_next; struct queue_entry *next = cur->qe_next;
struct console *con = QUEUE_CONTAINER(struct console, c_list, cur); struct console *con
= QUEUE_CONTAINER(struct console, c_list, cur);
if (con->c_flags & CON_BOOT) { if (con->c_flags & CON_BOOT) {
queue_delete(&consoles, cur); queue_delete(&consoles, cur);
} }
@@ -25,7 +26,8 @@ kern_status_t console_register(struct console *con)
unsigned long flags; unsigned long flags;
spin_lock_irqsave(&consoles_lock, &flags); spin_lock_irqsave(&consoles_lock, &flags);
queue_foreach (struct console, cur, &consoles, c_list) { queue_foreach(struct console, cur, &consoles, c_list)
{
if (!strcmp(cur->c_name, con->c_name)) { if (!strcmp(cur->c_name, con->c_name)) {
spin_unlock_irqrestore(&consoles_lock, flags); spin_unlock_irqrestore(&consoles_lock, flags);
return KERN_NAME_EXISTS; return KERN_NAME_EXISTS;
+29
View File
@@ -0,0 +1,29 @@
#include <kernel/equeue.h>
kern_status_t equeue_type_init(void)
{
return KERN_UNIMPLEMENTED;
}
struct equeue *equeue_cast(struct object *obj)
{
return NULL;
}
struct equeue *equeue_create(void)
{
return NULL;
}
kern_status_t equeue_enqueue(
struct equeue *q,
const equeue_packet_t *pkt,
enum equeue_flags flags)
{
return KERN_UNIMPLEMENTED;
}
kern_status_t equeue_dequeue(struct equeue *q, equeue_packet_t *out)
{
return KERN_UNIMPLEMENTED;
}
+231
View File
@@ -0,0 +1,231 @@
#include <kernel/address-space.h>
#include <kernel/futex.h>
#include <kernel/sched.h>
#include <kernel/task.h>
#include <magenta/status.h>
#define FUTEX_CREATE 0x40u
static struct btree shared_futex_list = {0};
static spin_lock_t shared_futex_list_lock = SPIN_LOCK_INIT;
static struct vm_cache futex_cache = {
.c_name = "futex",
.c_obj_size = sizeof(struct futex),
};
BTREE_DEFINE_SIMPLE_INSERT(struct futex, f_node, f_key, put_futex)
BTREE_DEFINE_SIMPLE_GET(struct futex, uintptr_t, f_node, f_key, get_futex)
kern_status_t futex_init(void)
{
vm_cache_init(&futex_cache);
return KERN_OK;
}
static kern_status_t get_data(
futex_key_t key,
unsigned int flags,
struct futex **out,
spin_lock_t **out_lock,
unsigned long *irq_flags)
{
spin_lock_t *lock = NULL;
struct btree *futex_list = NULL;
struct task *self = NULL;
if (flags & FUTEX_PRIVATE) {
self = get_current_task();
lock = &self->t_base.ob_lock;
futex_list = &self->t_futex;
} else if (flags & FUTEX_SHARED) {
lock = &shared_futex_list_lock;
futex_list = &shared_futex_list;
} else {
return KERN_INVALID_ARGUMENT;
}
spin_lock_irqsave(lock, irq_flags);
struct futex *futex = get_futex(futex_list, key);
if (!futex && !(flags & FUTEX_CREATE)) {
spin_unlock_irqrestore(lock, *irq_flags);
if (self) {
put_current_task(self);
}
return KERN_NO_ENTRY;
}
futex = vm_cache_alloc(&futex_cache, VM_NORMAL);
if (!futex) {
spin_unlock_irqrestore(lock, *irq_flags);
if (self) {
put_current_task(self);
}
return KERN_NO_MEMORY;
}
futex->f_key = key;
put_futex(futex_list, futex);
if (self) {
put_current_task(self);
}
*out = futex;
*out_lock = lock;
return KERN_OK;
}
static kern_status_t cleanup_data(struct futex *futex, unsigned int flags)
{
struct task *self = NULL;
struct btree *futex_list = NULL;
if (flags & FUTEX_PRIVATE) {
self = get_current_task();
futex_list = &self->t_futex;
} else if (flags & FUTEX_SHARED) {
futex_list = &shared_futex_list;
} else {
return KERN_INVALID_ARGUMENT;
}
btree_delete(futex_list, &futex->f_node);
vm_cache_free(&futex_cache, futex);
if (self) {
put_current_task(self);
}
return KERN_OK;
}
static kern_status_t futex_get_shared(kern_futex_t *futex, futex_key_t *out)
{
struct task *self = get_current_task();
struct address_space *space = self->t_address_space;
unsigned long flags;
address_space_lock_irqsave(space, &flags);
kern_status_t status = address_space_translate(
space,
(virt_addr_t)futex,
out,
&flags);
address_space_unlock_irqrestore(space, flags);
put_current_task(self);
return status;
}
static kern_status_t futex_get_private(kern_futex_t *futex, futex_key_t *out)
{
*out = (futex_key_t)futex;
return KERN_OK;
}
kern_status_t futex_get(
kern_futex_t *futex,
futex_key_t *out,
unsigned int flags)
{
if (flags & FUTEX_PRIVATE) {
return futex_get_private(futex, out);
}
if (flags & FUTEX_SHARED) {
return futex_get_shared(futex, out);
}
return KERN_INVALID_ARGUMENT;
}
static kern_status_t futex_read(
struct futex *futex,
unsigned int flags,
kern_futex_t *out)
{
if (flags & FUTEX_PRIVATE) {
virt_addr_t addr = futex->f_key;
*out = *(kern_futex_t *)addr;
return KERN_OK;
}
if (flags & FUTEX_SHARED) {
phys_addr_t paddr = futex->f_key;
virt_addr_t vaddr = (virt_addr_t)vm_phys_to_virt(paddr);
if (!vaddr) {
return KERN_MEMORY_FAULT;
}
*out = *(kern_futex_t *)vaddr;
return KERN_OK;
}
return KERN_INVALID_ARGUMENT;
}
kern_status_t futex_wait(
futex_key_t key,
kern_futex_t new_val,
unsigned int flags)
{
spin_lock_t *lock = NULL;
unsigned long irq_flags = 0;
struct futex *futex = NULL;
kern_status_t status = get_data(key, flags, &futex, &lock, &irq_flags);
if (status != KERN_OK) {
return status;
}
kern_futex_t current_val = 0;
status = futex_read(futex, flags, &current_val);
if (status != KERN_OK) {
spin_unlock_irqrestore(lock, irq_flags);
return status;
}
if (current_val != new_val) {
spin_unlock_irqrestore(lock, irq_flags);
return KERN_BAD_STATE;
}
struct wait_item waiter;
thread_wait_begin(&waiter, &futex->f_waiters);
spin_unlock_irqrestore(lock, irq_flags);
schedule(SCHED_NORMAL);
spin_lock_irqsave(lock, &irq_flags);
thread_wait_end(&waiter, &futex->f_waiters);
if (waitqueue_empty(&futex->f_waiters)) {
cleanup_data(futex, flags);
}
spin_unlock_irqrestore(lock, irq_flags);
return KERN_OK;
}
kern_status_t futex_wake(futex_key_t key, size_t nwaiters, unsigned int flags)
{
spin_lock_t *lock = NULL;
unsigned long irq_flags = 0;
struct futex *futex = NULL;
kern_status_t status = get_data(key, flags, &futex, &lock, &irq_flags);
if (status != KERN_OK) {
return status;
}
if (nwaiters == FUTEX_WAKE_ALL) {
wakeup_queue(&futex->f_waiters);
} else {
wakeup_n(&futex->f_waiters, nwaiters);
}
spin_unlock_irqrestore(lock, irq_flags);
return KERN_OK;
}
+307 -116
View File
@@ -1,16 +1,18 @@
#include <kernel/address-space.h>
#include <kernel/handle.h> #include <kernel/handle.h>
#include <kernel/libc/string.h> #include <kernel/libc/string.h>
#include <kernel/object.h> #include <kernel/object.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/util.h> #include <kernel/util.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#include <magenta/types.h>
/* depth=3 gives a maximum of ~66.6 million handles */ /* depth=3 gives a maximum of ~66.6 million handles */
#define MAX_TABLE_DEPTH 3 #define MAX_TABLE_DEPTH 3
#define RESERVED_HANDLES 64 #define RESERVED_HANDLES 64
static struct vm_cache handle_table_cache = { static struct vm_cache handle_table_cache = {
.c_name = "handle_table", .c_name = "handle-table",
.c_obj_size = sizeof(struct handle_table), .c_obj_size = sizeof(struct handle_table),
}; };
@@ -31,8 +33,123 @@ struct handle_table *handle_table_create(void)
return out; return out;
} }
static void do_handle_table_destroy_leaf(struct handle_table *tab)
{
while (1) {
unsigned int index = bitmap_lowest_set(
tab->t_handles.t_handle_map,
HANDLES_PER_TABLE);
if (index == BITMAP_NPOS) {
break;
}
struct handle *child = &tab->t_handles.t_handle_list[index];
bitmap_clear(tab->t_subtables.t_subtable_map, index);
if (child->h_object) {
object_unref(child->h_object);
child->h_object = NULL;
}
}
}
static void do_handle_table_destroy(
struct handle_table *tab,
unsigned int depth)
{
if (depth == MAX_TABLE_DEPTH - 1) {
do_handle_table_destroy_leaf(tab);
return;
}
for (size_t i = 0; i < REFS_PER_TABLE; i++) {
struct handle_table *child
= tab->t_subtables.t_subtable_list[i];
if (child) {
do_handle_table_destroy(child, depth + 1);
}
}
vm_cache_free(&handle_table_cache, tab);
}
void handle_table_destroy(struct handle_table *tab) void handle_table_destroy(struct handle_table *tab)
{ {
do_handle_table_destroy(tab, 0);
}
static kern_status_t do_handle_table_duplicate_leaf(
struct handle_table *src,
struct handle_table **dest)
{
struct handle_table *out
= vm_cache_alloc(&handle_table_cache, VM_NORMAL);
if (!out) {
return KERN_NO_MEMORY;
}
memcpy(out, src, sizeof *out);
for (size_t i = 0; i < HANDLES_PER_TABLE; i++) {
struct object *obj = src->t_handles.t_handle_list[i].h_object;
if (obj) {
object_ref(obj);
}
}
*dest = out;
return KERN_OK;
}
static kern_status_t do_handle_table_duplicate(
struct handle_table *src,
struct handle_table **dest,
unsigned int depth)
{
if (depth == MAX_TABLE_DEPTH - 1) {
return do_handle_table_duplicate_leaf(src, dest);
}
struct handle_table *out
= vm_cache_alloc(&handle_table_cache, VM_NORMAL);
if (!out) {
return KERN_NO_MEMORY;
}
memcpy(out->t_subtables.t_subtable_map,
src->t_subtables.t_subtable_map,
sizeof out->t_subtables.t_subtable_map);
memset(out->t_subtables.t_subtable_list,
0x0,
sizeof out->t_subtables.t_subtable_list);
for (size_t i = 0; i < REFS_PER_TABLE; i++) {
struct handle_table *child
= src->t_subtables.t_subtable_list[i];
struct handle_table *dup = NULL;
kern_status_t status = KERN_OK;
if (child) {
status = do_handle_table_duplicate(
child,
&dup,
depth + 1);
}
if (status == KERN_OK) {
out->t_subtables.t_subtable_list[i] = dup;
} else {
return status;
}
}
*dest = out;
return KERN_OK;
}
kern_status_t handle_table_duplicate(
struct handle_table *src,
struct handle_table **dest)
{
return do_handle_table_duplicate(src, dest, 0);
} }
static kern_status_t decode_handle_indices( static kern_status_t decode_handle_indices(
@@ -67,7 +184,7 @@ static kern_status_t encode_handle_indices(
return KERN_OK; return KERN_OK;
} }
kern_status_t handle_table_alloc_handle( static kern_status_t alloc_handle(
struct handle_table *tab, struct handle_table *tab,
struct handle **out_slot, struct handle **out_slot,
kern_handle_t *out_handle) kern_handle_t *out_handle)
@@ -122,6 +239,53 @@ kern_status_t handle_table_alloc_handle(
return encode_handle_indices(indices, out_handle); return encode_handle_indices(indices, out_handle);
} }
kern_status_t handle_table_alloc_handle(
struct handle_table *tab,
kern_handle_t value,
struct handle **out_slot,
kern_handle_t *out_handle)
{
if (value == KERN_HANDLE_INVALID) {
return alloc_handle(tab, out_slot, out_handle);
}
unsigned int indices[MAX_TABLE_DEPTH];
if (decode_handle_indices(value, indices) != KERN_OK) {
return KERN_HANDLE_INVALID;
}
int i;
for (i = 0; i < MAX_TABLE_DEPTH - 1; i++) {
struct handle_table *next
= tab->t_subtables.t_subtable_list[indices[i]];
if (!next) {
next = handle_table_create();
tab->t_subtables.t_subtable_list[indices[i]] = next;
}
if (!next) {
return KERN_NO_MEMORY;
}
tab = next;
}
unsigned int handle_index = indices[i];
bitmap_set(tab->t_handles.t_handle_map, handle_index);
struct handle *out = &tab->t_handles.t_handle_list[handle_index];
if (out->h_object) {
object_unref(out->h_object);
out->h_object = NULL;
out->h_flags = 0;
}
*out_slot = out;
*out_handle = value;
return KERN_OK;
}
kern_status_t handle_table_free_handle( kern_status_t handle_table_free_handle(
struct handle_table *tab, struct handle_table *tab,
kern_handle_t handle) kern_handle_t handle)
@@ -153,7 +317,7 @@ kern_status_t handle_table_free_handle(
= &tab->t_handles.t_handle_list[handle_index]; = &tab->t_handles.t_handle_list[handle_index];
if (handle_entry->h_object) { if (handle_entry->h_object) {
object_remove_handle(handle_entry->h_object); object_unref(handle_entry->h_object);
} }
memset(handle_entry, 0x0, sizeof *handle_entry); memset(handle_entry, 0x0, sizeof *handle_entry);
@@ -192,122 +356,149 @@ struct handle *handle_table_get_handle(
return &tab->t_handles.t_handle_list[handle_index]; return &tab->t_handles.t_handle_list[handle_index];
} }
struct handle_list_iterator { kern_status_t handle_table_transfer(
struct handle_list *it_list; struct address_space *dst_region,
size_t it_list_count; struct handle_table *dst,
size_t it_list_ptr; kern_msg_handle_t *dst_handles,
size_t dst_handles_max,
kern_handle_t *it_handles; struct address_space *src_region,
size_t it_nr_handles; struct handle_table *src,
}; kern_msg_handle_t *src_handles,
size_t src_handles_count)
static void handle_list_iterator_begin(
struct handle_list_iterator *it,
struct handle_list *list,
size_t list_count)
{ {
memset(it, 0x0, sizeof *it); kern_status_t status = KERN_OK;
it->it_list = list; size_t to_transfer = MIN(dst_handles_max, src_handles_count);
it->it_list_count = list_count;
while (it->it_list_ptr < list_count) { size_t i = 0;
if (list[it->it_list_ptr].l_nr_handles > 0) { for (size_t i = 0; i < to_transfer; i++) {
break; kern_msg_handle_t src_handle = {0}, dst_handle = {0};
} virt_addr_t src_handle_addr
= (virt_addr_t)src_handles + (i * sizeof src_handle);
virt_addr_t dst_handle_addr
= (virt_addr_t)dst_handles + (i * sizeof dst_handle);
status = address_space_read(
src_region,
src_handle_addr,
sizeof src_handle,
&src_handle,
NULL);
it->it_list_ptr++;
}
if (it->it_list_ptr >= list_count) {
return;
}
it->it_handles = list[it->it_list_ptr].l_handles;
it->it_nr_handles = list[it->it_list_ptr].l_nr_handles;
}
static void handle_list_iterator_seek(
struct handle_list_iterator *it,
size_t nr_handles)
{
if (nr_handles > it->it_nr_handles) {
nr_handles = it->it_nr_handles;
}
if (nr_handles < it->it_nr_handles) {
it->it_handles += nr_handles;
it->it_nr_handles -= nr_handles;
return;
}
it->it_list_ptr++;
while (it->it_list_ptr < it->it_list_count) {
if (it->it_list[it->it_list_ptr].l_nr_handles > 0) {
break;
}
it->it_list_ptr++;
}
if (it->it_list_ptr >= it->it_list_count) {
return;
}
it->it_handles = it->it_list[it->it_list_ptr].l_handles;
it->it_nr_handles = it->it_list[it->it_list_ptr].l_nr_handles;
}
kern_status_t handle_list_transfer(
struct handle_table *dest_table,
struct handle_list *dest_list,
size_t dest_list_count,
struct handle_table *src_table,
const struct handle_list *src_list,
size_t src_list_count)
{
struct handle_list_iterator src, dest;
handle_list_iterator_begin(
&src,
(struct handle_list *)src_list,
src_list_count);
handle_list_iterator_begin(&dest, dest_list, dest_list_count);
while (src.it_nr_handles && dest.it_nr_handles) {
size_t to_copy = MIN(src.it_nr_handles, dest.it_nr_handles);
for (size_t i = 0; i < to_copy; i++) {
kern_handle_t handle_v = src.it_handles[i];
struct handle *handle
= handle_table_get_handle(src_table, handle_v);
if (!handle) {
return KERN_HANDLE_INVALID;
}
struct object *obj = object_ref(handle->h_object);
handle_flags_t flags = handle->h_flags;
handle_table_free_handle(src_table, handle_v);
struct handle *dest_slot = NULL;
kern_status_t status = handle_table_alloc_handle(
dest_table,
&dest_slot,
&handle_v);
if (status != KERN_OK) { if (status != KERN_OK) {
src_handle.hnd_result = KERN_OK;
address_space_write(
src_region,
src_handle_addr,
sizeof src_handle,
&src_handle,
NULL);
break;
}
if (src_handle.hnd_value == KERN_HANDLE_INVALID) {
continue;
}
struct handle *src_entry
= handle_table_get_handle(src, src_handle.hnd_value);
struct handle *dst_entry = NULL;
kern_handle_t dst_value = KERN_HANDLE_INVALID;
if (!src_entry) {
status = KERN_INVALID_ARGUMENT;
src_handle.hnd_result = KERN_INVALID_ARGUMENT;
address_space_write(
src_region,
src_handle_addr,
sizeof src_handle,
&src_handle,
NULL);
break;
}
switch (src_handle.hnd_mode) {
case HANDLE_TRANSFER_IGNORE:
break;
case HANDLE_TRANSFER_MOVE:
status = handle_table_alloc_handle(
dst,
KERN_HANDLE_INVALID,
&dst_entry,
&dst_value);
if (status != KERN_OK) {
break;
}
dst_entry->h_object = src_entry->h_object;
dst_entry->h_flags = src_entry->h_flags;
object_ref(dst_entry->h_object);
handle_table_free_handle(src, src_handles[i].hnd_value);
dst_handle.hnd_mode = src_handles[i].hnd_mode;
dst_handle.hnd_value = dst_value;
dst_handle.hnd_result = KERN_OK;
break;
case HANDLE_TRANSFER_COPY:
status = handle_table_alloc_handle(
dst,
KERN_HANDLE_INVALID,
&dst_entry,
&dst_value);
if (status != KERN_OK) {
break;
}
dst_entry->h_object = src_entry->h_object;
dst_entry->h_flags = src_entry->h_flags;
object_ref(dst_entry->h_object);
dst_handle.hnd_mode = src_handles[i].hnd_mode;
dst_handle.hnd_value = dst_value;
dst_handle.hnd_result = KERN_OK;
break;
default:
status = KERN_INVALID_ARGUMENT;
break;
}
src_handle.hnd_result = status;
address_space_write(
src_region,
src_handle_addr,
sizeof src_handle,
&src_handle,
NULL);
address_space_write(
dst_region,
dst_handle_addr,
sizeof dst_handle,
&dst_handle,
NULL);
}
for (; i < src_handles_count; i++) {
kern_msg_handle_t handle = {0};
virt_addr_t handle_addr
= (virt_addr_t)src_handles + (i * sizeof handle);
address_space_read(
src_region,
handle_addr,
sizeof handle,
&handle,
NULL);
if (handle.hnd_mode != HANDLE_TRANSFER_MOVE) {
continue;
}
struct handle *src_entry
= handle_table_get_handle(src, handle.hnd_value);
if (src_entry) {
object_unref(src_entry->h_object);
handle_table_free_handle(src, handle.hnd_value);
}
}
return status; return status;
}
dest_slot->h_object = obj;
dest_slot->h_flags = flags;
object_add_handle(obj);
object_unref(obj);
dest.it_handles[i] = handle_v;
}
handle_list_iterator_seek(&src, to_copy);
handle_list_iterator_seek(&dest, to_copy);
}
return KERN_OK;
} }
+11 -11
View File
@@ -1,12 +1,12 @@
#include <kernel/address-space.h>
#include <kernel/iovec.h> #include <kernel/iovec.h>
#include <kernel/libc/string.h> #include <kernel/libc/string.h>
#include <kernel/util.h> #include <kernel/util.h>
#include <kernel/vm-region.h>
static bool read_iovec( static bool read_iovec(
struct iovec_iterator *it, struct iovec_iterator *it,
size_t index, size_t index,
struct iovec *out) kern_iovec_t *out)
{ {
if (index >= it->it_nr_vecs) { if (index >= it->it_nr_vecs) {
return false; return false;
@@ -18,20 +18,20 @@ static bool read_iovec(
} }
size_t nr_read = 0; size_t nr_read = 0;
kern_status_t status = vm_region_read_kernel( kern_status_t status = address_space_read(
it->it_region, it->it_region,
(virt_addr_t)it->it_vecs + (index * sizeof(struct iovec)), (virt_addr_t)it->it_vecs + (index * sizeof(kern_iovec_t)),
sizeof(struct iovec), sizeof(kern_iovec_t),
out, out,
&nr_read); &nr_read);
return (status == KERN_OK && nr_read != sizeof(struct iovec)); return (status == KERN_OK && nr_read != sizeof(kern_iovec_t));
} }
void iovec_iterator_begin_user( void iovec_iterator_begin_user(
struct iovec_iterator *it, struct iovec_iterator *it,
struct vm_region *region, struct address_space *region,
const struct iovec *vecs, const kern_iovec_t *vecs,
size_t nr_vecs) size_t nr_vecs)
{ {
memset(it, 0x0, sizeof *it); memset(it, 0x0, sizeof *it);
@@ -39,7 +39,7 @@ void iovec_iterator_begin_user(
it->it_vecs = vecs; it->it_vecs = vecs;
it->it_nr_vecs = nr_vecs; it->it_nr_vecs = nr_vecs;
struct iovec iov; kern_iovec_t iov;
while (it->it_vec_ptr < nr_vecs) { while (it->it_vec_ptr < nr_vecs) {
read_iovec(it, it->it_vec_ptr, &iov); read_iovec(it, it->it_vec_ptr, &iov);
@@ -60,7 +60,7 @@ void iovec_iterator_begin_user(
void iovec_iterator_begin( void iovec_iterator_begin(
struct iovec_iterator *it, struct iovec_iterator *it,
const struct iovec *vecs, const kern_iovec_t *vecs,
size_t nr_vecs) size_t nr_vecs)
{ {
memset(it, 0x0, sizeof *it); memset(it, 0x0, sizeof *it);
@@ -97,7 +97,7 @@ void iovec_iterator_seek(struct iovec_iterator *it, size_t nr_bytes)
} }
nr_bytes -= to_seek; nr_bytes -= to_seek;
struct iovec iov; kern_iovec_t iov;
it->it_vec_ptr++; it->it_vec_ptr++;
while (it->it_vec_ptr < it->it_nr_vecs) { while (it->it_vec_ptr < it->it_nr_vecs) {
+22
View File
@@ -0,0 +1,22 @@
#include <kernel/msg.h>
#include <kernel/vm.h>
static struct vm_cache msg_cache = {
.c_name = "msg",
.c_obj_size = sizeof(struct msg),
};
void msg_init(void)
{
vm_cache_init(&msg_cache);
}
struct msg *msg_alloc(void)
{
return vm_cache_alloc(&msg_cache, VM_NORMAL);
}
void msg_free(struct msg *msg)
{
vm_cache_free(&msg_cache, msg);
}
+67 -68
View File
@@ -1,6 +1,8 @@
#include <kernel/locks.h> #include <kernel/locks.h>
#include <kernel/object.h> #include <kernel/object.h>
#include <kernel/queue.h> #include <kernel/queue.h>
#include <kernel/sched.h>
#include <kernel/thread.h>
#define HAS_OP(obj, opname) ((obj)->ob_type->ob_ops.opname) #define HAS_OP(obj, opname) ((obj)->ob_type->ob_ops.opname)
@@ -72,90 +74,28 @@ struct object *object_create(struct object_type *type)
obj->ob_lock = SPIN_LOCK_INIT; obj->ob_lock = SPIN_LOCK_INIT;
obj->ob_magic = OBJECT_MAGIC; obj->ob_magic = OBJECT_MAGIC;
obj->ob_refcount = 1; obj->ob_refcount = 1;
obj->ob_handles = 0;
return obj; return obj;
} }
struct object *object_ref(struct object *obj) struct object *object_ref(struct object *obj)
{ {
obj->ob_refcount++; atomic_add_fetch(&obj->ob_refcount, 1);
return obj; return obj;
} }
static void __cleanup(struct object *obj, struct queue *queue)
{
if (HAS_OP(obj, destroy)) {
obj->ob_type->ob_ops.destroy(obj, queue);
}
vm_cache_free(&obj->ob_type->ob_cache, obj);
}
static void object_cleanup(struct object *obj, unsigned long flags)
{
if (obj->ob_refcount > 0 || obj->ob_handles > 0) {
spin_unlock_irqrestore(&obj->ob_lock, flags);
return;
}
struct queue queue = QUEUE_INIT;
__cleanup(obj, &queue);
if (!HAS_OP(obj, destroy_recurse)) {
return;
}
while (!queue_empty(&queue)) {
struct queue_entry *entry = queue_pop_front(&queue);
struct object *child = NULL;
obj->ob_type->ob_ops.destroy_recurse(entry, &child);
if (!child) {
continue;
}
if (child->ob_refcount > 1) {
child->ob_refcount--;
continue;
}
if (child->ob_refcount == 0 && child->ob_handles == 0) {
__cleanup(child, &queue);
}
}
}
void object_unref(struct object *obj) void object_unref(struct object *obj)
{ {
unsigned long flags; int ref = atomic_sub_fetch(&obj->ob_refcount, 1);
spin_lock_irqsave(&obj->ob_lock, &flags); if (ref > 0) {
if (obj->ob_refcount == 0) {
spin_unlock_irqrestore(&obj->ob_lock, flags);
return; return;
} }
obj->ob_refcount--; if (HAS_OP(obj, destroy)) {
object_cleanup(obj, flags); obj->ob_type->ob_ops.destroy(obj);
}
void object_add_handle(struct object *obj)
{
obj->ob_handles++;
}
void object_remove_handle(struct object *obj)
{
unsigned long flags;
spin_lock_irqsave(&obj->ob_lock, &flags);
if (obj->ob_handles == 0) {
spin_unlock_irqrestore(&obj->ob_lock, flags);
return;
} }
obj->ob_handles--; vm_cache_free(&obj->ob_type->ob_cache, obj);
object_cleanup(obj, flags);
} }
void object_lock(struct object *obj) void object_lock(struct object *obj)
@@ -178,6 +118,32 @@ void object_unlock_irqrestore(struct object *obj, unsigned long flags)
spin_unlock_irqrestore(&obj->ob_lock, flags); spin_unlock_irqrestore(&obj->ob_lock, flags);
} }
void object_lock_pair(struct object *a, struct object *b)
{
spin_lock_pair(&a->ob_lock, &b->ob_lock);
}
void object_unlock_pair(struct object *a, struct object *b)
{
spin_unlock_pair(&a->ob_lock, &b->ob_lock);
}
void object_lock_pair_irqsave(
struct object *a,
struct object *b,
unsigned long *flags)
{
spin_lock_pair_irqsave(&a->ob_lock, &b->ob_lock, flags);
}
void object_unlock_pair_irqrestore(
struct object *a,
struct object *b,
unsigned long flags)
{
spin_unlock_pair_irqrestore(&a->ob_lock, &b->ob_lock, flags);
}
void *object_data(struct object *obj) void *object_data(struct object *obj)
{ {
return (char *)obj + sizeof *obj; return (char *)obj + sizeof *obj;
@@ -192,3 +158,36 @@ struct object *object_header(void *p)
return obj; return obj;
} }
void object_assert_signal(struct object *obj, uint32_t signals)
{
obj->ob_signals |= signals;
wakeup_queue(&obj->ob_wq);
}
void object_clear_signal(struct object *obj, uint32_t signals)
{
obj->ob_signals &= ~signals;
}
void object_wait_signal(
struct object *obj,
uint32_t signals,
unsigned long *irq_flags)
{
struct thread *self = get_current_thread();
struct wait_item waiter;
wait_item_init(&waiter, self);
for (;;) {
thread_wait_begin(&waiter, &obj->ob_wq);
if (obj->ob_signals & signals) {
break;
}
object_unlock_irqrestore(obj, *irq_flags);
schedule(SCHED_NORMAL);
object_lock_irqsave(obj, irq_flags);
}
thread_wait_end(&waiter, &obj->ob_wq);
put_current_thread(self);
}
+7 -3
View File
@@ -2,7 +2,8 @@
#include <kernel/libc/stdio.h> #include <kernel/libc/stdio.h>
#include <kernel/machine/panic.h> #include <kernel/machine/panic.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h> #include <kernel/task.h>
#include <kernel/thread.h>
#include <stdarg.h> #include <stdarg.h>
static int has_panicked = 0; static int has_panicked = 0;
@@ -18,8 +19,8 @@ void panic_irq(struct ml_cpu_context *ctx, const char *fmt, ...)
printk("---[ kernel panic: %s", buf); printk("---[ kernel panic: %s", buf);
printk("kernel: " BUILD_ID ", compiler version: " __VERSION__); printk("kernel: " BUILD_ID ", compiler version: " __VERSION__);
struct task *task = current_task(); struct task *task = get_current_task();
struct thread *thr = current_thread(); struct thread *thr = get_current_thread();
if (task && thr) { if (task && thr) {
printk("task: %s (id: %d, thread: %d)", printk("task: %s (id: %d, thread: %d)",
@@ -30,6 +31,9 @@ void panic_irq(struct ml_cpu_context *ctx, const char *fmt, ...)
printk("task: [bootstrap]"); printk("task: [bootstrap]");
} }
put_current_thread(thr);
put_current_task(task);
printk("cpu: %u", this_cpu()); printk("cpu: %u", this_cpu());
ml_print_cpu_state(ctx); ml_print_cpu_state(ctx);
+78 -16
View File
@@ -1,15 +1,29 @@
#include <kernel/channel.h> #include <kernel/channel.h>
#include <kernel/port.h> #include <kernel/port.h>
#include <kernel/printk.h>
#include <kernel/thread.h>
#include <kernel/util.h> #include <kernel/util.h>
#define PORT_CAST(p) OBJECT_C_CAST(struct port, p_base, &port_type, p) #define PORT_CAST(p) OBJECT_C_CAST(struct port, p_base, &port_type, p)
static kern_status_t port_cleanup(struct object *obj);
static struct object_type port_type = { static struct object_type port_type = {
.ob_name = "port", .ob_name = "port",
.ob_size = sizeof(struct port), .ob_size = sizeof(struct port),
.ob_header_offset = offsetof(struct port, p_base), .ob_header_offset = offsetof(struct port, p_base),
.ob_ops = {
.destroy = port_cleanup,
},
}; };
static kern_status_t port_cleanup(struct object *obj)
{
struct port *port = PORT_CAST(obj);
port_disconnect(port);
return KERN_OK;
}
kern_status_t port_type_init(void) kern_status_t port_type_init(void)
{ {
return object_type_register(&port_type); return object_type_register(&port_type);
@@ -20,10 +34,10 @@ struct port *port_cast(struct object *obj)
return PORT_CAST(obj); return PORT_CAST(obj);
} }
static void wait_for_reply(struct kmsg *msg, unsigned long *lock_flags) static void wait_for_reply(struct msg *msg, unsigned long *lock_flags)
{ {
struct wait_item waiter; struct wait_item waiter;
struct thread *self = current_thread(); struct thread *self = get_current_thread();
wait_item_init(&waiter, self); wait_item_init(&waiter, self);
for (;;) { for (;;) {
@@ -38,6 +52,7 @@ static void wait_for_reply(struct kmsg *msg, unsigned long *lock_flags)
} }
self->tr_state = THREAD_READY; self->tr_state = THREAD_READY;
put_current_thread(self);
} }
struct port *port_create(void) struct port *port_create(void)
@@ -57,9 +72,29 @@ struct port *port_create(void)
kern_status_t port_connect(struct port *port, struct channel *remote) kern_status_t port_connect(struct port *port, struct channel *remote)
{ {
if (port->p_status != PORT_OFFLINE) { if (port->p_status != PORT_OFFLINE) {
tracek("port_connect: port in bad state (%d)", port->p_status);
return KERN_BAD_STATE; return KERN_BAD_STATE;
} }
struct msg *msg = msg_alloc();
if (!msg) {
return KERN_NO_MEMORY;
}
msg->msg_status = KMSG_ASYNC;
msg->msg_type = KERN_MSG_TYPE_EVENT;
msg->msg_event = KERN_MSG_EVENT_CONNECTION;
msg->msg_sender_port_id = port->p_base.ob_id;
struct thread *self = get_current_thread();
msg->msg_sender_thread_id = self->tr_id;
put_current_thread(self);
unsigned long flags;
channel_lock_irqsave(remote, &flags);
channel_enqueue_msg(remote, msg);
channel_unlock_irqrestore(remote, flags);
port->p_remote = remote; port->p_remote = remote;
port->p_status = PORT_READY; port->p_status = PORT_READY;
return KERN_OK; return KERN_OK;
@@ -68,9 +103,30 @@ kern_status_t port_connect(struct port *port, struct channel *remote)
kern_status_t port_disconnect(struct port *port) kern_status_t port_disconnect(struct port *port)
{ {
if (port->p_status != PORT_READY) { if (port->p_status != PORT_READY) {
tracek("port_disconnect: port in bad state (%d)",
port->p_status);
return KERN_BAD_STATE; return KERN_BAD_STATE;
} }
struct msg *msg = msg_alloc();
if (!msg) {
return KERN_NO_MEMORY;
}
msg->msg_status = KMSG_ASYNC;
msg->msg_type = KERN_MSG_TYPE_EVENT;
msg->msg_event = KERN_MSG_EVENT_DISCONNECTION;
msg->msg_sender_port_id = port->p_base.ob_id;
struct thread *self = get_current_thread();
msg->msg_sender_thread_id = self->tr_id;
put_current_thread(self);
unsigned long flags;
channel_lock_irqsave(port->p_remote, &flags);
channel_enqueue_msg(port->p_remote, msg);
channel_unlock_irqrestore(port->p_remote, flags);
port->p_remote = NULL; port->p_remote = NULL;
port->p_status = PORT_OFFLINE; port->p_status = PORT_OFFLINE;
return KERN_OK; return KERN_OK;
@@ -78,30 +134,36 @@ kern_status_t port_disconnect(struct port *port)
kern_status_t port_send_msg( kern_status_t port_send_msg(
struct port *port, struct port *port,
const struct msg *req, const kern_msg_t *in_msg,
struct msg *resp, kern_msg_t *out_reply,
unsigned long *lock_flags) unsigned long *lock_flags)
{ {
if (port->p_status != PORT_READY) { if (port->p_status != PORT_READY) {
tracek("port_send_msg: port in bad state (%d)", port->p_status);
return KERN_BAD_STATE; return KERN_BAD_STATE;
} }
struct thread *self = current_thread(); struct thread *self = get_current_thread();
struct kmsg *msg = &self->tr_msg; struct msg msg;
memset(msg, 0x0, sizeof *msg); memset(&msg, 0x0, sizeof msg);
msg->msg_status = KMSG_WAIT_RECEIVE; msg.msg_type = KERN_MSG_TYPE_DATA;
msg->msg_sender_thread = self; msg.msg_status = KMSG_WAIT_RECEIVE;
msg->msg_sender_port = port; msg.msg_sender_thread = self;
msg->msg_req = *req; msg.msg_sender_port = port;
msg->msg_resp = *resp; memcpy(&msg.msg_req, in_msg, sizeof msg.msg_req);
memcpy(&msg.msg_resp, out_reply, sizeof msg.msg_req);
unsigned long flags; unsigned long flags;
channel_lock_irqsave(port->p_remote, &flags); channel_lock_irqsave(port->p_remote, &flags);
port->p_status = PORT_SEND_BLOCKED; channel_enqueue_msg(port->p_remote, &msg);
channel_enqueue_msg(port->p_remote, msg);
channel_unlock_irqrestore(port->p_remote, flags); channel_unlock_irqrestore(port->p_remote, flags);
wait_for_reply(msg, lock_flags); wait_for_reply(&msg, lock_flags);
return msg->msg_result; channel_lock_irqsave(port->p_remote, &flags);
btree_delete(&port->p_remote->c_msg, &msg.msg_node);
channel_unlock_irqrestore(port->p_remote, flags);
put_current_thread(self);
return msg.msg_result;
} }
+25 -8
View File
@@ -1,7 +1,7 @@
#include <kernel/printk.h>
#include <kernel/locks.h>
#include <kernel/console.h> #include <kernel/console.h>
#include <kernel/libc/stdio.h> #include <kernel/libc/stdio.h>
#include <kernel/locks.h>
#include <kernel/printk.h>
#include <stdarg.h> #include <stdarg.h>
#define LOG_BUFFER_SIZE 0x40000 #define LOG_BUFFER_SIZE 0x40000
@@ -26,13 +26,18 @@ static void flush_log_buffer(void)
return; return;
} }
console_write(early_console, log_buffer + log_buffer_readp, log_buffer_writep - log_buffer_readp); console_write(early_console, log_buffer + log_buffer_readp,
log_buffer_writep - log_buffer_readp);
*/ */
unsigned long flags; unsigned long flags;
struct queue *consoles = get_consoles(&flags); struct queue *consoles = get_consoles(&flags);
queue_foreach(struct console, con, consoles, c_list) { queue_foreach(struct console, con, consoles, c_list)
console_write(con, log_buffer + log_buffer_readp, log_buffer_writep - log_buffer_readp); {
console_write(
con,
log_buffer + log_buffer_readp,
log_buffer_writep - log_buffer_readp);
} }
put_consoles(consoles, flags); put_consoles(consoles, flags);
@@ -61,6 +66,18 @@ void early_printk_init(struct console *con)
early_console = con; early_console = con;
} }
static void print_msg_direct(const char *s, size_t len)
{
unsigned long flags;
struct queue *consoles = get_consoles(&flags);
queue_foreach(struct console, con, consoles, c_list)
{
console_write(con, s, len);
}
put_consoles(consoles, flags);
}
int printk(const char *format, ...) int printk(const char *format, ...)
{ {
char msg[LOG_MSG_SIZE]; char msg[LOG_MSG_SIZE];
@@ -74,15 +91,15 @@ int printk(const char *format, ...)
msg[len + 1] = '\0'; msg[len + 1] = '\0';
if (log_buffer_writep == LOG_BUFFER_SIZE - 1) { if (log_buffer_writep == LOG_BUFFER_SIZE - 1) {
console_write(early_console, msg, len + 1); print_msg_direct(msg, len + 1);
return 0;
} }
unsigned long flags; unsigned long flags;
spin_lock_irqsave(&log_buffer_lock, &flags); spin_lock_irqsave(&log_buffer_lock, &flags);
save_log_message(msg); save_log_message(msg);
spin_unlock_irqrestore(&log_buffer_lock, flags);
flush_log_buffer(); flush_log_buffer();
spin_unlock_irqrestore(&log_buffer_lock, flags);
return 0; return 0;
} }
+2 -1
View File
@@ -1,4 +1,5 @@
#include <mango/status.h> #include <magenta/status.h>
#include <magenta/types.h>
#define ERROR_STRING_CASE(code) \ #define ERROR_STRING_CASE(code) \
case code: \ case code: \
+4 -3
View File
@@ -2,7 +2,8 @@
int memcmp(const void *vl, const void *vr, size_t n) int memcmp(const void *vl, const void *vr, size_t n)
{ {
const unsigned char *l=vl, *r=vr; const unsigned char *l = vl, *r = vr;
for (; n && *l == *r; n--, l++, r++); for (; n && *l == *r; n--, l++, r++)
return n ? *l-*r : 0; ;
return n ? *l - *r : 0;
} }
+1 -1
View File
@@ -1,5 +1,5 @@
#include <stdint.h>
#include <kernel/libc/string.h> #include <kernel/libc/string.h>
#include <stdint.h>
static void *memcpy_r(void *dest, const void *src, size_t sz) static void *memcpy_r(void *dest, const void *src, size_t sz)
{ {
@@ -1,13 +1,12 @@
file(GLOB headers ${CMAKE_CURRENT_SOURCE_DIR}/include/mango/*.h) file(GLOB headers ${CMAKE_CURRENT_SOURCE_DIR}/include/magenta/*.h)
file(GLOB asm_sources file(GLOB asm_sources
${CMAKE_CURRENT_SOURCE_DIR}/arch/${CMAKE_SYSTEM_PROCESSOR}/*.S) ${CMAKE_CURRENT_SOURCE_DIR}/arch/${CMAKE_SYSTEM_PROCESSOR}/*.S)
set_property(SOURCE ${asm_sources} PROPERTY LANGUAGE C)
set(public_include_dirs set(public_include_dirs
${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include-user) ${CMAKE_CURRENT_SOURCE_DIR}/include-user)
add_library(libmango STATIC ${asm_sources}) add_library(libmagenta STATIC ${asm_sources})
target_include_directories(libmango PUBLIC target_include_directories(libmagenta PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include-user) ${CMAKE_CURRENT_SOURCE_DIR}/include-user)
+115
View File
@@ -0,0 +1,115 @@
#include "magenta/syscall.h"
# Registers:
# rax = syscall ID + return value
# rdi = 1st parameter
# rsi = 2nd parameter
# rdx = 3rd parameter
# r12 = 4th parameter
# r8 = 5th parameter
# r9 = 6th parameter
# r13 = 7th parameter
# r14 = 8th parameter
.macro SYSCALL_GATE name index args
.global \name
.type \name, @function
\name:
push %rbp
mov %rsp, %rbp
# Syscall ID
mov $\index, %rax
# The syscall instruction uses rcx, so move this parameter to r12
.if \args >= 4
pushq %r12
movq %rcx, %r12
.endif
# r13 and r14 are callee-saved so we save it beforehand
.if \args >= 7
pushq %r13
movq 16(%rbp), %r13
.endif
.if \args >= 8
pushq %r14
movq 24(%rbp), %r14
.endif
syscall
.if \args >= 8
popq %r14
.endif
.if \args >= 7
popq %r13
.endif
.if \args >= 4
popq %r12
.endif
pop %rbp
ret
.endm
SYSCALL_GATE task_exit SYS_TASK_EXIT 1
SYSCALL_GATE task_self SYS_TASK_SELF 1
SYSCALL_GATE task_create SYS_TASK_CREATE 6
SYSCALL_GATE task_duplicate SYS_TASK_DUPLICATE 2
SYSCALL_GATE task_create_thread SYS_TASK_CREATE_THREAD 6
SYSCALL_GATE task_get_address_space SYS_TASK_GET_ADDRESS_SPACE 1
SYSCALL_GATE task_config_get SYS_TASK_CONFIG_GET 4
SYSCALL_GATE task_config_set SYS_TASK_CONFIG_SET 4
SYSCALL_GATE thread_self SYS_THREAD_SELF 1
SYSCALL_GATE thread_start SYS_THREAD_START 1
SYSCALL_GATE thread_exit SYS_THREAD_EXIT 0
SYSCALL_GATE thread_config_get SYS_THREAD_CONFIG_GET 4
SYSCALL_GATE thread_config_set SYS_THREAD_CONFIG_SET 4
SYSCALL_GATE vm_object_create SYS_VM_OBJECT_CREATE 5
SYSCALL_GATE vm_object_read SYS_VM_OBJECT_READ 5
SYSCALL_GATE vm_object_write SYS_VM_OBJECT_WRITE 5
SYSCALL_GATE vm_object_copy SYS_VM_OBJECT_COPY 6
SYSCALL_GATE address_space_read SYS_ADDRESS_SPACE_READ 5
SYSCALL_GATE address_space_write SYS_ADDRESS_SPACE_WRITE 5
SYSCALL_GATE address_space_map SYS_ADDRESS_SPACE_MAP 8
SYSCALL_GATE address_space_unmap SYS_ADDRESS_SPACE_UNMAP 3
SYSCALL_GATE address_space_reserve SYS_ADDRESS_SPACE_RESERVE 4
SYSCALL_GATE address_space_release SYS_ADDRESS_SPACE_RELEASE 3
SYSCALL_GATE kern_log SYS_KERN_LOG 1
SYSCALL_GATE kern_handle_close SYS_KERN_HANDLE_CLOSE 1
SYSCALL_GATE kern_handle_transfer SYS_KERN_HANDLE_TRANSFER 6
SYSCALL_GATE kern_config_get SYS_KERN_CONFIG_GET 3
SYSCALL_GATE kern_config_set SYS_KERN_CONFIG_SET 3
SYSCALL_GATE channel_create SYS_CHANNEL_CREATE 2
SYSCALL_GATE port_create SYS_PORT_CREATE 1
SYSCALL_GATE port_connect SYS_PORT_CONNECT 3
SYSCALL_GATE port_disconnect SYS_PORT_DISCONNECT 1
SYSCALL_GATE msg_send SYS_MSG_SEND 5
SYSCALL_GATE msg_recv SYS_MSG_RECV 4
SYSCALL_GATE msg_reply SYS_MSG_REPLY 4
SYSCALL_GATE msg_read SYS_MSG_READ 6
SYSCALL_GATE msg_write SYS_MSG_WRITE 6
SYSCALL_GATE vm_controller_create SYS_VM_CONTROLLER_CREATE 1
SYSCALL_GATE vm_controller_recv SYS_VM_CONTROLLER_RECV 2
SYSCALL_GATE vm_controller_recv_async SYS_VM_CONTROLLER_RECV_ASYNC 3
SYSCALL_GATE vm_controller_create_object SYS_VM_CONTROLLER_CREATE_OBJECT 7
SYSCALL_GATE vm_controller_prepare_attach SYS_VM_CONTROLLER_PREPARE_ATTACH 3
SYSCALL_GATE vm_controller_finish_attach SYS_VM_CONTROLLER_FINISH_ATTACH 3
SYSCALL_GATE vm_controller_detach_object SYS_VM_CONTROLLER_DETACH_OBJECT 2
SYSCALL_GATE vm_controller_supply_pages SYS_VM_CONTROLLER_SUPPLY_PAGES 6
SYSCALL_GATE kern_object_wait SYS_KERN_OBJECT_WAIT 2
SYSCALL_GATE kern_object_query SYS_KERN_OBJECT_QUERY 2
SYSCALL_GATE futex_wait SYS_FUTEX_WAIT 3
SYSCALL_GATE futex_wake SYS_FUTEX_WAKE 3
@@ -1,8 +1,8 @@
#ifndef MANGO_CONFIG_H_ #ifndef MAGENTA_CONFIG_H_
#define MANGO_CONFIG_H_ #define MAGENTA_CONFIG_H_
#include <mango/status.h> #include <magenta/status.h>
#include <mango/types.h> #include <magenta/types.h>
#include <stddef.h> #include <stddef.h>
extern kern_status_t kern_config_get( extern kern_status_t kern_config_get(
+9
View File
@@ -0,0 +1,9 @@
#ifndef MAGENTA_EQUEUE_H_
#define MAGENTA_EQUEUE_H_
#include <magenta/types.h>
extern kern_status_t equeue_create(kern_handle_t *out);
extern kern_status_t equeue_dequeue(kern_handle_t eq, equeue_packet_t *out);
#endif
+16
View File
@@ -0,0 +1,16 @@
#ifndef MAGENTA_FUTEX_H_
#define MAGENTA_FUTEX_H_
#include <magenta/status.h>
#include <magenta/types.h>
extern kern_status_t futex_wait(
kern_futex_t *futex,
kern_futex_t new_val,
unsigned int flags);
extern kern_status_t futex_wake(
kern_futex_t *futex,
unsigned int nr_waiters,
unsigned int flags);
#endif
+22
View File
@@ -0,0 +1,22 @@
#ifndef MAGENTA_HANDLE_H_
#define MAGENTA_HANDLE_H_
#include <magenta/status.h>
#include <magenta/types.h>
extern kern_status_t kern_handle_close(kern_handle_t handle);
extern kern_status_t kern_handle_transfer(
kern_handle_t src_task,
kern_handle_t src_handle,
kern_handle_t dest_task,
kern_handle_t dest_handle,
unsigned int mode,
kern_handle_t *out_handle);
extern kern_status_t kern_handle_control(
kern_handle_t task,
kern_handle_t handle,
uint32_t set_mask,
uint32_t clear_mask,
uint32_t *out_flags);
#endif
@@ -1,7 +1,8 @@
#ifndef MANGO_LOG_H_ #ifndef MAGENTA_LOG_H_
#define MANGO_LOG_H_ #define MAGENTA_LOG_H_
#include <mango/status.h> #include <magenta/status.h>
#include <magenta/types.h>
#undef TRACE #undef TRACE
@@ -9,18 +10,18 @@ extern kern_status_t kern_log(const char *s);
#define kern_logf(...) \ #define kern_logf(...) \
do { \ do { \
char s[128]; \ char __logbuf[128]; \
snprintf(s, sizeof s, __VA_ARGS__); \ snprintf(__logbuf, sizeof __logbuf, __VA_ARGS__); \
kern_log(s); \ kern_log(__logbuf); \
} while (0) } while (0)
#ifdef TRACE #ifdef TRACE
#define kern_trace(...) kern_log(__VA_ARGS__) #define kern_trace(...) kern_log(__VA_ARGS__)
#define kern_tracef(...) \ #define kern_tracef(...) \
do { \ do { \
char s[128]; \ char __logbuf[128]; \
snprintf(s, sizeof s, __VA_ARGS__); \ snprintf(__logbuf, sizeof __logbuf, __VA_ARGS__); \
kern_log(s); \ kern_log(__logbuf); \
} while (0) } while (0)
#else #else
#define kern_trace(...) #define kern_trace(...)
+43
View File
@@ -0,0 +1,43 @@
#ifndef MAGENTA_MSG_H_
#define MAGENTA_MSG_H_
#include <magenta/status.h>
#include <magenta/types.h>
extern kern_status_t channel_create(unsigned int id, kern_handle_t *out);
extern kern_status_t port_create(kern_handle_t *out);
extern kern_status_t port_connect(
kern_handle_t port,
tid_t task_id,
unsigned int channel_id);
extern kern_status_t port_disconnect(kern_handle_t port);
extern kern_status_t msg_send(
kern_handle_t port,
const kern_msg_t *msg,
kern_msg_t *out_response);
extern kern_status_t msg_recv(kern_handle_t channel, kern_msg_t *out);
extern kern_status_t msg_reply(
kern_handle_t channel,
msgid_t id,
const kern_msg_t *response);
extern kern_status_t msg_read(
kern_handle_t channel,
msgid_t id,
size_t offset,
kern_iovec_t *out,
size_t out_count,
size_t *nr_read);
extern kern_status_t msg_write(
kern_handle_t channel,
msgid_t id,
size_t offset,
const kern_iovec_t *in,
size_t nr_in,
size_t *nr_written);
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef MAGENTA_OBJECT_H_
#define MAGENTA_OBJECT_H_
#include <magenta/types.h>
extern kern_status_t kern_object_wait(kern_wait_item_t *items, size_t nr_items);
extern kern_status_t kern_object_query(
kern_handle_t handle,
kern_object_info_t *out);
#endif
+62
View File
@@ -0,0 +1,62 @@
#ifndef MAGENTA_TASK_H_
#define MAGENTA_TASK_H_
#include <magenta/status.h>
#include <magenta/types.h>
extern kern_status_t task_exit(int status);
extern kern_status_t task_self(kern_handle_t *out);
extern kern_status_t task_create(
kern_handle_t parent,
task_flags_t flags,
const char *name,
size_t name_len,
kern_handle_t *out_task,
kern_handle_t *out_address_space);
extern kern_status_t task_create_thread(
kern_handle_t task,
virt_addr_t ip,
virt_addr_t sp,
uintptr_t *args,
size_t nr_args,
kern_handle_t *out_thread);
extern kern_status_t task_get_address_space(
kern_handle_t task,
kern_handle_t *out);
extern kern_status_t task_config_get(
kern_handle_t task,
kern_config_key_t key,
void *ptr,
size_t len);
extern kern_status_t task_config_set(
kern_handle_t task,
kern_config_key_t key,
const void *ptr,
size_t len);
extern kern_status_t task_duplicate(
kern_handle_t *out_task,
kern_handle_t *out_address_space);
extern kern_status_t task_reset(
virt_addr_t ip,
virt_addr_t sp,
uintptr_t *args,
size_t nr_args,
virt_addr_t unmap_base,
size_t unmap_length);
extern kern_status_t thread_self(kern_handle_t *out);
extern kern_status_t thread_start(kern_handle_t thread);
extern kern_status_t thread_exit(void);
extern kern_status_t thread_config_get(
kern_handle_t thread,
kern_config_key_t key,
void *ptr,
size_t len);
extern kern_status_t thread_config_set(
kern_handle_t thread,
kern_config_key_t key,
const void *ptr,
size_t len);
#endif
+103
View File
@@ -0,0 +1,103 @@
#ifndef MAGENTA_VM_H_
#define MAGENTA_VM_H_
#include <magenta/status.h>
#include <magenta/types.h>
extern kern_status_t vm_object_create(
const char *name,
size_t name_len,
size_t data_len,
vm_prot_t prot,
kern_handle_t *out);
extern kern_status_t vm_object_read(
kern_handle_t object,
void *dst,
off_t offset,
size_t count,
size_t *nr_read);
extern kern_status_t vm_object_write(
kern_handle_t object,
const void *src,
off_t offset,
size_t count,
size_t *nr_written);
extern kern_status_t vm_object_copy(
kern_handle_t dst,
off_t dst_offset,
kern_handle_t src,
off_t src_offset,
size_t count,
size_t *nr_copied);
extern kern_status_t address_space_read(
kern_handle_t region,
void *dst,
virt_addr_t base,
size_t count,
size_t *nr_read);
extern kern_status_t address_space_write(
kern_handle_t region,
const void *src,
virt_addr_t base,
size_t count,
size_t *nr_read);
extern kern_status_t address_space_map(
kern_handle_t region,
virt_addr_t map_address,
kern_handle_t object,
off_t object_offset,
size_t length,
vm_flags_t flags,
vm_prot_t prot,
virt_addr_t *out_base_address);
extern kern_status_t address_space_unmap(
kern_handle_t region,
virt_addr_t base,
size_t length);
extern kern_status_t address_space_reserve(
kern_handle_t region,
virt_addr_t base,
size_t length,
virt_addr_t *out_base_address);
extern kern_status_t address_space_release(
kern_handle_t region,
virt_addr_t base,
size_t length);
extern kern_status_t vm_controller_create(kern_handle_t *out);
extern kern_status_t vm_controller_recv(
kern_handle_t ctrl,
equeue_packet_vm_request_t *out);
extern kern_status_t vm_controller_recv_async(
kern_handle_t ctrl,
kern_handle_t eq,
equeue_key_t key);
extern kern_status_t vm_controller_create_object(
kern_handle_t ctrl,
const char *name,
size_t name_len,
equeue_key_t key,
size_t data_len,
vm_prot_t prot,
kern_handle_t *out);
extern kern_status_t vm_controller_prepare_attach(
kern_handle_t ctrl,
uint64_t req_id,
kern_handle_t *out_vmo);
extern kern_status_t vm_controller_finish_attach(
kern_handle_t ctrl,
uint64_t req_id,
equeue_key_t new_key);
extern kern_status_t vm_controller_detach_object(
kern_handle_t ctrl,
kern_handle_t vmo);
extern kern_status_t vm_controller_supply_pages(
kern_handle_t ctrl,
kern_handle_t dst_vmo,
off_t dst_offset,
kern_handle_t src_vmo,
off_t src_offset,
size_t length);
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef MAGENTA_SIGNAL_H_
#define MAGENTA_SIGNAL_H_
#define THREAD_SIGNAL_STOPPED 0x01u
#define CHANNEL_SIGNAL_MSG_RECEIVED 0x01u
#define VM_CONTROLLER_SIGNAL_REQUEST_RECEIVED 0x01u
#define EQUEUE_SIGNAL_PACKET_RECEIVED 0x01u
#endif
@@ -1,7 +1,5 @@
#ifndef MANGO_STATUS_H_ #ifndef MAGENTA_STATUS_H_
#define MANGO_STATUS_H_ #define MAGENTA_STATUS_H_
typedef unsigned int kern_status_t;
#define KERN_OK (0) #define KERN_OK (0)
#define KERN_UNIMPLEMENTED (1) #define KERN_UNIMPLEMENTED (1)
+58
View File
@@ -0,0 +1,58 @@
#ifndef MAGENTA_SYSCALL_H_
#define MAGENTA_SYSCALL_H_
#define SYS_KERN_LOG 1
#define SYS_KERN_HANDLE_CLOSE 2
#define SYS_KERN_HANDLE_TRANSFER 3
#define SYS_KERN_HANDLE_CONTROL 4
#define SYS_KERN_CONFIG_GET 5
#define SYS_KERN_CONFIG_SET 6
#define SYS_KERN_OBJECT_WAIT 7
#define SYS_KERN_OBJECT_WAIT_ASYNC 8
#define SYS_TASK_EXIT 9
#define SYS_TASK_SELF 10
#define SYS_TASK_CREATE 11
#define SYS_TASK_CREATE_THREAD 12
#define SYS_TASK_GET_ADDRESS_SPACE 13
#define SYS_TASK_CONFIG_GET 14
#define SYS_TASK_CONFIG_SET 15
#define SYS_TASK_DUPLICATE 16
#define SYS_THREAD_SELF 17
#define SYS_THREAD_START 18
#define SYS_THREAD_EXIT 19
#define SYS_THREAD_CONFIG_GET 20
#define SYS_THREAD_CONFIG_SET 21
#define SYS_VM_OBJECT_CREATE 22
#define SYS_VM_OBJECT_READ 23
#define SYS_VM_OBJECT_WRITE 24
#define SYS_VM_OBJECT_COPY 25
#define SYS_ADDRESS_SPACE_READ 26
#define SYS_ADDRESS_SPACE_WRITE 27
#define SYS_ADDRESS_SPACE_MAP 28
#define SYS_ADDRESS_SPACE_UNMAP 29
#define SYS_ADDRESS_SPACE_RESERVE 30
#define SYS_ADDRESS_SPACE_RELEASE 31
#define SYS_MSG_SEND 32
#define SYS_MSG_RECV 33
#define SYS_MSG_REPLY 34
#define SYS_MSG_READ 35
#define SYS_MSG_WRITE 36
#define SYS_CHANNEL_CREATE 37
#define SYS_PORT_CREATE 38
#define SYS_PORT_CONNECT 39
#define SYS_PORT_DISCONNECT 40
#define SYS_EQUEUE_CREATE 41
#define SYS_EQUEUE_DEQUEUE 42
#define SYS_VM_CONTROLLER_CREATE 43
#define SYS_VM_CONTROLLER_RECV 44
#define SYS_VM_CONTROLLER_RECV_ASYNC 45
#define SYS_VM_CONTROLLER_CREATE_OBJECT 46
#define SYS_VM_CONTROLLER_PREPARE_ATTACH 47
#define SYS_VM_CONTROLLER_FINISH_ATTACH 48
#define SYS_VM_CONTROLLER_DETACH_OBJECT 49
#define SYS_VM_CONTROLLER_SUPPLY_PAGES 50
#define SYS_FUTEX_WAIT 51
#define SYS_FUTEX_WAKE 52
#define SYS_KERN_OBJECT_QUERY 53
#endif
+239
View File
@@ -0,0 +1,239 @@
#ifndef MAGENTA_TYPES_H_
#define MAGENTA_TYPES_H_
#include <stddef.h>
#include <stdint.h>
#define VM_PROT_READ 0x01u
#define VM_PROT_WRITE 0x02u
#define VM_PROT_EXEC 0x04u
#define VM_PROT_USER 0x08u
#define VM_PROT_SVR 0x10u
#define VM_PROT_NOCACHE 0x10u
#define VM_PROT_MAP_SPECIFIC 0x40u
#define MAP_ADDRESS_ANY ((virt_addr_t) - 1)
#define MAP_ADDRESS_INVALID ((virt_addr_t)0)
#define KERN_HANDLE_INVALID ((kern_handle_t)0xFFFFFFFF)
/* task creation flags */
#define TASK_F_DEFAULT 0x0000u
#define TASK_F_CLONE_ALL_HANDLES 0x0001u
/* config keys for use with kern_config_get/kern_config_set */
#define KERN_CFG_INVALID 0x00000u
#define KERN_CFG_PAGE_SIZE 0x00001u
/* config keys for use with task_config_get/task_config_set */
#define TASK_CFG_INVALID 0x00000u
#define TASK_CFG_ID 0x10001u
/* config keys for use with thread_config_get/thread_config_set */
#define THREAD_CFG_INVALID 0x00000u
#define THREAD_CFG_FSBASE 0x20001u
#define THREAD_CFG_GSBASE 0x20002u
/* user-defined flags that can be set on handles */
#define KERN_HANDLE_FLAG0 0x10000000UL
#define KERN_HANDLE_FLAG1 0x20000000UL
#define KERN_HANDLE_FLAG2 0x40000000UL
#define KERN_HANDLE_FLAG3 0x80000000UL
/* flags to specify when creating address-space mappings */
/* this mapping is private. if a task with this mapping is duplicated,
the duplicate task will receive a copy-on-write mapping. changes to one
mapping will not be visible to the other. */
#define VM_PRIVATE 0x0000u
/* this mapping is shared. if a task with this mapping is duplicated,
* the duplicate will receive a mapping of the same data. changes to one mapping
* will be visibile to the other */
#define VM_SHARED 0x0001u
/* maximum number of handles that can be sent in a single message */
#define KERN_MSG_MAX_HANDLES 64
/* the corresponding handle should be ignored */
#define HANDLE_TRANSFER_IGNORE 0
/* the corresponding handle should be moved to the recipient task. the handle
* will be closed. */
#define HANDLE_TRANSFER_MOVE 1
/* the corresponding handle should be copied to the recipient task. the handle
* will remain valid for the sending task. */
#define HANDLE_TRANSFER_COPY 2
/* maximum number of objects that can be waited on in a single call to
* kern_object_wait */
#define KERN_WAIT_MAX_ITEMS 64
/* message types */
#define KERN_MSG_TYPE_NONE 0
#define KERN_MSG_TYPE_DATA 1
#define KERN_MSG_TYPE_EVENT 2
/* event message types */
#define KERN_MSG_EVENT_NONE 0
#define KERN_MSG_EVENT_CONNECTION 1
#define KERN_MSG_EVENT_DISCONNECTION 2
/* equeue packet types */
#define EQUEUE_PKT_VM_REQUEST 0x01u
#define EQUEUE_PKT_ASYNC_SIGNAL 0x02u
/* vm request types */
#define VM_REQUEST_READ 0x01u
#define VM_REQUEST_DIRTY 0x02u
#define VM_REQUEST_ATTACH 0x03u
#define VM_REQUEST_DETACH 0x04u
/* futex special values */
#define FUTEX_WAKE_ALL ((size_t)-1)
/* futex flags */
#define FUTEX_PRIVATE 0x01u
#define FUTEX_SHARED 0x02u
#define IOVEC(p, len) \
{ \
.io_base = (virt_addr_t)(p), \
.io_len = (len), \
}
#define MSG_HANDLE(mode, value) \
{ \
.hnd_mode = (mode), \
.hnd_value = (value), \
}
#define MSG(data, data_count, handles, handles_len) \
{ \
.msg_data = (data), \
.msg_data_count = (data_count), \
.msg_handles = (handles), \
.msg_handles_count = (handles_len), \
}
typedef uintptr_t phys_addr_t;
typedef uintptr_t virt_addr_t;
typedef uint64_t msgid_t;
typedef uint64_t off_t;
typedef uint64_t koid_t;
typedef uintptr_t equeue_key_t;
typedef unsigned int tid_t;
typedef unsigned int vm_controller_packet_type_t;
typedef unsigned int kern_status_t;
typedef uint32_t kern_handle_t;
typedef uint32_t kern_config_key_t;
typedef uint32_t vm_prot_t;
typedef uint32_t vm_flags_t;
typedef uint32_t task_flags_t;
typedef int64_t ssize_t;
typedef uint32_t kern_futex_t;
typedef uint32_t kern_msg_type_t;
typedef uint32_t kern_msg_event_type_t;
typedef unsigned short equeue_packet_type_t;
typedef unsigned int umode_t;
typedef struct {
koid_t obj_id;
} kern_object_info_t;
typedef struct {
virt_addr_t io_base;
size_t io_len;
} kern_iovec_t;
typedef struct {
kern_handle_t w_handle;
uint32_t w_waitfor;
uint32_t w_observed;
} kern_wait_item_t;
typedef struct {
unsigned int hnd_mode;
kern_handle_t hnd_value;
kern_status_t hnd_result;
} kern_msg_handle_t;
typedef struct {
/* transaction id. identifies a particular request/response exchange.
* used when replying to a particular message. */
msgid_t msg_id;
/* the id of the task that sent a particular message. */
tid_t msg_sender;
/* the id of the port or channel used to send a particular message. */
koid_t msg_endpoint;
/* the message type */
kern_msg_type_t msg_type;
union {
/* msg_type = KERN_MSG_TYPE_DATA */
struct {
/* a list of iovecs that point to the buffers that make
* up the main message data. */
kern_iovec_t *msg_data;
size_t msg_data_count;
/* a list of handle entries that contain the kernel
* handles included in a message. */
kern_msg_handle_t *msg_handles;
size_t msg_handles_count;
};
/* msg_type = KERN_MSG_TYPE_EVENT */
struct {
kern_msg_event_type_t msg_event;
};
};
} kern_msg_t;
typedef struct {
uint32_t s_observed;
} equeue_packet_async_signal_t;
typedef struct {
/* the key of the vm-object for which the page request relates, as
* specified when the vm-object was created */
equeue_key_t req_vmo;
/* page request type. one of VM_REQUEST_* */
unsigned short req_type;
/* the offset into the vm-object for which pages are being requested */
union {
/* used for:
* VM_REQUEST_READ
* VM_REQUEST_DIRTY
*/
struct {
off_t req_offset;
/* the length in bytes of the region being requested */
size_t req_length;
};
/* used for:
* VM_REQUEST_ATTACH
*/
struct {
/* the key of the original/source vmo. */
equeue_key_t req_src_vmo;
/* a request ID. used to retrieve information about
* the newly-attached object, as the server won't know
* about it yet, and won't have a handle to it. */
uint64_t req_id;
};
};
} equeue_packet_vm_request_t;
typedef struct {
/* the type of packet. one of EQUEUE_PKT_* */
equeue_packet_type_t p_type;
/* the key of the object that is responsible for the event, as specified
* when the event was first subscribed to */
equeue_key_t p_key;
union {
/* p_type = EQUEUE_PKT_VM_REQUEST */
equeue_packet_vm_request_t vm_request;
/* p_type = EQUEUE_PKT_ASYNC_SIGNAL */
equeue_packet_async_signal_t async_signal;
};
} equeue_packet_t;
#endif
View File
-96
View File
@@ -1,96 +0,0 @@
#include "mango/syscall.h"
# Registers:
# rax = syscall ID + return value
# rdi = 1st parameter
# rsi = 2nd parameter
# rdx = 3rd parameter
# r12 = 4th parameter
# r8 = 5th parameter
# r9 = 6th parameter
# r13 = 7th parameter
# r14 = 8th parameter
.macro SYSCALL_GATE name index args
.global \name
.type \name, @function
\name:
push %rbp
mov %rsp, %rbp
# Syscall ID
mov $\index, %rax
# The syscall instruction uses rcx, so move this parameter to r12
.if \args >= 4
pushq %r12
movq %rcx, %r12
.endif
# r13 and r14 are callee-saved so we save it beforehand
.if \args >= 7
pushq %r13
movq 16(%rbp), %r13
.endif
.if \args >= 8
pushq %r14
movq 24(%rbp), %r14
.endif
syscall
.if \args >= 8
popq %r14
.endif
.if \args >= 7
popq %r13
.endif
.if \args >= 4
popq %r12
.endif
pop %rbp
ret
.endm
SYSCALL_GATE task_exit SYS_TASK_EXIT 1
SYSCALL_GATE task_self SYS_TASK_SELF 0
SYSCALL_GATE task_create SYS_TASK_CREATE 5
SYSCALL_GATE task_create_thread SYS_TASK_CREATE_THREAD 6
SYSCALL_GATE task_get_address_space SYS_TASK_GET_ADDRESS_SPACE 1
SYSCALL_GATE thread_start SYS_THREAD_START 1
SYSCALL_GATE vm_object_create SYS_VM_OBJECT_CREATE 5
SYSCALL_GATE vm_object_read SYS_VM_OBJECT_READ 5
SYSCALL_GATE vm_object_write SYS_VM_OBJECT_WRITE 5
SYSCALL_GATE vm_object_copy SYS_VM_OBJECT_COPY 6
SYSCALL_GATE vm_region_create SYS_VM_REGION_CREATE 8
SYSCALL_GATE vm_region_kill SYS_VM_REGION_KILL 1
SYSCALL_GATE vm_region_read SYS_VM_REGION_READ 5
SYSCALL_GATE vm_region_write SYS_VM_REGION_WRITE 5
SYSCALL_GATE vm_region_map_absolute SYS_VM_REGION_MAP_ABSOLUTE 7
SYSCALL_GATE vm_region_map_relative SYS_VM_REGION_MAP_RELATIVE 7
SYSCALL_GATE vm_region_unmap_absolute SYS_VM_REGION_UNMAP_ABSOLUTE 3
SYSCALL_GATE vm_region_unmap_relative SYS_VM_REGION_UNMAP_RELATIVE 3
SYSCALL_GATE kern_log SYS_KERN_LOG 1
SYSCALL_GATE kern_handle_close SYS_KERN_HANDLE_CLOSE 1
SYSCALL_GATE kern_config_get SYS_KERN_CONFIG_GET 3
SYSCALL_GATE kern_config_set SYS_KERN_CONFIG_SET 3
SYSCALL_GATE channel_create SYS_CHANNEL_CREATE 3
SYSCALL_GATE port_create SYS_PORT_CREATE 1
SYSCALL_GATE port_connect SYS_PORT_CONNECT 3
SYSCALL_GATE port_disconnect SYS_PORT_DISCONNECT 1
SYSCALL_GATE msg_send SYS_MSG_SEND 4
SYSCALL_GATE msg_recv SYS_MSG_RECV 4
SYSCALL_GATE msg_reply SYS_MSG_REPLY 4
SYSCALL_GATE msg_read SYS_MSG_READ 5
SYSCALL_GATE msg_read_handles SYS_MSG_READ_HANDLES 5
SYSCALL_GATE msg_write SYS_MSG_WRITE 5
SYSCALL_GATE msg_write_handles SYS_MSG_WRITE_HANDLES 5
-9
View File
@@ -1,9 +0,0 @@
#ifndef MANGO_HANDLE_H_
#define MANGO_HANDLE_H_
#include <mango/status.h>
#include <mango/types.h>
extern kern_status_t kern_handle_close(kern_handle_t handle);
#endif
-63
View File
@@ -1,63 +0,0 @@
#ifndef MANGO_MSG_H_
#define MANGO_MSG_H_
#include <mango/status.h>
#include <mango/types.h>
extern kern_status_t channel_create(
unsigned int id,
channel_flags_t flags,
kern_handle_t *out);
extern kern_status_t port_create(kern_handle_t *out);
extern kern_status_t port_connect(
kern_handle_t port,
tid_t task_id,
unsigned int channel_id);
extern kern_status_t port_disconnect(kern_handle_t port);
extern kern_status_t msg_send(
kern_handle_t port,
msg_flags_t flags,
const struct msg *req,
struct msg *resp);
extern kern_status_t msg_recv(
kern_handle_t channel,
msg_flags_t flags,
msgid_t *out_id,
struct msg *out_msg);
extern kern_status_t msg_reply(
kern_handle_t channel,
msg_flags_t flags,
msgid_t id,
const struct msg *reply);
extern kern_status_t msg_read(
kern_handle_t channel,
msgid_t id,
size_t offset,
struct iovec *out,
size_t out_count,
size_t *nr_read);
extern kern_status_t msg_read_handles(
kern_handle_t channel,
msgid_t id,
size_t offset,
struct handle_list *out,
size_t nr_out);
extern kern_status_t msg_write(
kern_handle_t channel,
msgid_t id,
size_t offset,
const struct iovec *in,
size_t nr_in);
extern kern_status_t msg_write_handles(
kern_handle_t channel,
msgid_t id,
size_t offset,
const struct handle_list *in,
size_t nr_in);
#endif
-29
View File
@@ -1,29 +0,0 @@
#ifndef MANGO_TASK_H_
#define MANGO_TASK_H_
#include <mango/status.h>
#include <mango/types.h>
extern kern_status_t task_exit(int status);
extern kern_status_t task_self(kern_handle_t *out);
extern kern_status_t task_create(
kern_handle_t parent,
const char *name,
size_t name_len,
kern_handle_t *out_task,
kern_handle_t *out_address_space);
extern kern_status_t task_create_thread(
kern_handle_t task,
virt_addr_t ip,
virt_addr_t sp,
uintptr_t *args,
size_t nr_args,
kern_handle_t *out_thread);
extern kern_status_t task_get_address_space(
kern_handle_t task,
kern_handle_t *out);
extern kern_status_t thread_start(kern_handle_t thread);
#endif
-80
View File
@@ -1,80 +0,0 @@
#ifndef MANGO_VM_H_
#define MANGO_VM_H_
#include <mango/status.h>
#include <mango/types.h>
extern kern_status_t vm_object_create(
const char *name,
size_t name_len,
size_t data_len,
vm_prot_t prot,
kern_handle_t *out);
extern kern_status_t vm_object_read(
kern_handle_t object,
void *dst,
off_t offset,
size_t count,
size_t *nr_read);
extern kern_status_t vm_object_write(
kern_handle_t object,
const void *src,
off_t offset,
size_t count,
size_t *nr_written);
extern kern_status_t vm_object_copy(
kern_handle_t dst,
off_t dst_offset,
kern_handle_t src,
off_t src_offset,
size_t count,
size_t *nr_copied);
extern kern_status_t vm_region_create(
kern_handle_t parent,
const char *name,
size_t name_len,
off_t offset,
size_t region_len,
vm_prot_t prot,
kern_handle_t *out,
virt_addr_t *out_base_address);
extern kern_status_t vm_region_kill(kern_handle_t region);
extern kern_status_t vm_region_read(
kern_handle_t region,
void *dst,
off_t offset,
size_t count,
size_t *nr_read);
extern kern_status_t vm_region_write(
kern_handle_t region,
const void *src,
off_t offset,
size_t count,
size_t *nr_read);
extern kern_status_t vm_region_map_absolute(
kern_handle_t region,
virt_addr_t map_address,
kern_handle_t object,
off_t object_offset,
size_t length,
vm_prot_t prot,
virt_addr_t *out_base_address);
extern kern_status_t vm_region_map_relative(
kern_handle_t region,
off_t region_offset,
kern_handle_t object,
off_t object_offset,
size_t length,
vm_prot_t prot,
virt_addr_t *out_base_address);
extern kern_status_t vm_region_unmap_absolute(
kern_handle_t region,
virt_addr_t address,
size_t length);
extern kern_status_t vm_region_unmap_relative(
kern_handle_t region,
off_t offset,
size_t length);
#endif
-38
View File
@@ -1,38 +0,0 @@
#ifndef MANGO_SYSCALL_H_
#define MANGO_SYSCALL_H_
#define SYS_TASK_EXIT 1
#define SYS_TASK_SELF 31
#define SYS_TASK_CREATE 2
#define SYS_TASK_CREATE_THREAD 3
#define SYS_TASK_GET_ADDRESS_SPACE 33
#define SYS_THREAD_START 30
#define SYS_VM_OBJECT_CREATE 4
#define SYS_VM_OBJECT_READ 5
#define SYS_VM_OBJECT_WRITE 6
#define SYS_VM_OBJECT_COPY 29
#define SYS_VM_REGION_CREATE 7
#define SYS_VM_REGION_KILL 34
#define SYS_VM_REGION_READ 8
#define SYS_VM_REGION_WRITE 9
#define SYS_VM_REGION_MAP_ABSOLUTE 10
#define SYS_VM_REGION_MAP_RELATIVE 11
#define SYS_VM_REGION_UNMAP_ABSOLUTE 12
#define SYS_VM_REGION_UNMAP_RELATIVE 13
#define SYS_KERN_LOG 14
#define SYS_KERN_HANDLE_CLOSE 15
#define SYS_KERN_CONFIG_GET 16
#define SYS_KERN_CONFIG_SET 17
#define SYS_MSG_SEND 18
#define SYS_MSG_RECV 19
#define SYS_MSG_REPLY 20
#define SYS_MSG_READ 21
#define SYS_MSG_READ_HANDLES 22
#define SYS_MSG_WRITE 23
#define SYS_MSG_WRITE_HANDLES 24
#define SYS_CHANNEL_CREATE 25
#define SYS_PORT_CREATE 26
#define SYS_PORT_CONNECT 27
#define SYS_PORT_DISCONNECT 28
#endif
-76
View File
@@ -1,76 +0,0 @@
#ifndef MANGO_TYPES_H_
#define MANGO_TYPES_H_
#include <stddef.h>
#include <stdint.h>
#define VM_PROT_READ 0x01u
#define VM_PROT_WRITE 0x02u
#define VM_PROT_EXEC 0x04u
#define VM_PROT_USER 0x08u
#define VM_PROT_SVR 0x10u
#define VM_PROT_NOCACHE 0x10u
#define VM_PROT_MAP_SPECIFIC 0x40u
/* if this flag is set, other tasks can connect to this channel using
* the port_connect_* syscalls.
* if this flag is NOT set, only threads in the task that owns the channel
* can create ports connecting to it. */
#define CHANNEL_F_ALLOW_DIRECT_CONNECTIONS 0x01u
/* msg_reply: once the reply has been sent, disconnect the port that sent the
* original message */
#define MSG_F_DISCONNECT_AFTER_REPLY 0x01u
#define VM_REGION_ANY_OFFSET ((off_t) - 1)
#define KERN_HANDLE_INVALID ((kern_handle_t)0xFFFFFFFF)
#define KERN_CFG_INVALID 0x00u
#define KERN_CFG_PAGE_SIZE 0x01u
#define IOVEC(p, len) \
{ \
.io_base = (virt_addr_t)(p), \
.io_len = (len), \
}
#define MSG(data, data_count, handles, handles_len) \
{ \
.msg_data = (data), \
.msg_data_count = (data_count), \
.msg_handles = (handles), \
.msg_handles_count = (handles_len), \
}
typedef uintptr_t phys_addr_t;
typedef uintptr_t virt_addr_t;
typedef uint64_t msgid_t;
typedef uint64_t off_t;
typedef uint64_t koid_t;
typedef unsigned int tid_t;
typedef uint32_t kern_handle_t;
typedef uint32_t kern_config_key_t;
typedef uint32_t vm_prot_t;
typedef uint32_t channel_flags_t;
typedef uint32_t msg_flags_t;
typedef unsigned int umode_t;
struct iovec {
virt_addr_t io_base;
size_t io_len;
};
struct handle_list {
kern_handle_t *l_handles;
size_t l_nr_handles;
};
struct msg {
struct iovec *msg_data;
size_t msg_data_count;
struct handle_list *msg_handles;
size_t msg_handles_count;
};
#endif

Some files were not shown because too many files have changed in this diff Show More