Skip to content

Commit d583063

Browse files
committed
[misc]removed brk heap in favor of full anon, cleaned up structs
1 parent f230b98 commit d583063

14 files changed

Lines changed: 74 additions & 163 deletions

File tree

kernel/bin/monitor_processes.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ui/draw/draw.h"
99
#include "std/string.h"
1010
#include "theme/theme.h"
11+
#include "memory/addr.h"
1112
#include "math/math.h"
1213
#include "syscalls/syscalls.h"
1314
#include "memory/memory_types.h"
@@ -17,12 +18,12 @@ char* parse_proc_state(int state){
1718
{
1819
case STOPPED:
1920
return "Stopped";
20-
2121
case READY:
22+
return "Ready";
2223
case RUNNING:
23-
case BLOCKED:
2424
return "Running";
25-
25+
case BLOCKED:
26+
return "Blocked";
2627
default:
2728
return "Invalid";
2829
}
@@ -46,7 +47,7 @@ void print_process_info(){
4647
if (proc->id != 0 && proc->state != STOPPED && (!procname || strcmp_case(procname,proc->name,true) == 0)){
4748
print("Process [%i]: %s [pid = %i | status = %s]",i,(uintptr_t)proc->name,proc->id,(uintptr_t)parse_proc_state(proc->state));
4849
print("Stack: %x (%x). SP: %x",proc->stack, proc->stack_size, proc->sp);
49-
print("Heap: %x (%x)",proc->mm.brk, calc_heap(proc->heap_phys));
50+
print("Heap: %x (%x)",proc->mm.mmap_bottom, calc_heap(proc->heap_phys));
5051
print("Flags: %x", proc->spsr);
5152
print("PC: %x",proc->pc);
5253
}
@@ -132,10 +133,10 @@ void draw_process_view(){
132133
fb_draw_string(&ctx,pc.data, xo, pc_y, scale, system_theme.bg_color);
133134
string_free(pc);
134135

135-
draw_memory("Stack", xo, stack_y, stack_width, stack_height, proc->stack - proc->sp, proc->stack_size);
136-
uint64_t heap = proc->mm.ttbr0 ? (proc->mm.rss_heap_pages * PAGE_SIZE) : calc_heap(proc->heap_phys);
137-
uint64_t heap_limit = proc->mm.ttbr0 ? (proc->mm.brk > proc->mm.heap_start ? (uint64_t)(proc->mm.brk - proc->mm.heap_start) : (uint64_t)PAGE_SIZE) : ((heap + 0xFFF) & ~0xFFF);
138-
draw_memory("Heap", xo + stack_width + 50, stack_y, stack_width, stack_height, heap, heap_limit);
136+
draw_memory("Stack", xo, stack_y, stack_width, stack_height, proc->stack - proc->sp, proc->stack_size ? proc->stack_size : 1);
137+
uint64_t heap = proc->mm.ttbr0 ? (proc->mm.rss_anon_pages * PAGE_SIZE) : calc_heap(proc->heap_phys);
138+
uint64_t heap_limit = proc->mm.ttbr0 ? (uint64_t)(proc->mm.mmap_top - proc->mm.mmap_bottom) : ((heap + 0xFFF) & ~0xFFF);
139+
draw_memory("Heap", xo + stack_width + 50, stack_y, stack_width, stack_height, heap, heap_limit ? heap_limit : PAGE_SIZE);
139140

140141
string flags = string_format("Flags: %x", proc->spsr);
141142
fb_draw_string(&ctx, flags.data, xo, flags_y, scale, system_theme.bg_color);

kernel/bin/tracert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static bool parse_args(int argc, char *argv[], tr_opts_t *o) {
3838
o->src_set = false;
3939
o->host = NULL;
4040

41-
for (int i = 1; i < argc; ++i) {
41+
for (int i = 1; i < argc; i++) {
4242
const char *a = argv[i];
4343
if (a && a[0] == '-') {
4444
if (strcmp_case(a, "-4",true) == 0) {

kernel/kernel_processes/kprocess_loader.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ process_t *create_kernel_process(const char *name, int (*func)(int argc, char* a
5050
proc->stack_size = stack_size;
5151

5252
proc->heap_phys = heap;
53-
proc->mm.heap_start = (uaddr_t)dmap_pa_to_kva(heap);
54-
proc->mm.brk = proc->mm.heap_start;
5553

5654
proc->sp = proc->stack;
5755

@@ -114,7 +112,8 @@ process_t *create_kernel_process(const char *name, int (*func)(int argc, char* a
114112
}
115113
}
116114

117-
kprintf("Kernel process %s (%i) allocated with address at %llx, stack at %llx-%llx, heap at %llx. %i argument(s)", (uintptr_t)name, proc->id, proc->pc, proc->sp - proc->stack_size, proc->sp, proc->mm.brk, argc);
115+
ready_process(proc);
116+
kprintf("Kernel process %s (%i) allocated with address at %llx, stack at %llx-%llx, heap at %llx. %i argument(s)", (uintptr_t)name, proc->id, proc->pc, proc->sp - proc->stack_size, proc->sp, (uaddr_t)dmap_pa_to_kva(proc->heap_phys), argc);
118117
irq_restore(irq);
119118

120119
return proc;

kernel/kernel_processes/windows/launcher.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ void add_entry(string_slice name, string_slice ext, string path, package_info in
4747
}
4848

4949
uint16_t find_extension(char *path){
50-
uint16_t count = 0;
51-
while (*path && *path != '.'){ path++; count++; }
52-
return path ? count : 0;
50+
if (!path) return 0;
51+
int32_t dot = -1;
52+
for (uint16_t i = 0; path[i]; i++) if (path[i] == '.') dot = i;
53+
if (dot <= 0) return 0;
54+
return (uint16_t)dot;
5355
}
5456

5557
package_info get_pkg_info(char* info_path){
@@ -65,9 +67,12 @@ package_info get_pkg_info(char* info_path){
6567
void handle_entry(const char *directory, const char *file) {
6668
string fullpath = string_format("%s/%s",directory, (uintptr_t)file);
6769
uint16_t ext_loc = find_extension((char*)file);
70+
if (!ext_loc) {
71+
string_free(fullpath);
72+
return;
73+
}
6874
string_slice name = make_string_slice(fullpath.data, fullpath.length - strlen(file), ext_loc);
69-
uint16_t extra = ext_loc ? 1 : 0;
70-
string_slice ext = make_string_slice(name.data + ext_loc + 1, 0, strlen(file)-ext_loc-extra);
75+
string_slice ext = make_string_slice(file, ext_loc + 1, strlen(file)-ext_loc-1);
7176
if (slice_lit_match(ext,"red",true)){
7277
string pkg_info = string_concat(fullpath, string_from_literal("/package.info"));
7378
add_entry(name, ext, fullpath, get_pkg_info(pkg_info.data));
@@ -192,7 +197,7 @@ void activate_current(){
192197
active_proc->priority = PROC_PRIORITY_FULL;
193198
process_active = true;
194199
sys_set_focus(active_proc->id);
195-
active_proc->state = READY;
200+
ready_process(active_proc);
196201
kprintf("[LAUNCHER] process launched");
197202
enable_interrupt();
198203
}

kernel/memory/mm_process.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ bool mm_remove_vma(mm_struct *mm, uaddr_t start, uaddr_t end) {
6666
if (end <= m->start) return false;
6767
if (start >= m->end) continue;
6868

69-
bool track_free = m->kind != VMA_KIND_HEAP && m->kind != VMA_KIND_STACK;
69+
bool track_free = m->kind != VMA_KIND_STACK;
7070
uaddr_t free_start = 0;
7171
uaddr_t free_end = 0;
7272

@@ -150,7 +150,7 @@ uaddr_t mm_alloc_mmap(mm_struct *mm, size_t size, uint8_t prot, uint8_t kind, ui
150150
return base;
151151
}
152152

153-
uaddr_t heap_guard = mm->brk + (MM_GAP_PAGES * PAGE_SIZE);
153+
uaddr_t heap_guard = mm->mmap_bottom + (MM_GAP_PAGES * PAGE_SIZE);
154154
if (!mm->mmap_cursor) return 0;
155155
uaddr_t base = (mm->mmap_cursor - size) & ~(PAGE_SIZE - 1);
156156
if (base < heap_guard) return 0;
@@ -216,7 +216,7 @@ bool mm_try_handle_page_fault(process_t *proc, uintptr_t far, uint64_t esr) {
216216
return true;
217217
}
218218

219-
if ((m->kind == VMA_KIND_HEAP && proc->mm.rss_heap_pages >= proc->mm.cap_heap_pages) || (m->kind == VMA_KIND_ANON && proc->mm.rss_anon_pages >= proc->mm.cap_anon_pages)) return false;
219+
if (m->kind == VMA_KIND_ANON && proc->mm.rss_anon_pages >= proc->mm.cap_anon_pages) return false;
220220

221221
paddr_t phys = palloc_inner(PAGE_SIZE, MEM_PRIV_USER, MEM_RW, true, false);
222222
if (!phys) return false;
@@ -225,8 +225,7 @@ bool mm_try_handle_page_fault(process_t *proc, uintptr_t far, uint64_t esr) {
225225
mmu_map_4kb((uint64_t*)proc->mm.ttbr0, va_page, phys, MAIR_IDX_NORMAL, m->prot | MEM_NORM, MEM_PRIV_USER);
226226
mmu_flush_asid(proc->mm.asid);
227227

228-
if (m->kind == VMA_KIND_HEAP) proc->mm.rss_heap_pages++;
229-
else if (m->kind == VMA_KIND_ANON) proc->mm.rss_anon_pages++;
228+
if (m->kind == VMA_KIND_ANON) proc->mm.rss_anon_pages++;
230229

231230
return true;
232231
}

kernel/memory/mm_process.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ typedef struct process process_t;
1010
#define VMA_FLAG_ZERO 4
1111
#define VMA_FLAG_NOFREE 8
1212
#define VMA_KIND_ELF 1
13-
#define VMA_KIND_HEAP 2
14-
#define VMA_KIND_STACK 3
15-
#define VMA_KIND_ANON 4
16-
#define VMA_KIND_SPECIAL 5
13+
#define VMA_KIND_STACK 2
14+
#define VMA_KIND_ANON 3
15+
#define VMA_KIND_SPECIAL 4
1716

1817
#define MAX_VMAS 128
1918
#define MM_GAP_PAGES 16
@@ -40,18 +39,14 @@ typedef struct mm_struct {
4039
uint16_t vma_count;
4140
mm_free_range mmap_free[MAX_VMAS];
4241
uint16_t mmap_free_count;
43-
uaddr_t heap_start;
44-
uaddr_t brk;
45-
uaddr_t brk_max;
42+
uaddr_t mmap_bottom;
4643
uaddr_t mmap_top;
4744
uaddr_t mmap_cursor;
4845
uaddr_t stack_top;
4946
uaddr_t stack_limit;
5047
uaddr_t stack_commit;
51-
uint64_t rss_heap_pages;
5248
uint64_t rss_stack_pages;
5349
uint64_t rss_anon_pages;
54-
uint64_t cap_heap_pages;
5550
uint64_t cap_stack_pages;
5651
uint64_t cap_anon_pages;
5752
} mm_struct;

kernel/memory/page_allocator.c

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,14 @@
1818
#define LOW_ADDR_WARN 0x100000ULL
1919

2020
#define ALLOC_TAG_MAGIC 0xCCDEC00ED00DAA0EULL
21-
#define ALLOC_TAG_MAGIC_INV (~ALLOC_TAG_MAGIC)
22-
#define ALLOC_TAG_SIZE 64
23-
#define ALLOC_KIND_SMALL 1
21+
#define ALLOC_TAG_SIZE sizeof(alloc_tag)
2422

2523
typedef struct {
2624
uint64_t magic;
27-
uint64_t magic_inv;
2825
uint64_t base_phys;
2926
uint64_t user_phys;
30-
uint64_t owner_phys;
3127
uint32_t alloc_size;
32-
uint32_t user_size;
33-
uint16_t alignment;
34-
uint8_t kind;
35-
uint8_t level;
36-
uint8_t attributes;
37-
uint8_t reserved0;
38-
uint16_t reserved1;
3928
uint32_t checksum;
40-
uint32_t checksum_inv;
4129
} alloc_tag;
4230

4331
typedef struct {
@@ -451,25 +439,11 @@ void* kalloc_inner(void *page, size_t size, uint16_t alignment, uint8_t level, u
451439

452440
alloc_tag* tag = (alloc_tag*)PHYS_TO_VIRT(tag_phys);
453441
tag->magic = ALLOC_TAG_MAGIC;
454-
tag->magic_inv = ALLOC_TAG_MAGIC_INV;
455442
tag->base_phys = base_phys;
456443
tag->user_phys = user_phys;
457-
tag->owner_phys = owner_phys;
458444
tag->alloc_size = (uint32_t)bsz;
459-
tag->user_size = (uint32_t)req_size;
460-
tag->alignment = alignment;
461-
tag->kind = ALLOC_KIND_SMALL;
462-
tag->level = level;
463-
tag->attributes = info->attributes;
464-
tag->reserved0 = 0;
465-
tag->reserved1 = 0;
466-
467-
uint64_t mix = tag->base_phys ^ tag->user_phys ^ tag->owner_phys ^ ((uint64_t)tag->alloc_size << 32) ^ tag->user_size;
468-
mix ^= ((uint64_t)tag->alignment << 48) ^ ((uint64_t)tag->kind << 40) ^ ((uint64_t)tag->level << 32) ^ ((uint64_t)tag->attributes << 24);
469-
mix ^= ALLOC_TAG_MAGIC; //token
470-
uint32_t c = (uint32_t)(mix ^ (mix >> 32));
471-
tag->checksum = c;
472-
tag->checksum_inv = ~c;
445+
uint64_t mix = tag->base_phys ^ tag->user_phys ^ ((uint64_t)tag->alloc_size << 32) ^ ALLOC_TAG_MAGIC;
446+
tag->checksum = (uint32_t)(mix ^ (mix >> 32));
473447

474448
memset((void*)PHYS_TO_VIRT(user_phys), 0, size);
475449
info->size += bsz;
@@ -517,25 +491,11 @@ void* kalloc_inner(void *page, size_t size, uint16_t alignment, uint8_t level, u
517491

518492
alloc_tag* tag = (alloc_tag*)PHYS_TO_VIRT(tag_phys);
519493
tag->magic = ALLOC_TAG_MAGIC;
520-
tag->magic_inv = ALLOC_TAG_MAGIC_INV;
521494
tag->base_phys = base_phys;
522495
tag->user_phys = user_phys;
523-
tag->owner_phys = owner_phys;
524496
tag->alloc_size = (uint32_t)small_need;
525-
tag->user_size = (uint32_t)req_size;
526-
tag->alignment = alignment;
527-
tag->kind = ALLOC_KIND_SMALL;
528-
tag->level = level;
529-
tag->attributes = info->attributes;
530-
tag->reserved0 = 0;
531-
tag->reserved1 = 0;
532-
533-
uint64_t mix = tag->base_phys ^ tag->user_phys ^ tag->owner_phys ^ ((uint64_t)tag->alloc_size << 32) ^ tag->user_size;
534-
mix ^= ((uint64_t)tag->alignment << 48) ^ ((uint64_t)tag->kind << 40) ^ ((uint64_t)tag->level << 32) ^ ((uint64_t)tag->attributes << 24);
535-
mix ^= ALLOC_TAG_MAGIC;
536-
uint32_t c = (uint32_t)(mix ^ (mix >> 32));
537-
tag->checksum = c;
538-
tag->checksum_inv = ~c;
497+
uint64_t mix = tag->base_phys ^ tag->user_phys ^ ((uint64_t)tag->alloc_size << 32) ^ ALLOC_TAG_MAGIC;
498+
tag->checksum = (uint32_t)(mix ^ (mix >> 32));
539499

540500
memset((void*)PHYS_TO_VIRT(user_phys), 0, size);
541501
info->size += small_need;
@@ -648,22 +608,15 @@ void kfree(void* ptr, size_t size) {
648608

649609
if(phys_tag) {
650610
tag = (alloc_tag*)PHYS_TO_VIRT(phys_tag);
651-
if(tag->magic == ALLOC_TAG_MAGIC && tag->magic_inv == ALLOC_TAG_MAGIC_INV && tag->user_phys == phys){
652-
uint64_t mix = tag->base_phys ^ tag->user_phys ^ tag->owner_phys ^ ((uint64_t)tag->alloc_size << 32) ^ tag->user_size;
653-
mix ^= ((uint64_t)tag->alignment << 48) ^ ((uint64_t)tag->kind << 40) ^ ((uint64_t)tag->level << 32) ^ ((uint64_t)tag->attributes << 24);
654-
mix ^= ALLOC_TAG_MAGIC; //token
611+
if(tag->magic == ALLOC_TAG_MAGIC && tag->user_phys == phys){
612+
uint64_t mix = tag->base_phys ^ tag->user_phys ^ ((uint64_t)tag->alloc_size << 32) ^ ALLOC_TAG_MAGIC;
655613
uint32_t c = (uint32_t)(mix ^ (mix >> 32));
656-
if(tag->checksum == c && tag->checksum_inv == ~c) tag_ok = true;
614+
if(tag->checksum == c) tag_ok = true;
657615
}
658616
}
659617
}
660618

661619
if(tag_ok) {
662-
if(tag->kind != ALLOC_KIND_SMALL) {
663-
kprintf("[kfree] bad tag kind ptr=%llx phys=%llx kind=%u size=%llx", (uint64_t)va, (uint64_t)phys, (unsigned)tag->kind, (uint64_t)size);
664-
panic("kfree bad tag kind", va);
665-
}
666-
667620
uintptr_t base_phys = (uintptr_t)tag->base_phys;
668621
uint64_t alloc_size = tag->alloc_size;
669622

kernel/process/debug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ void debug_load(){
2323

2424
kprintf("[DEBUG] Reading debug info from %x",file);
2525

26-
get_elf_debug_info(get_proc_by_pid(0), file, fd.size);
26+
get_elf_debug_info(get_kernel_proc(), file, fd.size);
2727

28-
kprintf("[DEBUG] .debug_line %x .debug_line_str %x", get_proc_by_pid(0)->debug_lines.ptr, get_proc_by_pid(0)->debug_line_str.ptr);
28+
kprintf("[DEBUG] .debug_line %x .debug_line_str %x", get_kernel_proc()->debug_lines.ptr, get_kernel_proc()->debug_line_str.ptr);
2929

3030
}

kernel/process/kernel_syscall_impl.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
extern page_index *p_index;
1818

1919
void* malloc(size_t size){
20-
process_t* k = get_proc_by_pid(1);
20+
process_t* k = get_kernel_proc();
2121
if (!k) return 0;
2222

23-
int tr = 0;
24-
paddr_t heap_pa = mmu_translate(0, k->mm.brk, &tr);
25-
if (tr) return 0;
23+
if (!k->heap_phys) return 0;
2624

27-
void* ptr = kalloc((void*)dmap_pa_to_kva(heap_pa), size, ALIGN_16B, MEM_PRIV_KERNEL);
25+
void* ptr = kalloc((void*)dmap_pa_to_kva(k->heap_phys), size, ALIGN_16B, MEM_PRIV_KERNEL);
2826
if (ptr && size >= PAGE_SIZE && k->alloc_map)
2927
register_allocation(k->alloc_map, ptr, size);
3028
return ptr;
@@ -36,7 +34,7 @@ void free_sized(void*ptr, size_t size){
3634

3735
void* page_alloc(size_t size){
3836
if (!size) return 0;
39-
process_t* k = get_proc_by_pid(1);//TODO: can we make this more fragmented? This inside a syscall, current proc outside
37+
process_t* k = get_kernel_proc();//TODO: can we make this more fragmented? This inside a syscall, current proc outside
4038
void *ptr = palloc(size, MEM_PRIV_KERNEL, MEM_RW | MEM_NORM, true);
4139
if (k && k->alloc_map && ptr) register_allocation(k->alloc_map, ptr, size);
4240
return ptr;
@@ -45,7 +43,7 @@ void* page_alloc(size_t size){
4543
void page_free(void *ptr){
4644
if (!ptr) return;
4745
if (((uintptr_t)ptr & (PAGE_SIZE - 1)) != 0) return;
48-
process_t* k = get_proc_by_pid(1);//TODO: can we make this more fragmented? This inside a syscall, current proc outside
46+
process_t* k = get_kernel_proc();//TODO: can we make this more fragmented? This inside a syscall, current proc outside
4947
if (k && k->alloc_map) {
5048
free_registered(k->alloc_map, ptr);
5149
return;

kernel/process/loading/elf_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ process_t* load_elf_process_path(const char *name, const char *bundle, const cha
228228
return 0;
229229
}
230230

231-
proc->state = READY;
231+
ready_process(proc);
232232
return proc;
233233
}
234234

0 commit comments

Comments
 (0)