Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
kalloc.c File Reference
#include "libk/serial.h"
#include "memory/memory_utils.h"
#include "memory/phys_base_allocator.h"
#include "memory/vm_manager.h"
#include <autoconf.h>
#include <hal/cpu/core.h>
#include <hal/cpu/irq_lock.h>
#include <hal/cpu/paging.h>
#include <memory/kalloc.h>
#include <spinlock.h>
#include <str.h>
#include <type.h>

Go to the source code of this file.

Data Structures

struct  freed_t
 
struct  kalloc_cpu_cache
 

Macros

#define MAX_FREED_VADDRS   512
 
#define KALLOC_REFILL(BUCKET)
 
#define KALLOC_SLAB_ALLOC(BUCKET)
 

Functions

struct kalloc_cpu_cache __attribute__ ((aligned(64)))
 
static uintptr_t lock_irqsave (spinlock_t *lk)
 
static void unlock_irqrestore (spinlock_t *lk, uintptr_t flags)
 
static void setup_redzone (void *ptr, size_t size)
 
static int check_redzone (void *ptr, size_t size)
 
static uintptr_t vaddr_alloc_locked (size_t page_count)
 
static void * alloc_page_locked (void)
 
static void refill_64 (struct kalloc_cpu_cache *cc, uintptr_t *gflags)
 
static void refill_128 (struct kalloc_cpu_cache *cc, uintptr_t *gflags)
 
static void refill_256 (struct kalloc_cpu_cache *cc, uintptr_t *gflags)
 
static void refill_512 (struct kalloc_cpu_cache *cc, uintptr_t *gflags)
 
static void refill_1024 (struct kalloc_cpu_cache *cc, uintptr_t *gflags)
 
static void refill_2048 (struct kalloc_cpu_cache *cc, uintptr_t *gflags)
 
 __attribute__ ((used, visibility("default"), section(".export")))
 This function handles memory allocation for a new dentry using the slab allocator.
 

Variables

void * c_64
 
void * c_128
 
void * c_256
 
void * c_512
 
void * c_1024
 
void * c_2048
 
size_t c_64_count
 
size_t c_128_count
 
size_t c_256_count
 
size_t c_512_count
 
size_t c_1024_count
 
size_t c_2048_count
 
spinlock_t lock
 
uint8_t _pad [64 -(sizeof(void *) *6+sizeof(size_t) *6+sizeof(spinlock_t)) % 64]
 
static struct kalloc_cpu_cache cpu_caches [VOXIA_MAX_CORE]
 
static spinlock_t kalloc_global_lock = {0}
 
static uintptr_t kalloc_next_addr = KALLOC_BASE_ADDR
 
static freed_t freed_vaddrs [512]
 
static size_t freed_vaddr_count = 0
 

Macro Definition Documentation

◆ KALLOC_REFILL

#define KALLOC_REFILL ( BUCKET)
Value:
static void refill_##BUCKET(struct kalloc_cpu_cache* cc, \
uintptr_t* gflags) { \
\
spin_acquire(&kalloc_global_lock); \
void* page = alloc_page_locked(); \
spin_release(&kalloc_global_lock); \
\
for (int i = 0; i < (int)(BLOCK_SIZE / BUCKET); i++) { \
void* slot = \
(void*)((uintptr_t)page + (size_t)i * BUCKET); \
*(void**)slot = cc->c_##BUCKET; \
cc->c_##BUCKET = slot; \
cc->c_##BUCKET##_count++; \
} \
(void)gflags; /* tidak dipakai di sini, tapi perlu untuk macro \
*/ \
}
static void * alloc_page_locked(void)
Definition kalloc.c:99
static spinlock_t kalloc_global_lock
Definition kalloc.c:48
uintptr_t page
Definition paging.c:0
#define BLOCK_SIZE
unsigned long uintptr_t
Definition type.h:73
struct xhci_slot_ctx slot
Definition xhci.hpp:0

Definition at line 108 of file kalloc.c.

Referenced by refill_1024(), refill_2048(), refill_256(), and refill_512().

◆ KALLOC_SLAB_ALLOC

