···11+/*
22+ * Copyright (C) 2020-2021 Nikita Melekhin. All rights reserved.
33+ *
44+ * Use of this source code is governed by a BSD-style license that can be
55+ * found in the LICENSE file.
66+ */
77+88+#ifndef _KERNEL_TASKING_BITS_CPU_H
99+#define _KERNEL_TASKING_BITS_CPU_H
1010+1111+#include <drivers/generic/fpu.h>
1212+#include <libkern/types.h>
1313+#include <platform/generic/tasking/context.h>
1414+1515+#define CPU_CNT 1
1616+#define THIS_CPU (&cpus[0])
1717+#define RUNNIG_THREAD (THIS_CPU->running_thread)
1818+#define FPU_ENABLED
1919+2020+struct thread;
2121+typedef int cpu_state_t;
2222+enum CPU_STATE {
2323+ CPU_IN_KERNEL,
2424+ CPU_IN_USERLAND,
2525+};
2626+2727+typedef struct {
2828+ char* kstack;
2929+ context_t* scheduler; // context of sched's registers
3030+ struct thread* running_thread;
3131+ cpu_state_t current_state;
3232+#ifdef FPU_ENABLED
3333+ // Information about current state of fpu.
3434+ struct thread* fpu_for_thread;
3535+ pid_t fpu_for_pid;
3636+#endif // FPU_ENABLED
3737+} cpu_t;
3838+3939+extern cpu_t cpus[CPU_CNT];
4040+4141+#endif // _KERNEL_TASKING_BITS_CPU_H
+24
kernel/include/tasking/cpu.h
···11+/*
22+ * Copyright (C) 2020-2021 Nikita Melekhin. All rights reserved.
33+ *
44+ * Use of this source code is governed by a BSD-style license that can be
55+ * found in the LICENSE file.
66+ */
77+88+#ifndef _KERNEL_TASKING_CPU_H
99+#define _KERNEL_TASKING_CPU_H
1010+1111+#include <drivers/generic/fpu.h>
1212+#include <tasking/bits/cpu.h>
1313+1414+static inline void cpu_enter_kernel_space()
1515+{
1616+ THIS_CPU->current_state = CPU_IN_KERNEL;
1717+}
1818+1919+static inline void cpu_leave_kernel_space()
2020+{
2121+ THIS_CPU->current_state = CPU_IN_USERLAND;
2222+}
2323+2424+#endif // _KERNEL_TASKING_CPU_H
···44#include <platform/generic/registers.h>
55#include <platform/generic/system.h>
66#include <platform/x86/isr_handler.h>
77+#include <tasking/cpu.h>
78#include <tasking/dump.h>
89#include <tasking/sched.h>
910#include <tasking/tasking.h>
···4950void isr_handler(trapframe_t* tf)
5051{
5152 system_disable_interrupts();
5353+ cpu_enter_kernel_space();
52545355 proc_t* p = NULL;
5456 if (likely(RUNNIG_THREAD)) {
···84868587 /* We are leaving interrupt, and later interrupts will be on,
8688 when flags are restored */
8989+ cpu_leave_kernel_space();
8790 system_enable_interrupts_only_counter();
8891}
+4
kernel/kernel/syscalls/handler.c
···1212#include <platform/generic/syscalls/params.h>
1313#include <platform/generic/system.h>
1414#include <syscalls/handlers.h>
1515+#include <tasking/cpu.h>
15161617/* From Linux 4.14.0 headers. */
1718/* https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md#x86-32_bit */
···7475 /* This hack has to be here, when a context switching happens
7576 during a syscall (e.g. when block occurs). The hack will start
7677 interrupts again after it has become a running thread. */
7878+ cpu_enter_kernel_space();
7779 system_enable_interrupts();
7880 return param1;
7981}
···100102void sys_handler(trapframe_t* tf)
101103{
102104 system_disable_interrupts();
105105+ cpu_enter_kernel_space();
103106 void (*callee)(trapframe_t*) = (void*)syscalls[sys_id];
104107 callee(tf);
108108+ cpu_leave_kernel_space();
105109 system_enable_interrupts_only_counter();
106110}
107111