PatchworkOS
Loading...
Searching...
No Matches
stack_pointer.c
Go to the documentation of this file.
2
3#include <kernel/log/log.h>
4#include <kernel/mem/vmm.h>
6
8{
9 if (stack == NULL || maxPages == 0 || !VMM_IS_PAGE_ALIGNED(maxAddress))
10 {
11 errno = EINVAL;
12 return ERR;
13 }
14
15 stack->top = maxAddress;
16 stack->bottom = stack->top - (maxPages * PAGE_SIZE);
17 if (stack->bottom >= stack->top) // Overflow
18 {
20 return ERR;
21 }
22 stack->guardTop = stack->bottom;
23 stack->guardBottom = stack->guardTop - (STACK_POINTER_GUARD_PAGES * PAGE_SIZE);
24 if (stack->guardBottom >= stack->guardTop) // Overflow
25 {
27 return ERR;
28 }
29 stack->lastPageFault = 0;
30
31 return 0;
32}
33
35{
36 if (stack == NULL || buffer == NULL || pages == 0 || !VMM_IS_PAGE_ALIGNED(buffer))
37 {
38 errno = EINVAL;
39 return ERR;
40 }
41
42 stack->top = (uintptr_t)buffer + pages * PAGE_SIZE;
43 stack->bottom = (uintptr_t)buffer;
44 if (stack->bottom >= stack->top) // Overflow
45 {
47 return ERR;
48 }
49 // No guard pages when using a buffer.
50 stack->guardTop = stack->bottom;
51 stack->guardBottom = stack->bottom;
52 stack->lastPageFault = 0;
53
54 memset(buffer, 0, pages * PAGE_SIZE);
55
56 return 0;
57}
58
60{
61 if (stack == NULL)
62 {
63 return;
64 }
65
66 vmm_unmap(&thread->process->space, (void*)stack->bottom, BYTES_TO_PAGES(stack->top - stack->bottom));
67
68 stack->top = 0;
69 stack->bottom = 0;
70 stack->guardTop = 0;
71 stack->guardBottom = 0;
72 stack->lastPageFault = 0;
73}
74
76{
77 if (stack == NULL)
78 {
79 return;
80 }
81
82 stack->top = 0;
83 stack->bottom = 0;
84 stack->guardTop = 0;
85 stack->guardBottom = 0;
86 stack->lastPageFault = 0;
87}
88
90{
91 if (stack == NULL)
92 {
93 return false;
94 }
95
96 uintptr_t endAddr = addr + length;
97 if (endAddr < addr)
98 {
99 return false;
100 }
101
102 return addr >= stack->bottom && endAddr < stack->top;
103}
104
106{
107 if (stack == NULL)
108 {
109 return false;
110 }
111
112 uintptr_t endAddr = addr + length;
113 if (endAddr < addr)
114 {
115 return false;
116 }
117
118 return !(endAddr <= stack->guardBottom || addr >= stack->guardTop);
119}
void stack_pointer_deinit_buffer(stack_pointer_t *stack)
Deinitializes a stack pointer structure that was initialized using stack_pointer_init_buffer().
#define STACK_POINTER_GUARD_PAGES
The amount of guard pages to use for stacks.
uint64_t stack_pointer_init_buffer(stack_pointer_t *stack, void *buffer, uint64_t pages)
Initializes a stack pointer structure using a provided buffer, does not allocate or map any memory.
void stack_pointer_deinit(stack_pointer_t *stack, thread_t *thread)
Deinitializes a stack pointer structure and unmaps any mapped memory.
bool stack_pointer_overlaps_guard(stack_pointer_t *stack, uintptr_t addr, uint64_t length)
Check if an region overlaps the guard.
uint64_t stack_pointer_init(stack_pointer_t *stack, uintptr_t maxAddress, uint64_t maxPages)
Initializes a stack pointer structure, does not allocate or map any memory.
bool stack_pointer_is_in_stack(stack_pointer_t *stack, uintptr_t addr, uint64_t length)
Check if an region is within the stack.
#define VMM_IS_PAGE_ALIGNED(addr)
Check if an address is page aligned.
Definition vmm.h:83
uint64_t vmm_unmap(space_t *space, void *virtAddr, uint64_t length)
Unmaps virtual memory from a given address space.
Definition vmm.c:339
#define EINVAL
Invalid argument.
Definition errno.h:142
#define EOVERFLOW
Value too large for defined data type.
Definition errno.h:402
#define errno
Error number variable.
Definition errno.h:27
#define PAGE_SIZE
Memory page size.
Definition proc.h:140
#define BYTES_TO_PAGES(amount)
Convert bytes to pages.
Definition proc.h:151
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t maxPages
Definition mem.c:16
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static pmm_stack_t stack
Definition pmm.c:36
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
_PUBLIC void * memset(void *s, int c, size_t n)
Definition memset.c:4
space_t space
Definition process.h:59
Structure to define a stack in memory.
Thread of execution structure.
Definition thread.h:55
process_t * process
The parent process that the thread executes within.
Definition thread.h:57