-
Notifications
You must be signed in to change notification settings - Fork 1
Development #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Development #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "Ipc.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "Process.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "Panic.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "../Memory/MemOps.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void IpcSendMessage(uint32_t target_pid, IpcMessage* msg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ASSERT(msg != NULL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Process* target = GetProcessByPid(target_pid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!target) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Handle error: target process not found | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MessageQueue* queue = &target->ipc_queue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (queue->count >= MAX_MESSAGES) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Handle error: queue is full | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
FastMemcpy(&queue->messages[queue->tail], msg, sizeof(IpcMessage)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
queue->tail = (queue->tail + 1) % MAX_MESSAGES; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
queue->count++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (target->state == PROC_BLOCKED) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
target->state = PROC_READY; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int IpcReceiveMessage(IpcMessage* msg_buffer) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ASSERT(msg_buffer != NULL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Process* current = GetCurrentProcess(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MessageQueue* queue = ¤t->ipc_queue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (queue->count == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
current->state = PROC_BLOCKED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Yield(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
FastMemcpy(msg_buffer, &queue->messages[queue->head], sizeof(IpcMessage)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
queue->head = (queue->head + 1) % MAX_MESSAGES; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
queue->count--; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return 0; // Success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+30
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add synchronization and improve blocking mechanism. The current implementation has potential race conditions and inefficient blocking: Consider these improvements: int IpcReceiveMessage(IpcMessage* msg_buffer) {
ASSERT(msg_buffer != NULL);
Process* current = GetCurrentProcess();
MessageQueue* queue = ¤t->ipc_queue;
+ // TODO: Add spinlock or disable interrupts here for queue protection
+
while (queue->count == 0) {
+ // TODO: Release spinlock before blocking
current->state = PROC_BLOCKED;
Yield();
+ // TODO: Re-acquire spinlock after waking up
}
FastMemcpy(msg_buffer, &queue->messages[queue->head], sizeof(IpcMessage));
queue->head = (queue->head + 1) % MAX_MESSAGES;
queue->count--;
+
+ // TODO: Release spinlock or re-enable interrupts here
return 0; // Success
} Also consider adding a timeout mechanism to prevent indefinite blocking. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef IPC_H | ||
#define IPC_H | ||
|
||
#include "stdint.h" | ||
|
||
#define MAX_MESSAGES 16 // Max messages per process queue | ||
|
||
// Extensible message types | ||
typedef enum { | ||
IPC_TYPE_DATA, | ||
IPC_TYPE_NOTIFICATION, | ||
// Future types can be added here | ||
} IpcMessageType; | ||
|
||
typedef struct { | ||
uint32_t sender_pid; | ||
IpcMessageType type; | ||
uint64_t size; | ||
union { | ||
char data[256]; // For general data | ||
uint64_t value; // For notifications or simple values | ||
} payload; | ||
} IpcMessage; | ||
|
||
typedef struct { | ||
IpcMessage messages[MAX_MESSAGES]; | ||
uint32_t head; | ||
uint32_t tail; | ||
uint32_t count; | ||
} MessageQueue; | ||
|
||
void IpcSendMessage(uint32_t target_pid, IpcMessage* msg); | ||
int IpcReceiveMessage(IpcMessage* msg_buffer); | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
#define CPU_H | ||
|
||
#include "stdint.h" | ||
|
||
typedef struct { | ||
uint8_t sse:1; | ||
uint8_t sse2:1; | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,8 +4,7 @@ | |||||
#include "Panic.h" | ||||||
#include "Io.h" | ||||||
#include "../Memory/MemOps.h" | ||||||
|
||||||
#define NULL ((void*)0) | ||||||
#include "../Core/Ipc.h" | ||||||
#define offsetof(type, member) ((uint64_t)&(((type*)0)->member)) | ||||||
|
||||||
static Process processes[MAX_PROCESSES]; | ||||||
|
@@ -121,7 +120,7 @@ uint32_t CreateProcess(void (*entry_point)(void)) { | |||||
} | ||||||
|
||||||
void ProcessExitStub() { | ||||||
PrintKernel("[KERNEL] Process returned from its main function. This is an error!\n"); | ||||||
PrintKernel("[KERNEL] Process returned from its main function. -FATAL EXECPTION-\n"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo in error message. "EXECPTION" should be "EXCEPTION". - PrintKernel("[KERNEL] Process returned from its main function. -FATAL EXECPTION-\n");
+ PrintKernel("[KERNEL] Process returned from its main function. -FATAL EXCEPTION-\n"); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||
PrintKernel("Terminating process PID: "); | ||||||
PrintKernelInt(GetCurrentProcess()->pid); | ||||||
PrintKernel("\n"); | ||||||
|
@@ -177,6 +176,11 @@ uint32_t CreateSecureProcess(void (*entry_point)(void), uint8_t privilege) { | |||||
processes[slot].priority = (privilege == PROC_PRIV_SYSTEM) ? 10 : 100; | ||||||
processes[slot].is_user_mode = (privilege != PROC_PRIV_SYSTEM); | ||||||
|
||||||
// Initialize IPC queue | ||||||
processes[slot].ipc_queue.head = 0; | ||||||
processes[slot].ipc_queue.tail = 0; | ||||||
processes[slot].ipc_queue.count = 0; | ||||||
|
||||||
// Create the token | ||||||
init_token(&processes[slot].token, creator->pid, privilege, new_pid); | ||||||
|
||||||
|
@@ -198,6 +202,7 @@ uint32_t CreateSecureProcess(void (*entry_point)(void), uint8_t privilege) { | |||||
} | ||||||
|
||||||
void ScheduleFromInterrupt(struct Registers* regs) { | ||||||
ASSERT(regs != NULL); | ||||||
if (process_count <= 1) return; | ||||||
|
||||||
// Save current context | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,12 @@ | |
#include "Idt.h" | ||
#include "Panic.h" | ||
#include "../Memory/MemOps.h" // For FastMemcpy | ||
#include "../Core/Ipc.h" | ||
|
||
#define likely(x) __builtin_expect(!!(x), 1) | ||
#define unlikely(x) __builtin_expect(!!(x), 0) | ||
extern void SyscallEntry(void); | ||
uint64_t SyscallHandler(uint64_t syscall_num, uint64_t arg1, uint64_t arg2, uint64_t arg3) { | ||
uint64_t Syscall(uint64_t syscall_num, uint64_t arg1, uint64_t arg2, uint64_t arg3) { | ||
Process* current = GetCurrentProcess(); | ||
if (unlikely(!current)) { | ||
Panic("Syscall from invalid process"); | ||
|
@@ -49,6 +50,15 @@ uint64_t SyscallHandler(uint64_t syscall_num, uint64_t arg1, uint64_t arg2, uint | |
|
||
case SYS_GETPID: | ||
return current ? current->pid : -1; | ||
|
||
case SYS_IPC_SEND: | ||
// arg1 = target_pid, arg2 = message | ||
IpcSendMessage((uint32_t)arg1, (IpcMessage*)arg2); | ||
return 0; | ||
Comment on lines
+54
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Validate user pointers and add security checks for IPC send. The current implementation has several security vulnerabilities:
Consider implementing proper validation: case SYS_IPC_SEND:
// arg1 = target_pid, arg2 = message
- IpcSendMessage((uint32_t)arg1, (IpcMessage*)arg2);
- return 0;
+ if (!arg2 || arg1 == 0) {
+ return -1; // Invalid arguments
+ }
+
+ // Copy message from user space to kernel space
+ IpcMessage kernel_msg;
+ if (CopyFromUser(&kernel_msg, (void*)arg2, sizeof(IpcMessage)) != 0) {
+ return -1; // Failed to copy from user space
+ }
+
+ // Validate message fields
+ if (kernel_msg.size > sizeof(kernel_msg.payload.data)) {
+ return -1; // Invalid message size
+ }
+
+ // Set sender PID to prevent spoofing
+ kernel_msg.sender_pid = current->pid;
+
+ IpcSendMessage((uint32_t)arg1, &kernel_msg);
+ return 0; Note: You'll need to implement
🤖 Prompt for AI Agents
|
||
|
||
case SYS_IPC_RECV: | ||
// arg1 = message_buffer | ||
return IpcReceiveMessage((IpcMessage*)arg1); | ||
Comment on lines
+59
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Validate user buffer for IPC receive. The implementation lacks validation of the user-provided buffer pointer, which could lead to arbitrary memory writes. Implement proper validation and safe copying: case SYS_IPC_RECV:
// arg1 = message_buffer
- return IpcReceiveMessage((IpcMessage*)arg1);
+ if (!arg1) {
+ return -1; // Invalid buffer
+ }
+
+ // Receive message into kernel buffer first
+ IpcMessage kernel_msg;
+ int result = IpcReceiveMessage(&kernel_msg);
+ if (result != 0) {
+ return result;
+ }
+
+ // Copy message to user space
+ if (CopyToUser((void*)arg1, &kernel_msg, sizeof(IpcMessage)) != 0) {
+ return -1; // Failed to copy to user space
+ }
+
+ return 0; Note: You'll need to implement
🤖 Prompt for AI Agents
|
||
|
||
default: | ||
return -1; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve error handling and add synchronization for IpcSendMessage.
Several issues need to be addressed:
Consider these improvements:
📝 Committable suggestion
🤖 Prompt for AI Agents