PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
stack_pointer.c
Go to the documentation of this file.
1#include <kernel/cpu/regs.h>
3
4#include <kernel/log/log.h>
5#include <kernel/mem/vmm.h>
8
10{
11 if (stack == NULL || maxPages == 0 || !VMM_IS_PAGE_ALIGNED(maxAddress))
12 {
13 errno = EINVAL;
14 return ERR;
15 }
16
17 stack->top = maxAddress;
18 stack->bottom = stack->top - (maxPages * PAGE_SIZE);
19 if (stack->bottom >= stack->top) // Overflow
20 {
22 return ERR;
23 }
24 stack->guardTop = stack->bottom;
25 stack->guardBottom = stack->guardTop - (STACK_POINTER_GUARD_PAGES * PAGE_SIZE);
26 if (stack->guardBottom >= stack->guardTop) // Overflow
27 {
29 return ERR;
30 }
31 stack->lastPageFault = 0;
32
33 return 0;
34}
35
37{
38 if (stack == NULL || buffer == NULL || pages == 0 || !VMM_IS_PAGE_ALIGNED(buffer))
39 {
40 errno = EINVAL;
41 return ERR;
42 }
43
45 stack->bottom = (uintptr_t)buffer;
46 if (stack->bottom >= stack->top) // Overflow
47 {
49 return ERR;
50 }
51 // No guard pages when using a buffer.
52 stack->guardTop = stack->bottom;
53 stack->guardBottom = stack->bottom;
54 stack->lastPageFault = 0;
55
57
58 return 0;
59}
60
62{
63 if (stack == NULL)
64 {
65 return;
66 }
67
68 vmm_unmap(&thread->process->space, (void*)stack->bottom, BYTES_TO_PAGES(stack->top - stack->bottom));
69
70 stack->top = 0;
71 stack->bottom = 0;
72 stack->guardTop = 0;
73 stack->guardBottom = 0;
74 stack->lastPageFault = 0;
75}
76
78{
79 if (stack == NULL)
80 {
81 return;
82 }
83
84 stack->top = 0;
85 stack->bottom = 0;
86 stack->guardTop = 0;
87 stack->guardBottom = 0;
88 stack->lastPageFault = 0;
89}
90
92{
93 if (stack == NULL)
94 {
95 return false;
96 }
97
98 uintptr_t endAddr = addr + length;
99 if (endAddr < addr)
100 {
101 return false;
102 }
103
104 return addr >= stack->bottom && endAddr < stack->top;
105}
106
108{
109 if (stack == NULL)
110 {
111 return false;
112 }
113
114 uintptr_t endAddr = addr + length;
115 if (endAddr < addr)
116 {
117 return false;
118 }
119
120 return !(endAddr <= stack->guardBottom || addr >= stack->guardTop);
121}
122
124{
125 uint64_t rsp = rsp_read();
126 for (uint64_t i = 0; i < offset; i += MIN(PAGE_SIZE, offset - i))
127 {
128 volatile uint8_t dummy;
129 dummy = *((uint8_t*)(rsp - i));
130 }
131}
size_t maxPages
Definition main.c:238
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
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.
void stack_pointer_poke(uint64_t offset)
Poke the stack to ensure that a page fault will occur at the given offset.
#define VMM_IS_PAGE_ALIGNED(addr)
Check if an address is page aligned.
Definition vmm.h:83
void * vmm_unmap(space_t *space, void *virtAddr, size_t length)
Unmaps virtual memory from a given address space.
Definition vmm.c:336
#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 MIN(x, y)
Definition math.h:18
#define BYTES_TO_PAGES(amount)
Convert a size in bytes to pages.
Definition proc.h:107
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
#define PAGE_SIZE
The size of a memory page in bytes.
Definition PAGE_SIZE.h:8
static uint64_t offset
Definition screen.c:19
static page_t * pages
Definition pmm.c:38
static page_stack_t * stack
Definition pmm.c:40
static uint64_t rsp_read(void)
Definition regs.h:140
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
__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:84
Structure to define a stack in memory.
Thread of execution structure.
Definition thread.h:61
process_t * process
The parent process that the thread executes within.
Definition thread.h:62