Skip to content

Commit f9289d2

Browse files
authored
Merge pull request #107 from KaiNorberg/develop
Develop
2 parents 3984a1d + 2af42e5 commit f9289d2

152 files changed

Lines changed: 4037 additions & 2287 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Make.defaults

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ MKCWD = @mkdir -p $(@D)
22

33
CC = gcc
44
AS = gcc
5-
LD = ld
6-
AR = ar
5+
LD = gcc
6+
AR = gcc-ar
77
OBJCOPY = objcopy
88

99
CFLAGS := \
1010
-std=gnu11 \
11+
-ffreestanding \
12+
-fno-builtin \
13+
-fno-pie \
14+
-mno-red-zone \
15+
-nostdinc \
1116
-Wall \
1217
-Wextra \
1318
-Werror \
@@ -20,14 +25,8 @@ CFLAGS := \
2025
-Wno-deprecated-non-prototype \
2126
-Wno-unused-but-set-parameter \
2227
-Wno-unused-command-line-argument \
23-
-nostdlib \
24-
-nostdinc \
25-
-ffreestanding \
26-
-fno-builtin \
2728
-isystem include \
2829
-isystem include/libstd \
29-
-Lbin/libstd \
30-
-Lbin/libpatchwork \
3130
-Iinclude \
3231
-Ilib \
3332
-I$(SRCDIR) \
@@ -40,7 +39,6 @@ CFLAGS_DISABLE_SIMD := \
4039
-mno-ssse3 -mno-sse4
4140

4241
ASFLAGS := \
43-
-nostdlib \
4442
-nostdinc \
4543
-ffreestanding \
4644
-fno-builtin \
@@ -52,7 +50,8 @@ ASFLAGS := \
5250
LDFLAGS := \
5351
-nostdlib \
5452
-Lbin/libstd \
55-
-Lbin/libpatchwork
53+
-Lbin/libpatchwork \
54+
-no-pie
5655

5756
ifeq ($(TESTING),1)
5857
CFLAGS += -D_TESTING_
@@ -85,13 +84,11 @@ CFLAGS_MODULE := \
8584
-D__STDC_WANT_LIB_EXT1__=1
8685

8786
ASFLAGS_MODULE := \
88-
$(ASFLAGS) \
8987
-fPIC \
9088
-D_KERNEL_MODULE_ \
9189
-D__STDC_WANT_LIB_EXT1__=1
9290

9391
LDFLAGS_MODULE := \
94-
$(LDFLAGS) \
9592
-shared \
9693
-fPIC \
9794
-Tinclude/modules/linker.lds
@@ -106,4 +103,4 @@ OBJ = $(patsubst src/%, $(BUILDDIR)/%.o, $(SRC))
106103

107104
DEP = $(OBJ:.o=.d)
108105

109-
-include $(DEP)
106+
-include $(DEP)

Makefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,22 @@ lib/gnu-efi/.built: | lib/gnu-efi
9797

9898
$(BOOT_TARGET): setup | bin/boot
9999
@echo "BUILD boot"
100-
@$(MAKE) -s --no-print-directory -f src/boot/boot.mk SRCDIR=src/boot BUILDDIR=build/boot BINDIR=bin/boot
100+
@$(MAKE) -s --no-print-directory -f src/boot/boot.mk SRCDIR=src/boot BUILDDIR=build/boot BINDIR=bin/boot all
101101
@touch $@
102102

103103
$(KERNEL_TARGET): setup | bin/kernel
104104
@echo "BUILD kernel"
105-
@$(MAKE) -s --no-print-directory -f src/kernel/kernel.mk SRCDIR=src/kernel BUILDDIR=build/kernel BINDIR=bin/kernel
105+
@$(MAKE) -s --no-print-directory -f src/kernel/kernel.mk SRCDIR=src/kernel BUILDDIR=build/kernel BINDIR=bin/kernel all
106106
@touch $@
107107

108108
$(LIBSTD_TARGET): setup | bin/libstd
109109
@echo "BUILD libstd"
110-
@$(MAKE) -s --no-print-directory -f src/libstd/libstd.mk SRCDIR=src/libstd BUILDDIR=build/libstd BINDIR=bin/libstd
110+
@$(MAKE) -s --no-print-directory -f src/libstd/libstd.mk SRCDIR=src/libstd BUILDDIR=build/libstd BINDIR=bin/libstd all
111111
@touch $@
112112

