PatchworkOS  c9fea19
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>
7
9{
10 if (stack == NULL || maxPages == 0 || !VMM_IS_PAGE_ALIGNED(maxAddress))
11 {
12 errno = EINVAL;
13 return ERR;
14 }
15
16 stack->top = maxAddress;
17 stack->bottom = stack->top - (maxPages * PAGE_SIZE);
18 if (stack->bottom >= stack->top) // Overflow
19 {
21 return ERR;
22 }
23 stack->guardTop = stack->bottom;
24 stack->guardBottom = stack->guardTop - (STACK_POINTER_GUARD_PAGES * PAGE_SIZE);
25 if (stack->guardBottom >= stack->guardTop) // Overflow
26 {
28 return ERR;
29 }
30 stack->lastPageFault = 0;
31
32 return 0;
33}
34
36{
37 if (stack == NULL || buffer == NULL || pages == 0 || !VMM_IS_PAGE_ALIGNED(buffer))
38 {
39 errno = EINVAL;
40 return ERR;
41 }
42
43 stack->top = (uintptr_t)buffer + pages * PAGE_SIZE;
44 stack->bottom = (uintptr_t)buffer;
45 if (stack->bottom >= stack->top) // Overflow
46 {
48 return ERR;
49 }
50 // No guard pages when using a buffer.
51 stack->guardTop = stack->bottom;
52 stack->guardBottom = stack->bottom;
53 stack->lastPageFault = 0;
54
55 memset(buffer, 0, pages * PAGE_SIZE);
56
57 return 0;
58}
59
61{
62 if (stack == NULL)
63 {
64 return;
65 }
66
67 vmm_unmap(&thread->process->space, (void*)stack->bottom, BYTES_TO_PAGES(stack->top - stack->bottom));
68
69 stack->top = 0;
70 stack->bottom = 0;
71 stack->guardTop = 0;
72 stack->guardBottom = 0;
73 stack->lastPageFault = 0;
74}
75
77{
78 if (stack == NULL)
79 {
80 return;
81 }
82
83 stack->top = 0;
84 stack->bottom = 0;
85 stack->guardTop = 0;
86 stack->guardBottom = 0;
87 stack->lastPageFault = 0;
88}
89
91{
92 if (stack == NULL)
93 {
94 return false;
95 }
96
97 uintptr_t endAddr = addr + length;
98 if (endAddr < addr)
99 {
100 return false;
101 }
102
103 return addr >= stack->bottom && endAddr < stack->top;
104}
105
107{
108 if (stack == NULL)
109 {
110 return false;
111 }
112
113 uintptr_t endAddr = addr + length;
114 if (endAddr < addr)
115 {
116 return false;
117 }
118
119 return !(endAddr <= stack->guardBottom || addr >= stack->guardTop);
120}
121
123{
124 uint64_t rsp = rsp_read();
125 for (uint64_t i = 0; i < offset; i += MIN(PAGE_SIZE, offset - i))
126 {
127 volatile uint8_t dummy;
128 dummy = *((uint8_t*)(rsp - i));
129 }
130}
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, uint64_t length)
Unmaps virtual memory from a given address space.
Definition vmm.c:334
#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:16
#define PAGE_SIZE
The size of a memory page in bytes.
Definition proc.h:106
#define BYTES_TO_PAGES(amount)
Convert a size in bytes to pages.
Definition proc.h:114
#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:38
static uint64_t rsp_read()
Definition regs.h:138
__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:210
Structure to define a stack in memory.
Thread of execution structure.
Definition thread.h:56
process_t * process
The parent process that the thread executes within.
Definition thread.h:57