#define KALLOC_SLAB_ALLOC ( BUCKET)
Value:
do { \
uint32_t cpu = get_current_core_data()->core_id; \
struct kalloc_cpu_cache* cc = &cpu_caches[cpu]; \
if (cc->c_##BUCKET##_count == 0) \
refill_##BUCKET(cc, &flags); \
void* slot = cc->c_##BUCKET; \
cc->c_##BUCKET = *(void**)slot; \
cc->c_##BUCKET##_count--; \
unlock_irqrestore(&cc->lock, flags); \
\
meta->size = size; \
\
void* red_before = \
(void*)((uintptr_t)slot + sizeof(kalloc_metadata_t)); \
setup_redzone(red_before, KALLOC_REDZONE_SIZE); \
\
void* data = \
(void*)((uintptr_t)red_before + KALLOC_REDZONE_SIZE); \
void* red_after = (void*)((uintptr_t)data + size); \
setup_redzone(red_after, KALLOC_REDZONE_SIZE); \
\
return data; \
} while (0)
each_core_data * get_current_core_data(void)
Definition core.c:54
struct fs_data data
Definition filesystem.h:1
uint16_t flags
Definition thread.h:5
static struct kalloc_cpu_cache cpu_caches[VOXIA_MAX_CORE]
Definition kalloc.c:46
static uintptr_t lock_irqsave(spinlock_t *lk)
Definition kalloc.c:55
#define KALLOC_REDZONE_SIZE
Definition kalloc.h:10
#define KALLOC_REDZONE_MAGIC
Definition kalloc.h:11
spinlock_t lock
Definition kalloc.c:38
uint32_t magic
Definition kalloc.h:20
unsigned int uint32_t
Definition type.h:19
size_t size
Definition vnode.h:3

Definition at line 133 of file kalloc.c.

Referenced by __attribute__().

◆ MAX_FREED_VADDRS

#define MAX_FREED_VADDRS   512

Definition at line 14 of file kalloc.c.

Referenced by push_freed_vaddr().

Function Documentation

◆ __attribute__() [1/2]

struct kalloc_cpu_cache __attribute__ ( (aligned(64)) )

Definition at line 1 of file core.h.

◆ __attribute__() [2/2]

__attribute__ ( (used, visibility("default"), section(".export")) )

This function handles memory allocation for a new dentry using the slab allocator.

It also links the dentry to its corresponding VNode (if provided).

Note
This function performs lazy initialization of the dentry slab cache if it does not exist yet.
Parameters
nameThe name of the file or directory.
vnodePointer to the associated VNode (inode data). Pass NULL if the VNode is not yet available.
Returns
dentry_ptr A pointer to the newly allocated and initialized dentry.

Definition at line 161 of file kalloc.c.

References ALIGN_UP, BLOCK_SIZE, data, get_kernel_vmm_page(), kalloc_global_lock, KALLOC_REDZONE_MAGIC, KALLOC_REDZONE_SIZE, KALLOC_SLAB_ALLOC, KERNEL_API, lock_irqsave(), kalloc_metadata_t::magic, NULL, PAGE_PRESENT, PAGE_USER, PAGE_WRITABLE, paging_get_highest_page_map(), phys_base_alloc(), setup_redzone(), kalloc_metadata_t::size, size, unlock_irqrestore(), vaddr_alloc_locked(), vma_register(), and vxMultipleMmap().

◆ alloc_page_locked()

static void * alloc_page_locked ( void )
static

◆ check_redzone()

static int check_redzone ( void * ptr,
size_t size )
static

Definition at line 72 of file kalloc.c.

References KALLOC_REDZONE_MAGIC, ptr, and size.

◆ lock_irqsave()

static uintptr_t lock_irqsave ( spinlock_t * lk)
inlinestatic

Definition at line 55 of file kalloc.c.

References flags, irq_save(), and spin_acquire().

Referenced by __attribute__().

◆ refill_1024()

static void refill_1024 ( struct kalloc_cpu_cache * cc,
uintptr_t * gflags )
static

Definition at line 130 of file kalloc.c.

References KALLOC_REFILL.

◆ refill_128()

static void refill_128 ( struct kalloc_cpu_cache * cc,
uintptr_t * gflags )
static

Definition at line 127 of file kalloc.c.

◆ refill_2048()

static void refill_2048 ( struct kalloc_cpu_cache * cc,
uintptr_t * gflags )
static

Definition at line 131 of file kalloc.c.

References KALLOC_REFILL.

◆ refill_256()

static void refill_256 ( struct kalloc_cpu_cache * cc,
uintptr_t * gflags )
static

Definition at line 128 of file kalloc.c.

References KALLOC_REFILL.

◆ refill_512()

static void refill_512 ( struct kalloc_cpu_cache * cc,
uintptr_t * gflags )
static

Definition at line 129 of file kalloc.c.

References KALLOC_REFILL.

◆ refill_64()

static void refill_64 ( struct kalloc_cpu_cache * cc,
uintptr_t * gflags )
static

Definition at line 126 of file kalloc.c.

◆ setup_redzone()

static void setup_redzone ( void * ptr,
size_t size )
static

Definition at line 66 of file kalloc.c.

References KALLOC_REDZONE_MAGIC, ptr, and size.

Referenced by __attribute__().

◆ unlock_irqrestore()

static void unlock_irqrestore ( spinlock_t * lk,
uintptr_t flags )
inlinestatic

Definition at line 61 of file kalloc.c.

References flags, irq_restore(), and spin_release().

Referenced by __attribute__().

◆ vaddr_alloc_locked()

static uintptr_t vaddr_alloc_locked ( size_t page_count)
static

Definition at line 81 of file kalloc.c.

References BLOCK_SIZE, freed_vaddr_count, freed_vaddrs, kalloc_next_addr, and size.

Referenced by __attribute__(), and alloc_page_locked().

Variable Documentation

◆ _pad

uint8_t _pad[64 -(sizeof(void *) *6+sizeof(size_t) *6+ sizeof(spinlock_t)) % 64]

Definition at line 19 of file kalloc.c.

Referenced by __attribute__().

◆ c_1024

void* c_1024

Definition at line 5 of file kalloc.c.

◆ c_1024_count

size_t c_1024_count

Definition at line 13 of file kalloc.c.

◆ c_128

void* c_128

Definition at line 2 of file kalloc.c.

◆ c_128_count

size_t c_128_count

Definition at line 10 of file kalloc.c.

◆ c_2048

void* c_2048

Definition at line 6 of file kalloc.c.

◆ c_2048_count

size_t c_2048_count

Definition at line 14 of file kalloc.c.

◆ c_256

void* c_256

Definition at line 3 of file kalloc.c.

◆ c_256_count

size_t c_256_count

Definition at line 11 of file kalloc.c.

◆ c_512

void* c_512

Definition at line 4 of file kalloc.c.

◆ c_512_count

size_t c_512_count

Definition at line 12 of file kalloc.c.

◆ c_64

void* c_64

Definition at line 1 of file kalloc.c.

◆ c_64_count

size_t c_64_count

Definition at line 9 of file kalloc.c.

◆ cpu_caches

struct kalloc_cpu_cache cpu_caches[VOXIA_MAX_CORE]
static

Definition at line 46 of file kalloc.c.

◆ freed_vaddr_count

size_t freed_vaddr_count = 0
static

Definition at line 53 of file kalloc.c.

Referenced by get_default_slab_addr(), push_freed_vaddr(), and vaddr_alloc_locked().

◆ freed_vaddrs

freed_t freed_vaddrs[512]
static

Definition at line 52 of file kalloc.c.

Referenced by get_default_slab_addr(), push_freed_vaddr(), and vaddr_alloc_locked().

◆ kalloc_global_lock

spinlock_t kalloc_global_lock = {0}
static

Definition at line 48 of file kalloc.c.

Referenced by __attribute__().

◆ kalloc_next_addr

uintptr_t kalloc_next_addr = KALLOC_BASE_ADDR
static

Definition at line 50 of file kalloc.c.

Referenced by vaddr_alloc_locked().

◆ lock

spinlock_t lock

Definition at line 16 of file kalloc.c.