Skip to content

Commit 28d4b09

Browse files
committed
Fixed fork issues
1 parent 47a730e commit 28d4b09

File tree

19 files changed

+509
-367
lines changed

19 files changed

+509
-367
lines changed

Source/Config/arch/x86_64/Artifacts/kernel/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#define KERNEL_STACK_SIZE 0x1000
2828

2929
/* Maximal number of CPU supported by the architecture */
30-
#define SOC_CPU_COUNT 4
30+
#define SOC_CPU_COUNT 1
3131

3232
/* Kernel log level */
3333
#define KERNEL_LOG_LEVEL DEBUG_LOG_LEVEL

Source/Config/arch/x86_64/Artifacts/kernel/settings.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ endif
2626
KERNEL_LINKER_FILE = ../../Config/arch/x86_64/Artifacts/kernel/kernel_linker.ld
2727

2828
DEBUG_FLAGS = -O0 -g
29-
EXTRA_FLAGS = -O3 -fno-asynchronous-unwind-tables
29+
EXTRA_FLAGS = -O3 -fno-asynchronous-unwind-tables -g
3030

3131
CFLAGS = -std=c11 -nostdinc -fno-builtin -nostdlib \
3232
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c -fno-pie \

Source/Config/arch/x86_64/x86_64_fdt.dts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
compatible = "x86,x86-vesa";
204204
resolution = <1024 768>;
205205
depth = <32>;
206-
refresh-rate = <1>;
206+
refresh-rate = <60>;
207207
device = "/dev/vesa";
208208
};
209209

Source/Kernel/arch/bsp/x86/src/core_mgt.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
*/
116116
static bool _ipiInterruptHandler(kernel_thread_t* pCurrThread);
117117

118+
#endif /* #if SOC_CPU_COUNT > 1 */
119+
118120
/**
119121
* @brief Attaches the Core Manager driver to the system.
120122
*
@@ -133,6 +135,8 @@ static OS_RETURN_E _coreMgtAttach(const fdt_node_t* pkFdtNode);
133135
* GLOBAL VARIABLES
134136
******************************************************************************/
135137

138+
#if SOC_CPU_COUNT > 1
139+
136140
/************************* Imported global variables **************************/
137141
/** @brief Stores the number of enabled (running) cores in the system. */
138142
extern volatile uint32_t _bootedCPUCount;
@@ -467,7 +471,8 @@ void cpuMgtSendIpi(const uint32_t kFlags,
467471
const bool kAllocateParam)
468472
{
469473
(void)kFlags;
470-
(void)kpParams;
474+
(void)pParams;
475+
(void)kAllocateParam;
471476
}
472477

473478
#endif /* SOC_CPU_COUNT > 1 */

Source/Kernel/arch/cpu/i386/src/memory.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,9 @@ static void _pageFaultHandler(kernel_thread_t* pCurrentThread)
858858
/* Check if the entry is set as COW */
859859
if((flags & MEMMGR_MAP_COW) == MEMMGR_MAP_COW)
860860
{
861-
error = _memoryManageCOW(faultAddress,
862-
physAddr,
863-
pCurrentThread);
861+
error = memoryManageCOW(faultAddress,
862+
physAddr,
863+
pCurrentThread);
864864
if(error != OS_NO_ERR)
865865
{
866866
staleEntry = false;

Source/Kernel/arch/cpu/includes/memory.h

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ typedef struct
7676
uintptr_t limit;
7777
} mem_range_t;
7878

79+
/** @brief Structure that defines an information about a memory page. */
80+
typedef struct
81+
{
82+
/** @brief Virtual address of the memory page. */
83+
uintptr_t virtAddress;
84+
/** @brief Physical address of the memory page. */
85+
uintptr_t physAddress;
86+
/** @brief Mapping flags */
87+
uintptr_t flags;
88+
} memory_page_info_t;
89+
7990
/*******************************************************************************
8091
* MACROS
8192
******************************************************************************/
@@ -370,10 +381,10 @@ OS_RETURN_E memoryUserMapDirect(const void* kPhysicalAddress,
370381
kernel_process_t* pProcess);
371382

372383
/**
373-
* @brief Unmaps a virtual region (memory or hardware) from the kernel address
384+
* @brief Unmaps a virtual region (memory or hardware) from the user address
374385
* space.
375386
*
376-
* @details Unmaps a virtual region (memory or hardware) from the kernel address
387+
* @details Unmaps a virtual region (memory or hardware) from the user address
377388
* space. The virtual address and the size must be aligned on page
378389
* boundaries. If not, the unmapping fails and an error is returned.
379390
*
@@ -436,6 +447,38 @@ OS_RETURN_E memoryUserFree(const void* kVirtualAddress,
436447
const size_t kSize,
437448
kernel_process_t* pProcess);
438449

450+
/**
451+
* @brief Handles a CopyOnWrite event.
452+
*
453+
* @details Handles a CopyOnWrite event. If the faulted page is COW, copies it
454+
* if the reference count is greater than 1, otherwise simply set the page as
455+
* writable.
456+
*
457+
* @param[in] kFaultVirtAddr The virtual address that generated to fault.
458+
* @param[in] kPhysAddr The physical address that corresponds to the faulted
459+
* page.
460+
* @param[in] kpThread The thread that raised the COW exception.
461+
*
462+
* @return The function returns the success or error status.
463+
*/
464+
OS_RETURN_E memoryManageCOW(const uintptr_t kFaultVirtAddr,
465+
const uintptr_t kPhysAddr,
466+
const kernel_thread_t* kpThread);
467+
468+
/**
469+
* @brief Returns the page informations of a thread.
470+
*
471+
* @details Returns the page informations of a thread. This function will fill
472+
* the table given as parameter with the page information of the given thread.
473+
*
474+
* @param[in] kpThread The thread for which the information should be extracted.
475+
* @param[out] pPageInfo The table to fill.
476+
* @param[in/out] pSize The size of the table. This value is updated with the
477+
* actual size of the table after filling it.
478+
*/
479+
void memoryGetPagesInfo(const kernel_thread_t* kpThread,
480+
memory_page_info_t* pPageInfo,
481+
size_t* pSize);
439482
#endif /* #ifndef __MEMORY_MGR_ */
440483

441484
/************************************ EOF *************************************/

Source/Kernel/arch/cpu/x86_64/src/cpu.c

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,6 +4935,9 @@ OS_RETURN_E cpuCopyVirtualCPUs(const kernel_thread_t* kpSrcThread,
49354935
pStackOffset;
49364936
}
49374937

4938+
/* Update kernel stack end */
4939+
pCurVCpu->kernelStackEnd = pDstThread->kernelStackEnd - 0x10;
4940+
49384941
return OS_NO_ERR;
49394942
}
49404943