113113
$(LIBPATCHWORK_TARGET): setup | bin/libpatchwork
114114
@echo "BUILD libpatchwork"
115-
@$(MAKE) -s --no-print-directory -f src/libpatchwork/libpatchwork.mk SRCDIR=src/libpatchwork BUILDDIR=build/libpatchwork BINDIR=bin/libpatchwork
115+
@$(MAKE) -s --no-print-directory -f src/libpatchwork/libpatchwork.mk SRCDIR=src/libpatchwork BUILDDIR=build/libpatchwork BINDIR=bin/libpatchwork all
116116
@touch $@
117117

118118
lib/argon2/.built: $(MODULES_TARGETS)
@@ -129,7 +129,7 @@ lib/argon2/.built: $(MODULES_TARGETS)
129129
define MODULE_RULE
130130
bin/modules/.$(1).built: $(filter %/$(1).mk,$(MODULES_MK)) $(BOOT_TARGET) $(KERNEL_TARGET) $(LIBSTD_TARGET) $(LIBPATCHWORK_TARGET) | bin/modules
131131
@echo "BUILD module $(1)"
132-
@$(MAKE) -s --no-print-directory -f $(filter %/$(1).mk,$(MODULES_MK)) SRCDIR=$(dir $(filter %/$(1).mk,$(MODULES_MK))) BUILDDIR=build/modules/$(1) BINDIR=bin/modules MODULE=$(1)
132+
@$$(MAKE) -s --no-print-directory -f $$(filter %/$(1).mk,$$(MODULES_MK)) SRCDIR=$$(dir $$(filter %/$(1).mk,$$(MODULES_MK))) BUILDDIR=build/modules/$(1) BINDIR=bin/modules MODULE=$(1) all
133133
@touch $$@
134134
endef
135135

@@ -138,7 +138,7 @@ $(foreach mod,$(MODULES_NAMES),$(eval $(call MODULE_RULE,$(mod))))
138138
define BOX_RULE
139139
bin/boxes/.$(1).built: $(filter %/$(1).mk,$(BOXES_MK)) $(MODULES_TARGETS) lib/argon2/.built | bin/boxes
140140
@echo "BUILD box $(1)"
141-
@$(MAKE) -s --no-print-directory -f $(filter %/$(1).mk,$(BOXES_MK)) SRCDIR=$(dir $(filter %/$(1).mk,$(BOXES_MK))) BUILDDIR=build/boxes/$(1) BINDIR=bin/boxes BOX=$(1)
141+
@$$(MAKE) -s --no-print-directory -f $$(filter %/$(1).mk,$$(BOXES_MK)) SRCDIR=$$(dir $$(filter %/$(1).mk,$$(BOXES_MK))) BUILDDIR=build/boxes/$(1) BINDIR=bin/boxes BOX=$(1) all
142142
@touch $$@
143143
endef
144144

@@ -147,7 +147,7 @@ $(foreach box,$(BOXES_NAMES),$(eval $(call BOX_RULE,$(box))))
147147
define PROGRAM_RULE
148148
bin/programs/.$(1).built: $(filter %/$(1).mk,$(PROGRAMS_MK)) $(BOXES_TARGETS) | bin/programs
149149
@echo "BUILD program $(1)"
150-
@$(MAKE) -s --no-print-directory -f $(filter %/$(1).mk,$(PROGRAMS_MK)) SRCDIR=$(dir $(filter %/$(1).mk,$(PROGRAMS_MK))) BUILDDIR=build/programs/$(1) BINDIR=bin/programs PROGRAM=$(1)
150+
@$$(MAKE) -s --no-print-directory -f $$(filter %/$(1).mk,$$(PROGRAMS_MK)) SRCDIR=$$(dir $$(filter %/$(1).mk,$$(PROGRAMS_MK))) BUILDDIR=build/programs/$(1) BINDIR=bin/programs PROGRAM=$(1) all
151151
@touch $$@
152152
endef
153153

