This operating system is a macro-kernel, has its root in Intel's x86 platform and its ecosystem. It runs in protected mode and uses 4GiB addressing with two-level paging mechanism. It does not have x86_64 variant and does not support multi-core machine.
-The memory is split into two parts, that is, 3GiB for user space (0x400000~0xBFFFFFFF) and 1GiB for kernel space (0xC0000000~0xFFFFFFFF). Such paradigm is a common practicing found in major operating systems, for example x86_32 version of Linux and Windows. For a more detailed arrangement of memory in LunaixOS, please refer to [LunaixOS Virtual Memory Mappings](docs/img/lunaix-os-mem.png).
+The memory is split into two parts, that is, 3GiB for user space (0x400000 ~ 0xBFFFFFFF) and 1GiB for kernel space (0xC0000000 ~ 0xFFFFFFFF). Such paradigm is a common practicing found in major operating systems, for example x86_32 version of Linux and Windows. For a more detailed arrangement of memory in LunaixOS, please refer to [LunaixOS Virtual Memory Mappings](docs/img/lunaix-os-mem.png).
The following list presents all features it does have in current stage.
if (do_kernel(&mapping)) {
return;
}
- goto segv_term;
+ // 如果不是,那么看看内核是不是需要用户页。
}
struct mm_region* hit_region = region_get(&__current->mm.regions, ptr);
if (!hit_region) {
- // Into the void...
+ // 当你凝视深渊时……
goto segv_term;
}
- x86_pte_t* pte = &PTE_MOUNTED(PD_REFERENCED, ptr >> 12);
+ volatile x86_pte_t* pte = &PTE_MOUNTED(PD_REFERENCED, ptr >> 12);
if ((*pte & PG_PRESENT)) {
if ((hit_region->attr & COW_MASK) == COW_MASK) {
// normal page fault, do COW
#include <arch/x86/tss.h>
#include <lunaix/common.h>
-struct x86_tss _tss = { .link = 0, .esp0 = KSTACK_TOP, .ss0 = KDATA_SEG };
+volatile struct x86_tss _tss = { .link = 0,
+ .esp0 = KSTACK_TOP,
+ .ss0 = KDATA_SEG };
void
tss_update_esp(uint32_t esp0)
--- /dev/null
+#include <lunaix/lunistd.h>
+#include <lunaix/proc.h>
+#include <lunaix/signal.h>
+#include <lunaix/spike.h>
+#include <lunaix/syslog.h>
+#include <lunaix/types.h>
+
+LOG_MODULE("SIGDEMO")
+
+void __USER__
+sigchild_handler(int signum)
+{
+ kprintf(KINFO "SIGCHLD received\n");
+}
+
+void __USER__
+sigsegv_handler(int signum)
+{
+ pid_t pid = getpid();
+ kprintf(KWARN "SIGSEGV received on process %d\n", pid);
+ _exit(signum);
+}
+
+void __USER__
+sigalrm_handler(int signum)
+{
+ pid_t pid = getpid();
+ kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
+}
+
+void __USER__
+_signal_demo_main()
+{
+ signal(_SIGCHLD, sigchild_handler);
+ signal(_SIGSEGV, sigsegv_handler);
+ signal(_SIGALRM, sigalrm_handler);
+
+ alarm(5);
+
+ int status;
+ pid_t p = 0;
+
+ kprintf(KINFO "Child sleep 3s, parent pause.\n");
+ if (!fork()) {
+ sleep(3);
+ _exit(0);
+ }
+
+ pause();
+
+ kprintf("Parent resumed on SIGCHILD\n");
+
+ for (int i = 0; i < 5; i++) {
+ pid_t pid = 0;
+ if (!(pid = fork())) {
+ signal(_SIGSEGV, sigsegv_handler);
+ sleep(i);
+ if (i == 3) {
+ i = *(int*)0xdeadc0de; // seg fault!
+ }
+ kprintf(KINFO "%d\n", i);
+ _exit(0);
+ }
+ kprintf(KINFO "Forked %d\n", pid);
+ }
+
+ while ((p = wait(&status)) >= 0) {
+ short code = WEXITSTATUS(status);
+ if (WIFSIGNALED(status)) {
+ kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
+ p,
+ code);
+ } else if (WIFEXITED(status)) {
+ kprintf(KINFO "Process %d exited with code %d\n", p, code);
+ } else {
+ kprintf(KWARN "Process %d aborted with code %d\n", p, code);
+ }
+ }
+
+ kprintf("done\n");
+
+ spin();
+}
\ No newline at end of file
#include <lunaix/ds/semaphore.h>
#include <lunaix/sched.h>
-void sem_init(struct sem_t *sem, unsigned int initial) {
+void
+sem_init(struct sem_t* sem, unsigned int initial)
+{
sem->counter = ATOMIC_VAR_INIT(initial);
}
-void sem_wait(struct sem_t *sem) {
+void
+sem_wait(struct sem_t* sem)
+{
while (!atomic_load(&sem->counter)) {
schedule();
}
atomic_fetch_sub(&sem->counter, 1);
}
-void sem_post(struct sem_t *sem) {
+void
+sem_post(struct sem_t* sem)
+{
atomic_fetch_add(&sem->counter, 1);
// TODO: wake up a thread
}
\ No newline at end of file
#include <lunaix/mm/kalloc.h>
#include <lunaix/mm/vmm.h>
#include <lunaix/proc.h>
-#include <lunaix/signal.h>
#include <lunaix/spike.h>
#include <lunaix/syslog.h>
#include <lunaix/timer.h>
#define WAIT_DEMO
#define IN_USER_MODE
-void __USER__
-sigchild_handler(int signum)
-{
- kprintf(KINFO "SIGCHLD received\n");
-}
-
-void __USER__
-sigsegv_handler(int signum)
-{
- pid_t pid = getpid();
- kprintf(KWARN "SIGSEGV received on process %d\n", pid);
- _exit(signum);
-}
-
-void __USER__
-sigalrm_handler(int signum)
-{
- pid_t pid = getpid();
- kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
-}
-
void __USER__
_lxinit_main()
{
}
#endif
- signal(_SIGCHLD, sigchild_handler);
- signal(_SIGSEGV, sigsegv_handler);
- signal(_SIGALRM, sigalrm_handler);
-
- alarm(5);
-
int status;
#ifdef WAIT_DEMO
// 测试wait
kprintf("I am child, I am about to terminated\n");
_exit(1);
}
- pause();
+ wait(&status);
pid_t child = wait(&status);
kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
child,
for (size_t i = 0; i < 5; i++) {
pid_t pid = 0;
if (!(pid = fork())) {
- signal(_SIGSEGV, sigsegv_handler);
sleep(i);
if (i == 3) {
i = *(int*)0xdeadc0de; // seg fault!
while ((p = wait(&status)) >= 0) {
short code = WEXITSTATUS(status);
- if (WIFSIGNALED(status)) {
- kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
- p,
- code);
- } else if (WIFEXITED(status)) {
+ if (WIFEXITED(status)) {
kprintf(KINFO "Process %d exited with code %d\n", p, code);
} else {
kprintf(KWARN "Process %d aborted with code %d\n", p, code);
void
__do_reserved_memory(int unlock);
+//#define DEMO_SIGNAL
+
void __USER__
__proc0_usr()
{
if (!fork()) {
+#ifdef DEMO_SIGNAL
+ asm("jmp _signal_demo_main");
+#else
asm("jmp _lxinit_main");
+#endif
}
while (1) {
void* default_handlers[_SIG_NUM] = {
// TODO: 添加默认handler
+ [_SIGINT] = default_sighandler_term, [_SIGTERM] = default_sighandler_term,
+ [_SIGKILL] = default_sighandler_term, [_SIGSEGV] = default_sighandler_term,
[_SIGINT] = default_sighandler_term,
- [_SIGTERM] = default_sighandler_term,
- [_SIGKILL] = default_sighandler_term,
};
// Referenced in kernel/asm/x86/interrupt.S