@@ -5425,10 +5428,8 @@ OS_RETURN_E cpuCopyLocalStorage(kernel_thread_t* pThread,
54255428
size_t align;
54265429
size_t size;
54275430
OS_RETURN_E error;
5428-
OS_RETURN_E intError;
5429-
uintptr_t tls;
5430-
uintptr_t tlsPhys;
5431-
void* pTmpData;
5431+
uintptr_t srcTls;
5432+
uintptr_t srcTlsPhys;
54325433

54335434
if(pThread->type == THREAD_TYPE_KERNEL)
54345435
{
@@ -5444,68 +5445,39 @@ OS_RETURN_E cpuCopyLocalStorage(kernel_thread_t* pThread,
54445445
return OS_NO_ERR;
54455446
}
54465447

5447-
/* Compute the memory size with alignement */
5448+
5449+
/* Get the original thread TLS virtual and physical addresses */
54485450
align = MAX(pThread->pProcess->mainTlsAlign, __alignof__(user_thread_t));
5449-
/* We do not support more than a page alignement */
5450-
if(align > KERNEL_PAGE_SIZE)
5451-
{
5452-
return OS_ERR_NOT_SUPPORTED;
5453-
}
5451+
srcTls = (uintptr_t)kpSrcThread->pUserThreadData -
5452+
ALIGN_UP(pThread->pProcess->mainTlsSize, align);
5453+
srcTlsPhys = memoryMgrGetPhysAddr(srcTls, kpSrcThread->pProcess, NULL);
54545454

5455+
/* Copy the thread local storage that was COW */
54555456
size = ALIGN_UP(pThread->pProcess->mainTlsSize, align) +
54565457
sizeof(user_thread_t);
54575458

54585459
/* Align to page size */
54595460
size = ALIGN_UP(size, KERNEL_PAGE_SIZE);
5460-
5461-
pThread->pUserThreadData = memoryUserAllocate(size,
5462-
MEMMGR_MAP_RW |
5463-
MEMMGR_MAP_USER,
5464-
pThread->pProcess,
5465-
&error);
5466-
if(error != OS_NO_ERR)
5467-
{
5468-
return error;
5469-
}
5470-
5471-
tlsPhys = memoryMgrGetPhysAddr((uintptr_t)pThread->pUserThreadData,
5472-
pThread->pProcess,
5473-
NULL);
5474-
CPU_ASSERT(tlsPhys != MEMMGR_PHYS_ADDR_ERROR,
5475-
"Failed to get mapped physical address",
5476-
OS_ERR_INCORRECT_VALUE);
5477-
5478-
pTmpData = memoryKernelMap((void*)tlsPhys,
5479-
size,
5480-
MEMMGR_MAP_KERNEL | MEMMGR_MAP_RW,
5481-
&error);
5482-
if(error != OS_NO_ERR)
5461+
while(size > 0)
54835462
{
5484-
intError = memoryUserFree(pThread->pUserThreadData,
5485-
size,
5486-
pThread->pProcess);
5487-
CPU_ASSERT(intError == OS_NO_ERR,
5488-
"Failed to free allocated memory",
5489-
OS_ERR_INCORRECT_VALUE);
5490-
return error;
5463+
error = memoryManageCOW(srcTls, srcTlsPhys, pThread);
5464+
if(error != OS_NO_ERR)
5465+
{
5466+
return error;
5467+
}
5468+
error = memoryManageCOW(srcTls, srcTlsPhys, kpSrcThread);
5469+
if(error != OS_NO_ERR)
5470+
{
5471+
return error;
5472+
}
5473+
size -= KERNEL_PAGE_SIZE;
5474+
srcTls += KERNEL_PAGE_SIZE;
5475+
srcTlsPhys += KERNEL_PAGE_SIZE;
54915476
}
54925477

5493-
/* Get the TLS and copy */
5494-
tls = (uintptr_t)kpSrcThread->pUserThreadData -
5495-
ALIGN_UP(pThread->pProcess->mainTlsSize, align);
5496-
memcpy(pTmpData,
5497-
(void*)tls,
5498-
(uintptr_t)kpSrcThread->pUserThreadData - tls);
5499-
5500-
intError = memoryKernelUnmap(pTmpData, size);
5501-
CPU_ASSERT(intError == OS_NO_ERR,
5502-
"Failed to unmap mapped memory",
5503-
OS_ERR_INCORRECT_VALUE);
5504-
55055478
/* Setup the user data pointer */
5506-
pThread->pUserThreadData = (void*)((uintptr_t)pThread->pUserThreadData +
5507-
ALIGN_UP(pThread->pProcess->mainTlsSize, align));
5508-
5479+
pThread->pUserThreadData = kpSrcThread->pUserThreadData;
5480+
55095481
return OS_NO_ERR;
55105482
}
55115483

Source/Kernel/arch/cpu/x86_64/src/cpuContext.s

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ cpuSignalHandlerLoop:
269269
; Param:
270270
; Input: rdi: The address to return to when restoring the context
271271
; rsi: The current thread
272-
273272
cpuSwitchKernelSyscallContext:
274273
; Save the return address in rcx
275274
pop rcx

Source/Kernel/arch/cpu/x86_64/src/cpuSyscall.s

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ cpuSystemCallInit:
9191
; Input: rdi: The systen call ID
9292
; rsi: A pointer to the system call parameters.
9393
cpuUserSyscallHandler:
94+
cli
95+
9496
; Save the user stack
9597
push r11
9698
push rcx
@@ -120,18 +122,29 @@ cpuUserSyscallHandler:
120122
mov [r12 + VCPU_OFF_SYSCALL_RSP], rsp
121123

122124
; Call the main C handler
125+
sti
123126
call syscallHandle
124127

125128
; Discard the scheduler return that was pushed in case the call generated a
126129
; scheduling.
127130
add rsp, 8
128131

132+
; Get the current thread handle and VCPU
133+
push rdi
134+
push rsi
135+
call schedGetCurrentThread
136+
pop rsi
137+
pop rdi
138+
mov rax, [rax]
139+
129140
; Clear the syscall stack registration
130-
xor rax, rax
131-
mov [r12 + VCPU_OFF_SYSCALL_RSP], rax
141+
xor rbx, rbx
142+
mov [rax + VCPU_OFF_SYSCALL_RSP], rbx
132143

133144

134145
__cpuUserSyscallReturn:
146+
cli
147+
135148
; Restore the user stack
136149
pop rax
137150
mov rsp, rax
@@ -148,6 +161,7 @@ __cpuUserSyscallReturn:
148161

149162
; Return from syscall
150163
o64 sysret
164+
151165

152166
;-------------------------------------------------------------------------------
153167
; Raise a kernel space system call.

Source/Kernel/arch/cpu/x86_64/src/intHandlers.s

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ __intHandler%1:
4242
jmp __intHandlerEntry ; jump to the common handler
4343
%endmacro
4444

45+
%macro ERR_CODE_INT_HANDLER_DBG 1 ; Interrupt that do not come with an
46+
; err code.
47+
48+
global __intHandler%1
49+
__intHandler%1:
50+
cli
51+
pop rax
52+
pop rbx
53+
pop rcx
54+
pop rdx
55+
pop r8
56+
mov rsp, r8
57+
_dbg_loop%1:
58+
cli
59+
jmp _dbg_loop%1 ; jump to the common handler
60+
%endmacro
61+
4562
;-------------------------------------------------------------------------------
4663
; EXTERN DATA
4764
;-------------------------------------------------------------------------------
@@ -77,11 +94,11 @@ __intHandlerEntry:
7794
call interruptMainHandler
7895

7996
; Now create handlers for each interrupt
80-
ERR_CODE_INT_HANDLER 8
97+
ERR_CODE_INT_HANDLER_DBG 8
8198
ERR_CODE_INT_HANDLER 10
8299
ERR_CODE_INT_HANDLER 11
83100
ERR_CODE_INT_HANDLER 12
84-
ERR_CODE_INT_HANDLER 13
101+
ERR_CODE_INT_HANDLER_DBG 13
85102
ERR_CODE_INT_HANDLER 14
86103
ERR_CODE_INT_HANDLER 17
87104
ERR_CODE_INT_HANDLER 30

0 commit comments

Comments
 (0)