@@ -252,7 +252,7 @@ nuke: clean
252252
@rm -rf lib/doomgeneric-patchworkos lib/lua-5.4.7 lib/acpica lib/acpica_tests lib/argon2 include/argon2 meta/docs
253253
@rm -rf lib/gnu-efi/.built lib/argon2/.built lib/acpica_tests/.built
254254

255-
QEMU_MEMORY ?= 1G
255+
QEMU_MEMORY ?= 2G
256256
QEMU_CPUS ?= $(shell nproc 2>/dev/null || echo 8)
257257
QEMU_MACHINE ?= q35
258258
QEMU_ARGS ?=

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ Will this project ever reach its goals? Probably not, but that's not the point.
5858
- Physical and virtual memory management is `O(1)` per page and `O(n)` where `n` is the number of pages per allocation/mapping operation, see [benchmarks](#benchmarks) for more info.
5959
- File based IPC including [pipes](https://kainorberg.github.io/PatchworkOS/html/d7/d64/group__modules__ipc__pipe.html), [shared memory](https://kainorberg.github.io/PatchworkOS/html/df/d3f/group__modules__ipc__shmem.html), [sockets](https://kainorberg.github.io/PatchworkOS/html/d4/db0/group__kernel__fs__netfs.html) and Plan9 inspired "signals" called [notes](https://kainorberg.github.io/PatchworkOS/html/d8/db1/group__kernel__ipc__note.html).
6060
- File based device API [abstractions](https://kainorberg.github.io/PatchworkOS/html/de/d7b/group__kernel__drivers__abstract.html), including framebuffers, input devices, etc.
61-
- [Synchronization primitives](https://kainorberg.github.io/PatchworkOS/html/dd/d6b/group__kernel__sync.html) including mutexes, read-write locks, sequential locks, futexes and others.
61+
- [Synchronization primitives](https://kainorberg.github.io/PatchworkOS/html/dd/d6b/group__kernel__sync.html) including Read-Copy-Update, mutexes, R/W locks, sequential locks, futexes and others.
6262
- Highly [Modular design](#modules), even [SMP Bootstrapping](https://kainorberg.github.io/PatchworkOS/html/d3/d0a/group__modules__smp.html) is done in a module.
6363

6464
### File System
6565

66-
- UNIX-style VFS with mountpoints, hardlinks, symlinks, per-process namespaces, etc.
66+
- Inode and dentry based VFS with RCU traversal, hardlinks, symlinks, per-process namespaces, etc.
6767
- Custom [Framebuffer BitMaP](https://github.com/KaiNorberg/fbmp) (.fbmp) image format, allows for faster loading by removing the need for parsing.
6868
- Custom [Grayscale Raster Font](https://github.com/KaiNorberg/grf) (.grf) font format, allows for antialiasing and kerning without complex vector graphics.
6969

@@ -84,7 +84,6 @@ Will this project ever reach its goals? Probably not, but that's not the point.
8484

8585
## Notable Future Plans
8686

87-
- Fix filesystem mounting vulnerability.
8887
- File servers (FUSE, 9P?).
8988
- Implement user system in user-space using namespaces.
9089
- Improve `share()` and `claim()` security by specifying a target PID when sharing.
@@ -440,7 +439,7 @@ It would even be possible to implement a multi-user-like system entirely in user
440439

441440
### Hiding Dentries
442441

443-
For complex use cases, relying on just mountpoints becomes exponentially complex. As such, the Virtual File System allows a filesystem to dynamically hide directories and files using the `revalidate()` dentry operation.
442+
For complex use cases, relying on just mountpoints becomes exponentially complex. As such, the Virtual File System allows a filesystem to dynamically hide directories and files using the `revalidate()` dentry operation.
444443

445444
For example, in "procfs", a process can see all the `/proc/[pid]/` files of processes in its namespace and in child namespaces but for processes in parent namespaces certain files will appear to not exist in the filesystem hierarchy. The "netfs" filesystem works similarly making sure that only processes in the namespace that created a socket can see its directory.
446445

include/kernel/config.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
* The `CONFIG_TIME_SLICE` constant defines the default time slice given to threads when they are scheduled.
9696
*
9797
*/
98-
#define CONFIG_TIME_SLICE ((CLOCKS_PER_SEC / 1000) * 10)
98+
#define CONFIG_TIME_SLICE ((CLOCKS_PER_MS) * 10)
9999

100100
/**
101101
* @brief Cache hot threshold configuration.
@@ -106,7 +106,7 @@
106106
* hot", meaning that the data is likely still in the CPU cache.
107107
*
108108
*/
109-
#define CONFIG_CACHE_HOT_THRESHOLD ((CLOCKS_PER_SEC / 1000) * 5)
109+
#define CONFIG_CACHE_HOT_THRESHOLD ((CLOCKS_PER_MS) * 5)
110110

111111
/**
112112
* @brief Maximum mutex slow spin configuration.
@@ -169,4 +169,25 @@
169169
* The `CONFIG_LOG_KLOG_BUFFER_SIZE` constant defines the size of the buffer used for the `/dev/klog` file.
170170
*
171171
*/
172-
#define CONFIG_KLOG_SIZE 0x4000
172+
#define CONFIG_KLOG_SIZE 0x4000
173+
174+
/**
175+
* @brief Per-CPU data size configuration.
176+
* @ingroup kernel
177+
* @def CONFIG_PERCPU_SIZE
178+
*
179+
* The `CONFIG_PERCPU_SIZE` constant defines the size allocated for per-CPU data.
180+
*
181+
*/
182+
#define CONFIG_PERCPU_SIZE 0x10000
183+
184+
/**
185+
* @brief Maximum wait queues configuration.
186+
* @ingroup kernel
187+
* @def CONFIG_MAX_WAIT_QUEUES
188+
*
189+
* The `CONFIG_MAX_WAIT_QUEUES` constant defines the maximum amount of wait queues that a thread can wait on
190+
* simultaneously.
191+
*
192+
*/
193+
#define CONFIG_MAX_WAIT_QUEUES 64

include/kernel/cpu/cli.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#pragma once
2+
3+
#include <assert.h>
4+
#include <kernel/cpu/cpu.h>
5+
#include <kernel/cpu/percpu.h>
6+
#include <kernel/cpu/regs.h>
7+
#include <stdbool.h>
8+
#include <stdint.h>
9+
10+
/**
11+
* @brief Clear Interrupt Flag (CLI) Handling
12+
* @defgroup kernel_cpu_cli CLI
13+
* @ingroup kernel_cpu
14+
*
15+
* Manages nested CLI (Clear Interrupt Flag) calls.
16+
* @{
17+
*/
18+
19+
/**
20+
* @brief Increments the CLI depth, disabling interrupts if depth was zero.
21+
*
22+
* @warning Must have a matching `cli_pop()` call to re-enable interrupts when depth reaches zero.
23+
*/
24+
static inline void cli_push(void)
25+
{
26+
uint64_t rflags = rflags_read();
27+
asm volatile("cli" ::: "memory");
28+
29+
if (SELF->cli == 0)
30+
{
31+
SELF->oldRflags = rflags;
32+
}
33+
SELF->cli++;
34+
}
35+
36+
/**
37+
* @brief Decrements the CLI depth, re-enabling interrupts if depth reaches zero and interrupts were enabled prior to
38+
* the first `cli_push()` call.
39+
*
40+
* @warning This function should only be called after a `cli_push()` call.
41+
*/
42+
static inline void cli_pop(void)
43+
{
44+
assert(!(rflags_read() & RFLAGS_INTERRUPT_ENABLE));
45+
assert(SELF->cli != 0);
46+
SELF->cli--;
47+
if (SELF->cli == 0 && (SELF->oldRflags & RFLAGS_INTERRUPT_ENABLE))
48+
{
49+
asm volatile("sti" ::: "memory");
50+
}
51+
}
52+
53+
/**
54+
* @brief Macro to increment CLI depth for the duration of the current scope.
55+
*/
56+
#define CLI_SCOPE() \
57+
cli_push(); \
58+
__attribute__((cleanup(cli_scope_cleanup))) int CONCAT(i, __COUNTER__) = 1;
59+
60+
static inline void cli_scope_cleanup(int* _)
61+
{
62+
cli_pop();
63+
}
64+
65+
/** @} */

0 commit comments

Comments
 (0)