From 236963e7563be3e3f8220dac7bb4af446928e194 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 24 Jun 2024 22:55:29 -0400 Subject: Clean out for expt Signed-off-by: Ian Moffett --- sys/vm/tlsf.c | 1264 -------------------------------------------------- sys/vm/vm_device.c | 119 ----- sys/vm/vm_dynalloc.c | 93 ---- sys/vm/vm_fault.c | 132 ------ sys/vm/vm_init.c | 91 ---- sys/vm/vm_map.c | 540 --------------------- sys/vm/vm_obj.c | 93 ---- sys/vm/vm_page.c | 73 --- sys/vm/vm_pager.c | 68 --- sys/vm/vm_physseg.c | 272 ----------- sys/vm/vm_stat.c | 42 -- sys/vm/vm_vnode.c | 114 ----- 12 files changed, 2901 deletions(-) delete mode 100644 sys/vm/tlsf.c delete mode 100644 sys/vm/vm_device.c delete mode 100644 sys/vm/vm_dynalloc.c delete mode 100644 sys/vm/vm_fault.c delete mode 100644 sys/vm/vm_init.c delete mode 100644 sys/vm/vm_map.c delete mode 100644 sys/vm/vm_obj.c delete mode 100644 sys/vm/vm_page.c delete mode 100644 sys/vm/vm_pager.c delete mode 100644 sys/vm/vm_physseg.c delete mode 100644 sys/vm/vm_stat.c delete mode 100644 sys/vm/vm_vnode.c (limited to 'sys/vm') diff --git a/sys/vm/tlsf.c b/sys/vm/tlsf.c deleted file mode 100644 index d4a6ddf..0000000 --- a/sys/vm/tlsf.c +++ /dev/null @@ -1,1264 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define printf kprintf - -#if defined(__cplusplus) -#define tlsf_decl inline -#else -#define tlsf_decl static -#endif - -/* -** Architecture-specific bit manipulation routines. -** -** TLSF achieves O(1) cost for malloc and free operations by limiting -** the search for a free block to a free list of guaranteed size -** adequate to fulfill the request, combined with efficient free list -** queries using bitmasks and architecture-specific bit-manipulation -** routines. -** -** Most modern processors provide instructions to count leading zeroes -** in a word, find the lowest and highest set bit, etc. These -** specific implementations will be used when available, falling back -** to a reasonably efficient generic implementation. -** -** NOTE: TLSF spec relies on ffs/fls returning value 0..31. -** ffs/fls return 1-32 by default, returning 0 for error. -*/ - -/* -** Detect whether or not we are building for a 32- or 64-bit (LP/LLP) -** architecture. There is no reliable portable method at compile-time. -*/ -#if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) \ - || defined (_WIN64) || defined (__LP64__) || defined (__LLP64__) -#define TLSF_64BIT -#endif - -/* -** gcc 3.4 and above have builtin support, specialized for architecture. -** Some compilers masquerade as gcc; patchlevel test filters them out. -*/ -#if defined (__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \ - && defined (__GNUC_PATCHLEVEL__) - -#if defined (__SNC__) -/* SNC for Playstation 3. */ - -tlsf_decl int tlsf_ffs(unsigned int word) -{ - const unsigned int reverse = word & (~word + 1); - const int bit = 32 - __builtin_clz(reverse); - return bit - 1; -} - -#else - -tlsf_decl int tlsf_ffs(unsigned int word) -{ - return __builtin_ffs(word) - 1; -} - -#endif - -tlsf_decl int tlsf_fls(unsigned int word) -{ - const int bit = word ? 32 - __builtin_clz(word) : 0; - return bit - 1; -} - -#elif defined (_MSC_VER) && (_MSC_VER >= 1400) && (defined (_M_IX86) || defined (_M_X64)) -/* Microsoft Visual C++ support on x86/X64 architectures. */ - -#include - -#pragma intrinsic(_BitScanReverse) -#pragma intrinsic(_BitScanForward) - -tlsf_decl int tlsf_fls(unsigned int word) -{ - unsigned long index; - return _BitScanReverse(&index, word) ? index : -1; -} - -tlsf_decl int tlsf_ffs(unsigned int word) -{ - unsigned long index; - return _BitScanForward(&index, word) ? index : -1; -} - -#elif defined (_MSC_VER) && defined (_M_PPC) -/* Microsoft Visual C++ support on PowerPC architectures. */ - -#include - -tlsf_decl int tlsf_fls(unsigned int word) -{ - const int bit = 32 - _CountLeadingZeros(word); - return bit - 1; -} - -tlsf_decl int tlsf_ffs(unsigned int word) -{ - const unsigned int reverse = word & (~word + 1); - const int bit = 32 - _CountLeadingZeros(reverse); - return bit - 1; -} - -#elif defined (__ARMCC_VERSION) -/* RealView Compilation Tools for ARM */ - -tlsf_decl int tlsf_ffs(unsigned int word) -{ - const unsigned int reverse = word & (~word + 1); - const int bit = 32 - __clz(reverse); - return bit - 1; -} - -tlsf_decl int tlsf_fls(unsigned int word) -{ - const int bit = word ? 32 - __clz(word) : 0; - return bit - 1; -} - -#elif defined (__ghs__) -/* Green Hills support for PowerPC */ - -#include - -tlsf_decl int tlsf_ffs(unsigned int word) -{ - const unsigned int reverse = word & (~word + 1); - const int bit = 32 - __CLZ32(reverse); - return bit - 1; -} - -tlsf_decl int tlsf_fls(unsigned int word) -{ - const int bit = word ? 32 - __CLZ32(word) : 0; - return bit - 1; -} - -#else -/* Fall back to generic implementation. */ - -tlsf_decl int tlsf_fls_generic(unsigned int word) -{ - int bit = 32; - - if (!word) bit -= 1; - if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; } - if (!(word & 0xff000000)) { word <<= 8; bit -= 8; } - if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; } - if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; } - if (!(word & 0x80000000)) { word <<= 1; bit -= 1; } - - return bit; -} - -/* Implement ffs in terms of fls. */ -tlsf_decl int tlsf_ffs(unsigned int word) -{ - return tlsf_fls_generic(word & (~word + 1)) - 1; -} - -tlsf_decl int tlsf_fls(unsigned int word) -{ - return tlsf_fls_generic(word) - 1; -} - -#endif - -/* Possibly 64-bit version of tlsf_fls. */ -#if defined (TLSF_64BIT) -tlsf_decl int tlsf_fls_sizet(size_t size) -{ - int high = (int)(size >> 32); - int bits = 0; - if (high) - { - bits = 32 + tlsf_fls(high); - } - else - { - bits = tlsf_fls((int)size & 0xffffffff); - - } - return bits; -} -#else -#define tlsf_fls_sizet tlsf_fls -#endif - -#undef tlsf_decl - -/* -** Constants. -*/ - -/* Public constants: may be modified. */ -enum tlsf_public -{ - /* log2 of number of linear subdivisions of block sizes. Larger - ** values require more memory in the control structure. Values of - ** 4 or 5 are typical. - */ - SL_INDEX_COUNT_LOG2 = 5, -}; - -/* Private constants: do not modify. */ -enum tlsf_private -{ -#if defined (TLSF_64BIT) - /* All allocation sizes and addresses are aligned to 8 bytes. */ - ALIGN_SIZE_LOG2 = 3, -#else - /* All allocation sizes and addresses are aligned to 4 bytes. */ - ALIGN_SIZE_LOG2 = 2, -#endif - ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2), - - /* - ** We support allocations of sizes up to (1 << FL_INDEX_MAX) bits. - ** However, because we linearly subdivide the second-level lists, and - ** our minimum size granularity is 4 bytes, it doesn't make sense to - ** create first-level lists for sizes smaller than SL_INDEX_COUNT * 4, - ** or (1 << (SL_INDEX_COUNT_LOG2 + 2)) bytes, as there we will be - ** trying to split size ranges into more slots than we have available. - ** Instead, we calculate the minimum threshold size, and place all - ** blocks below that size into the 0th first-level list. - */ - -#if defined (TLSF_64BIT) - /* - ** TODO: We can increase this to support larger sizes, at the expense - ** of more overhead in the TLSF structure. - */ - FL_INDEX_MAX = 32, -#else - FL_INDEX_MAX = 30, -#endif - SL_INDEX_COUNT = (1 << SL_INDEX_COUNT_LOG2), - FL_INDEX_SHIFT = (SL_INDEX_COUNT_LOG2 + ALIGN_SIZE_LOG2), - FL_INDEX_COUNT = (FL_INDEX_MAX - FL_INDEX_SHIFT + 1), - - SMALL_BLOCK_SIZE = (1 << FL_INDEX_SHIFT), -}; - -/* -** Cast and min/max macros. -*/ - -#define tlsf_cast(t, exp) ((t) (exp)) -#define tlsf_min(a, b) ((a) < (b) ? (a) : (b)) -#define tlsf_max(a, b) ((a) > (b) ? (a) : (b)) - -/* -** Set assert macro, if it has not been provided by the user. -*/ -#if !defined (tlsf_assert) -#define tlsf_assert __assert -#endif - -/* -** Static assertion mechanism. -*/ - -#define _tlsf_glue2(x, y) x ## y -#define _tlsf_glue(x, y) _tlsf_glue2(x, y) -#define tlsf_static_assert(exp) \ - typedef char _tlsf_glue(static_assert, __LINE__) [(exp) ? 1 : -1] - -/* This code has been tested on 32- and 64-bit (LP/LLP) architectures. */ -tlsf_static_assert(sizeof(int) * CHAR_BIT == 32); -tlsf_static_assert(sizeof(size_t) * CHAR_BIT >= 32); -tlsf_static_assert(sizeof(size_t) * CHAR_BIT <= 64); - -/* SL_INDEX_COUNT must be <= number of bits in sl_bitmap's storage type. */ -tlsf_static_assert(sizeof(unsigned int) * CHAR_BIT >= SL_INDEX_COUNT); - -/* Ensure we've properly tuned our sizes. */ -tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT); - -/* -** Data structures and associated constants. -*/ - -/* -** Block header structure. -** -** There are several implementation subtleties involved: -** - The prev_phys_block field is only valid if the previous block is free. -** - The prev_phys_block field is actually stored at the end of the -** previous block. It appears at the beginning of this structure only to -** simplify the implementation. -** - The next_free / prev_free fields are only valid if the block is free. -*/ -typedef struct block_header_t -{ - /* Points to the previous physical block. */ - struct block_header_t* prev_phys_block; - - /* The size of this block, excluding the block header. */ - size_t size; - - /* Next and previous free blocks. */ - struct block_header_t* next_free; - struct block_header_t* prev_free; -} block_header_t; - -/* -** Since block sizes are always at least a multiple of 4, the two least -** significant bits of the size field are used to store the block status: -** - bit 0: whether block is busy or free -** - bit 1: whether previous block is busy or free -*/ -static const size_t block_header_free_bit = 1 << 0; -static const size_t block_header_prev_free_bit = 1 << 1; - -/* -** The size of the block header exposed to used blocks is the size field. -** The prev_phys_block field is stored *inside* the previous free block. -*/ -static const size_t block_header_overhead = sizeof(size_t); - -/* User data starts directly after the size field in a used block. */ -static const size_t block_start_offset = - offsetof(block_header_t, size) + sizeof(size_t); - -/* -** A free block must be large enough to store its header minus the size of -** the prev_phys_block field, and no larger than the number of addressable -** bits for FL_INDEX. -*/ -static const size_t block_size_min = - sizeof(block_header_t) - sizeof(block_header_t*); -static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX; - - -/* The TLSF control structure. */ -typedef struct control_t -{ - /* Empty lists point at this block to indicate they are free. */ - block_header_t block_null; - - /* Bitmaps for free lists. */ - unsigned int fl_bitmap; - unsigned int sl_bitmap[FL_INDEX_COUNT]; - - /* Head of free lists. */ - block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT]; -} control_t; - -/* A type used for casting when doing pointer arithmetic. */ -typedef ptrdiff_t tlsfptr_t; - -/* -** block_header_t member functions. -*/ - -static size_t block_size(const block_header_t* block) -{ - return block->size & ~(block_header_free_bit | block_header_prev_free_bit); -} - -static void block_set_size(block_header_t* block, size_t size) -{ - const size_t oldsize = block->size; - block->size = size | (oldsize & (block_header_free_bit | block_header_prev_free_bit)); -} - -static int block_is_last(const block_header_t* block) -{ - return block_size(block) == 0; -} - -static int block_is_free(const block_header_t* block) -{ - return tlsf_cast(int, block->size & block_header_free_bit); -} - -static void block_set_free(block_header_t* block) -{ - block->size |= block_header_free_bit; -} - -static void block_set_used(block_header_t* block) -{ - block->size &= ~block_header_free_bit; -} - -static int block_is_prev_free(const block_header_t* block) -{ - return tlsf_cast(int, block->size & block_header_prev_free_bit); -} - -static void block_set_prev_free(block_header_t* block) -{ - block->size |= block_header_prev_free_bit; -} - -static void block_set_prev_used(block_header_t* block) -{ - block->size &= ~block_header_prev_free_bit; -} - -static block_header_t* block_from_ptr(const void* ptr) -{ - return tlsf_cast(block_header_t*, - tlsf_cast(unsigned char*, ptr) - block_start_offset); -} - -static void* block_to_ptr(const block_header_t* block) -{ - return tlsf_cast(void*, - tlsf_cast(unsigned char*, block) + block_start_offset); -} - -/* Return location of next block after block of given size. */ -static block_header_t* offset_to_block(const void* ptr, size_t size) -{ - return tlsf_cast(block_header_t*, tlsf_cast(tlsfptr_t, ptr) + size); -} - -/* Return location of previous block. */ -static block_header_t* block_prev(const block_header_t* block) -{ - tlsf_assert(block_is_prev_free(block) && "previous block must be free"); - return block->prev_phys_block; -} - -/* Return location of next existing block. */ -static block_header_t* block_next(const block_header_t* block) -{ - block_header_t* next = offset_to_block(block_to_ptr(block), - block_size(block) - block_header_overhead); - tlsf_assert(!block_is_last(block)); - return next; -} - -/* Link a new block with its physical neighbor, return the neighbor. */ -static block_header_t* block_link_next(block_header_t* block) -{ - block_header_t* next = block_next(block); - next->prev_phys_block = block; - return next; -} - -static void block_mark_as_free(block_header_t* block) -{ - /* Link the block to the next block, first. */ - block_header_t* next = block_link_next(block); - block_set_prev_free(next); - block_set_free(block); -} - -static void block_mark_as_used(block_header_t* block) -{ - block_header_t* next = block_next(block); - block_set_prev_used(next); - block_set_used(block); -} - -static size_t align_up(size_t x, size_t align) -{ - tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); - return (x + (align - 1)) & ~(align - 1); -} - -static size_t align_down(size_t x, size_t align) -{ - tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); - return x - (x & (align - 1)); -} - -static void* align_ptr(const void* ptr, size_t align) -{ - const tlsfptr_t aligned = - (tlsf_cast(tlsfptr_t, ptr) + (align - 1)) & ~(align - 1); - tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); - return tlsf_cast(void*, aligned); -} - -/* -** Adjust an allocation size to be aligned to word size, and no smaller -** than internal minimum. -*/ -static size_t adjust_request_size(size_t size, size_t align) -{ - size_t adjust = 0; - if (size) - { - const size_t aligned = align_up(size, align); - - /* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */ - if (aligned < block_size_max) - { - adjust = tlsf_max(aligned, block_size_min); - } - } - return adjust; -} - -/* -** TLSF utility functions. In most cases, these are direct translations of -** the documentation found in the white paper. -*/ - -static void mapping_insert(size_t size, int* fli, int* sli) -{ - int fl, sl; - if (size < SMALL_BLOCK_SIZE) - { - /* Store small blocks in first list. */ - fl = 0; - sl = tlsf_cast(int, size) / (SMALL_BLOCK_SIZE / SL_INDEX_COUNT); - } - else - { - fl = tlsf_fls_sizet(size); - sl = tlsf_cast(int, size >> (fl - SL_INDEX_COUNT_LOG2)) ^ (1 << SL_INDEX_COUNT_LOG2); - fl -= (FL_INDEX_SHIFT - 1); - } - *fli = fl; - *sli = sl; -} - -/* This version rounds up to the next block size (for allocations) */ -static void mapping_search(size_t size, int* fli, int* sli) -{ - if (size >= SMALL_BLOCK_SIZE) - { - const size_t round = (1 << (tlsf_fls_sizet(size) - SL_INDEX_COUNT_LOG2)) - 1; - size += round; - } - mapping_insert(size, fli, sli); -} - -static block_header_t* search_suitable_block(control_t* control, int* fli, int* sli) -{ - int fl = *fli; - int sl = *sli; - - /* - ** First, search for a block in the list associated with the given - ** fl/sl index. - */ - unsigned int sl_map = control->sl_bitmap[fl] & (~0U << sl); - if (!sl_map) - { - /* No block exists. Search in the next largest first-level list. */ - const unsigned int fl_map = control->fl_bitmap & (~0U << (fl + 1)); - if (!fl_map) - { - /* No free blocks available, memory has been exhausted. */ - return 0; - } - - fl = tlsf_ffs(fl_map); - *fli = fl; - sl_map = control->sl_bitmap[fl]; - } - tlsf_assert(sl_map && "internal error - second level bitmap is null"); - sl = tlsf_ffs(sl_map); - *sli = sl; - - /* Return the first block in the free list. */ - return control->blocks[fl][sl]; -} - -/* Remove a free block from the free list.*/ -static void remove_free_block(control_t* control, block_header_t* block, int fl, int sl) -{ - block_header_t* prev = block->prev_free; - block_header_t* next = block->next_free; - tlsf_assert(prev && "prev_free field can not be null"); - tlsf_assert(next && "next_free field can not be null"); - next->prev_free = prev; - prev->next_free = next; - - /* If this block is the head of the free list, set new head. */ - if (control->blocks[fl][sl] == block) - { - control->blocks[fl][sl] = next; - - /* If the new head is null, clear the bitmap. */ - if (next == &control->block_null) - { - control->sl_bitmap[fl] &= ~(1U << sl); - - /* If the second bitmap is now empty, clear the fl bitmap. */ - if (!control->sl_bitmap[fl]) - { - control->fl_bitmap &= ~(1U << fl); - } - } - } -} - -/* Insert a free block into the free block list. */ -static void insert_free_block(control_t* control, block_header_t* block, int fl, int sl) -{ - block_header_t* current = control->blocks[fl][sl]; - tlsf_assert(current && "free list cannot have a null entry"); - tlsf_assert(block && "cannot insert a null entry into the free list"); - block->next_free = current; - block->prev_free = &control->block_null; - current->prev_free = block; - - tlsf_assert((uintptr_t)block_to_ptr(block) == (uintptr_t)align_ptr(block_to_ptr(block), ALIGN_SIZE) - && "block not aligned properly"); - /* - ** Insert the new block at the head of the list, and mark the first- - ** and second-level bitmaps appropriately. - */ - control->blocks[fl][sl] = block; - control->fl_bitmap |= (1U << fl); - control->sl_bitmap[fl] |= (1U << sl); -} - -/* Remove a given block from the free list. */ -static void block_remove(control_t* control, block_header_t* block) -{ - int fl, sl; - mapping_insert(block_size(block), &fl, &sl); - remove_free_block(control, block, fl, sl); -} - -/* Insert a given block into the free list. */ -static void block_insert(control_t* control, block_header_t* block) -{ - int fl, sl; - mapping_insert(block_size(block), &fl, &sl); - insert_free_block(control, block, fl, sl); -} - -static int block_can_split(block_header_t* block, size_t size) -{ - return block_size(block) >= sizeof(block_header_t) + size; -} - -/* Split a block into two, the second of which is free. */ -static block_header_t* block_split(block_header_t* block, size_t size) -{ - /* Calculate the amount of space left in the remaining block. */ - block_header_t* remaining = - offset_to_block(block_to_ptr(block), size - block_header_overhead); - - const size_t remain_size = block_size(block) - (size + block_header_overhead); - - tlsf_assert((uintptr_t)block_to_ptr(remaining) == (uintptr_t)align_ptr(block_to_ptr(remaining), ALIGN_SIZE) - && "remaining block not aligned properly"); - - tlsf_assert(block_size(block) == remain_size + size + block_header_overhead); - block_set_size(remaining, remain_size); - tlsf_assert(block_size(remaining) >= block_size_min && "block split with invalid size"); - - block_set_size(block, size); - block_mark_as_free(remaining); - - return remaining; -} - -/* Absorb a free block's storage into an adjacent previous free block. */ -static block_header_t* block_absorb(block_header_t* prev, block_header_t* block) -{ - tlsf_assert(!block_is_last(prev) && "previous block can't be last"); - /* Note: Leaves flags untouched. */ - prev->size += block_size(block) + block_header_overhead; - block_link_next(prev); - return prev; -} - -/* Merge a just-freed block with an adjacent previous free block. */ -static block_header_t* block_merge_prev(control_t* control, block_header_t* block) -{ - if (block_is_prev_free(block)) - { - block_header_t* prev = block_prev(block); - tlsf_assert(prev && "prev physical block can't be null"); - tlsf_assert(block_is_free(prev) && "prev block is not free though marked as such"); - block_remove(control, prev); - block = block_absorb(prev, block); - } - - return block; -} - -/* Merge a just-freed block with an adjacent free block. */ -static block_header_t* block_merge_next(control_t* control, block_header_t* block) -{ - block_header_t* next = block_next(block); - tlsf_assert(next && "next physical block can't be null"); - - if (block_is_free(next)) - { - tlsf_assert(!block_is_last(block) && "previous block can't be last"); - block_remove(control, next); - block = block_absorb(block, next); - } - - return block; -} - -/* Trim any trailing block space off the end of a block, return to pool. */ -static void block_trim_free(control_t* control, block_header_t* block, size_t size) -{ - tlsf_assert(block_is_free(block) && "block must be free"); - if (block_can_split(block, size)) - { - block_header_t* remaining_block = block_split(block, size); - block_link_next(block); - block_set_prev_free(remaining_block); - block_insert(control, remaining_block); - } -} - -/* Trim any trailing block space off the end of a used block, return to pool. */ -static void block_trim_used(control_t* control, block_header_t* block, size_t size) -{ - tlsf_assert(!block_is_free(block) && "block must be used"); - if (block_can_split(block, size)) - { - /* If the next block is free, we must coalesce. */ - block_header_t* remaining_block = block_split(block, size); - block_set_prev_used(remaining_block); - - remaining_block = block_merge_next(control, remaining_block); - block_insert(control, remaining_block); - } -} - -static block_header_t* block_trim_free_leading(control_t* control, block_header_t* block, size_t size) -{ - block_header_t* remaining_block = block; - if (block_can_split(block, size)) - { - /* We want the 2nd block. */ - remaining_block = block_split(block, size - block_header_overhead); - block_set_prev_free(remaining_block); - - block_link_next(block); - block_insert(control, block); - } - - return remaining_block; -} - -static block_header_t* block_locate_free(control_t* control, size_t size) -{ - int fl = 0, sl = 0; - block_header_t* block = 0; - - if (size) - { - mapping_search(size, &fl, &sl); - - /* - ** mapping_search can futz with the size, so for excessively large sizes it can sometimes wind up - ** with indices that are off the end of the block array. - ** So, we protect against that here, since this is the only callsite of mapping_search. - ** Note that we don't need to check sl, since it comes from a modulo operation that guarantees it's always in range. - */ - if (fl < FL_INDEX_COUNT) - { - block = search_suitable_block(control, &fl, &sl); - } - } - - if (block) - { - tlsf_assert(block_size(block) >= size); - remove_free_block(control, block, fl, sl); - } - - return block; -} - -static void* block_prepare_used(control_t* control, block_header_t* block, size_t size) -{ - void* p = 0; - if (block) - { - tlsf_assert(size && "size must be non-zero"); - block_trim_free(control, block, size); - block_mark_as_used(block); - p = block_to_ptr(block); - } - return p; -} - -/* Clear structure and point all empty lists at the null block. */ -static void control_construct(control_t* control) -{ - int i, j; - - control->block_null.next_free = &control->block_null; - control->block_null.prev_free = &control->block_null; - - control->fl_bitmap = 0; - for (i = 0; i < FL_INDEX_COUNT; ++i) - { - control->sl_bitmap[i] = 0; - for (j = 0; j < SL_INDEX_COUNT; ++j) - { - control->blocks[i][j] = &control->block_null; - } - } -} - -/* -** Debugging utilities. -*/ - -typedef struct integrity_t -{ - int prev_status; - int status; -} integrity_t; - -#define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } } - -static void integrity_walker(void* ptr, size_t size, int used, void* user) -{ - block_header_t* block = block_from_ptr(ptr); - integrity_t* integ = tlsf_cast(integrity_t*, user); - const int this_prev_status = block_is_prev_free(block) ? 1 : 0; - const int this_status = block_is_free(block) ? 1 : 0; - const size_t this_block_size = block_size(block); - - int status = 0; - (void)used; - tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect"); - tlsf_insist(size == this_block_size && "block size incorrect"); - - integ->prev_status = this_status; - integ->status += status; -} - -int tlsf_check(tlsf_t tlsf) -{ - int i, j; - - control_t* control = tlsf_cast(control_t*, tlsf); - int status = 0; - - /* Check that the free lists and bitmaps are accurate. */ - for (i = 0; i < FL_INDEX_COUNT; ++i) - { - for (j = 0; j < SL_INDEX_COUNT; ++j) - { - const int fl_map = control->fl_bitmap & (1U << i); - const int sl_list = control->sl_bitmap[i]; - const int sl_map = sl_list & (1U << j); - const block_header_t* block = control->blocks[i][j]; - - /* Check that first- and second-level lists agree. */ - if (!fl_map) - { - tlsf_insist(!sl_map && "second-level map must be null"); - } - - if (!sl_map) - { - tlsf_insist((uint64_t)block == (uint64_t)&control->block_null && "block list must be null"); - continue; - } - - /* Check that there is at least one free block. */ - tlsf_insist(sl_list && "no free blocks in second-level map"); - tlsf_insist((uintptr_t)block != (uintptr_t)&control->block_null && "block should not be null"); - - while (block != &control->block_null) - { - int fli, sli; - tlsf_insist(block_is_free(block) && "block should be free"); - tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced"); - tlsf_insist(!block_is_free(block_next(block)) && "blocks should have coalesced"); - tlsf_insist(block_is_prev_free(block_next(block)) && "block should be free"); - tlsf_insist(block_size(block) >= block_size_min && "block not minimum size"); - - mapping_insert(block_size(block), &fli, &sli); - tlsf_insist(fli == i && sli == j && "block size indexed in wrong list"); - block = block->next_free; - } - } - } - - return status; -} - -#undef tlsf_insist - -static void default_walker(void* ptr, size_t size, int used, void* user) -{ - (void)user; - printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr)); -} - -void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user) -{ - tlsf_walker pool_walker = walker ? walker : default_walker; - block_header_t* block = - offset_to_block(pool, -(int)block_header_overhead); - - while (block && !block_is_last(block)) - { - pool_walker( - block_to_ptr(block), - block_size(block), - !block_is_free(block), - user); - block = block_next(block); - } -} - -size_t tlsf_block_size(void* ptr) -{ - size_t size = 0; - if (ptr) - { - const block_header_t* block = block_from_ptr(ptr); - size = block_size(block); - } - return size; -} - -int tlsf_check_pool(pool_t pool) -{ - /* Check that the blocks are physically correct. */ - integrity_t integ = { 0, 0 }; - tlsf_walk_pool(pool, integrity_walker, &integ); - - return integ.status; -} - -/* -** Size of the TLSF structures in a given memory block passed to -** tlsf_create, equal to the size of a control_t -*/ -size_t tlsf_size(void) -{ - return sizeof(control_t); -} - -size_t tlsf_align_size(void) -{ - return ALIGN_SIZE; -} - -size_t tlsf_block_size_min(void) -{ - return block_size_min; -} - -size_t tlsf_block_size_max(void) -{ - return block_size_max; -} - -/* -** Overhead of the TLSF structures in a given memory block passed to -** tlsf_add_pool, equal to the overhead of a free block and the -** sentinel block. -*/ -size_t tlsf_pool_overhead(void) -{ - return 2 * block_header_overhead; -} - -size_t tlsf_alloc_overhead(void) -{ - return block_header_overhead; -} - -pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes) -{ - block_header_t* block; - block_header_t* next; - - const size_t pool_overhead = tlsf_pool_overhead(); - const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE); - - if (((ptrdiff_t)mem % ALIGN_SIZE) != 0) - { - printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n", - (unsigned int)ALIGN_SIZE); - return 0; - } - - if (pool_bytes < block_size_min || pool_bytes > block_size_max) - { -#if defined (TLSF_64BIT) - printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n", - (unsigned int)(pool_overhead + block_size_min), - (unsigned int)((pool_overhead + block_size_max) / 256)); -#else - printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n", - (unsigned int)(pool_overhead + block_size_min), - (unsigned int)(pool_overhead + block_size_max)); -#endif - return 0; - } - - /* - ** Create the main free block. Offset the start of the block slightly - ** so that the prev_phys_block field falls outside of the pool - - ** it will never be used. - */ - block = offset_to_block(mem, -(tlsfptr_t)block_header_overhead); - block_set_size(block, pool_bytes); - block_set_free(block); - block_set_prev_used(block); - block_insert(tlsf_cast(control_t*, tlsf), block); - - /* Split the block to create a zero-size sentinel block. */ - next = block_link_next(block); - block_set_size(next, 0); - block_set_used(next); - block_set_prev_free(next); - - return mem; -} - -void tlsf_remove_pool(tlsf_t tlsf, pool_t pool) -{ - control_t* control = tlsf_cast(control_t*, tlsf); - block_header_t* block = offset_to_block(pool, -(int)block_header_overhead); - - int fl = 0, sl = 0; - - tlsf_assert(block_is_free(block) && "block should be free"); - tlsf_assert(!block_is_free(block_next(block)) && "next block should not be free"); - tlsf_assert(block_size(block_next(block)) == 0 && "next block size should be zero"); - - mapping_insert(block_size(block), &fl, &sl); - remove_free_block(control, block, fl, sl); -} - -/* -** TLSF main interface. -*/ - -#if _DEBUG -int test_ffs_fls() -{ - /* Verify ffs/fls work properly. */ - int rv = 0; - rv += (tlsf_ffs(0) == -1) ? 0 : 0x1; - rv += (tlsf_fls(0) == -1) ? 0 : 0x2; - rv += (tlsf_ffs(1) == 0) ? 0 : 0x4; - rv += (tlsf_fls(1) == 0) ? 0 : 0x8; - rv += (tlsf_ffs(0x80000000) == 31) ? 0 : 0x10; - rv += (tlsf_ffs(0x80008000) == 15) ? 0 : 0x20; - rv += (tlsf_fls(0x80000008) == 31) ? 0 : 0x40; - rv += (tlsf_fls(0x7FFFFFFF) == 30) ? 0 : 0x80; - -#if defined (TLSF_64BIT) - rv += (tlsf_fls_sizet(0x80000000) == 31) ? 0 : 0x100; - rv += (tlsf_fls_sizet(0x100000000) == 32) ? 0 : 0x200; - rv += (tlsf_fls_sizet(0xffffffffffffffff) == 63) ? 0 : 0x400; -#endif - - if (rv) - { - printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv); - } - return rv; -} -#endif - -tlsf_t tlsf_create(void* mem) -{ -#if _DEBUG - if (test_ffs_fls()) - { - return 0; - } -#endif - - if (((tlsfptr_t)mem % ALIGN_SIZE) != 0) - { - printf("tlsf_create: Memory must be aligned to %u bytes.\n", - (unsigned int)ALIGN_SIZE); - return 0; - } - - control_construct(tlsf_cast(control_t*, mem)); - - return tlsf_cast(tlsf_t, mem); -} - -tlsf_t tlsf_create_with_pool(void* mem, size_t bytes) -{ - tlsf_t tlsf = tlsf_create(mem); - tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size()); - return tlsf; -} - -void tlsf_destroy(tlsf_t tlsf) -{ - /* Nothing to do. */ - (void)tlsf; -} - -pool_t tlsf_get_pool(tlsf_t tlsf) -{ - return tlsf_cast(pool_t, (char*)tlsf + tlsf_size()); -} - -void* tlsf_malloc(tlsf_t tlsf, size_t size) -{ - control_t* control = tlsf_cast(control_t*, tlsf); - const size_t adjust = adjust_request_size(size, ALIGN_SIZE); - block_header_t* block = block_locate_free(control, adjust); - return block_prepare_used(control, block, adjust); -} - -void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size) -{ - control_t* control = tlsf_cast(control_t*, tlsf); - const size_t adjust = adjust_request_size(size, ALIGN_SIZE); - - /* - ** We must allocate an additional minimum block size bytes so that if - ** our free block will leave an alignment gap which is smaller, we can - ** trim a leading free block and release it back to the pool. We must - ** do this because the previous physical block is in use, therefore - ** the prev_phys_block field is not valid, and we can't simply adjust - ** the size of that block. - */ - const size_t gap_minimum = sizeof(block_header_t); - const size_t size_with_gap = adjust_request_size(adjust + align + gap_minimum, align); - - /* - ** If alignment is less than or equals base alignment, we're done. - ** If we requested 0 bytes, return null, as tlsf_malloc(0) does. - */ - const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust; - - block_header_t* block = block_locate_free(control, aligned_size); - - /* This can't be a static assert. */ - tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead); - - if (block) - { - void* ptr = block_to_ptr(block); - void* aligned = align_ptr(ptr, align); - size_t gap = tlsf_cast(size_t, - tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); - - /* If gap size is too small, offset to next aligned boundary. */ - if (gap && gap < gap_minimum) - { - const size_t gap_remain = gap_minimum - gap; - const size_t offset = tlsf_max(gap_remain, align); - const void* next_aligned = tlsf_cast(void*, - tlsf_cast(tlsfptr_t, aligned) + offset); - - aligned = align_ptr(next_aligned, align); - gap = tlsf_cast(size_t, - tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); - } - - if (gap) - { - tlsf_assert(gap >= gap_minimum && "gap size too small"); - block = block_trim_free_leading(control, block, gap); - } - } - - return block_prepare_used(control, block, adjust); -} - -void tlsf_free(tlsf_t tlsf, void* ptr) -{ - /* Don't attempt to free a NULL pointer. */ - if (ptr) - { - control_t* control = tlsf_cast(control_t*, tlsf); - block_header_t* block = block_from_ptr(ptr); - tlsf_assert(!block_is_free(block) && "block already marked as free"); - block_mark_as_free(block); - block = block_merge_prev(control, block); - block = block_merge_next(control, block); - block_insert(control, block); - } -} - -/* -** The TLSF block information provides us with enough information to -** provide a reasonably intelligent implementation of realloc, growing or -** shrinking the currently allocated block as required. -** -** This routine handles the somewhat esoteric edge cases of realloc: -** - a non-zero size with a null pointer will behave like malloc -** - a zero size with a non-null pointer will behave like free -** - a request that cannot be satisfied will leave the original buffer -** untouched -** - an extended buffer size will leave the newly-allocated area with -** contents undefined -*/ -void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size) -{ - control_t* control = tlsf_cast(control_t*, tlsf); - void* p = 0; - - /* Zero-size requests are treated as free. */ - if (ptr && size == 0) - { - tlsf_free(tlsf, ptr); - } - /* Requests with NULL pointers are treated as malloc. */ - else if (!ptr) - { - p = tlsf_malloc(tlsf, size); - } - else - { - block_header_t* block = block_from_ptr(ptr); - block_header_t* next = block_next(block); - - const size_t cursize = block_size(block); - const size_t combined = cursize + block_size(next) + block_header_overhead; - const size_t adjust = adjust_request_size(size, ALIGN_SIZE); - - tlsf_assert(!block_is_free(block) && "block already marked as free"); - - /* - ** If the next block is used, or when combined with the current - ** block, does not offer enough space, we must reallocate and copy. - */ - if (adjust > cursize && (!block_is_free(next) || adjust > combined)) - { - p = tlsf_malloc(tlsf, size); - if (p) - { - const size_t minsize = tlsf_min(cursize, size); - memcpy(p, ptr, minsize); - tlsf_free(tlsf, ptr); - } - } - else - { - /* Do we need to expand to the next block? */ - if (adjust > cursize) - { - block_merge_next(control, block); - block_mark_as_used(block); - } - - /* Trim the resulting block and return the original pointer. */ - block_trim_used(control, block, adjust); - p = ptr; - } - } - - return p; -} diff --git a/sys/vm/vm_device.c b/sys/vm/vm_device.c deleted file mode 100644 index 976dec7..0000000 --- a/sys/vm/vm_device.c +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static int dv_pager_get(struct vm_object *obj, off_t off, size_t len, - struct vm_page *pg); - -static int dv_pager_store(struct vm_object *obj, off_t off, size_t len, - struct vm_page *pg); - -static int dv_pager_paddr(struct vm_object *obj, paddr_t *paddr, - vm_prot_t prot); - -/* Pager operations */ -struct vm_pagerops g_dev_pagerops = { - .get = dv_pager_get, - .store = dv_pager_store, - .get_paddr = dv_pager_paddr -}; - -/* - * Fetch a device descriptor from a memory - * object. - * - * Returns 0 on success. - */ -static int -dv_get_dev(struct vm_object *obj, struct device **res) -{ - struct device *dev; - struct vnode *vp; - int status; - - if ((vp = obj->vnode) == NULL) - return -EIO; - - /* Now try to fetch the device */ - if ((status = devfs_get_dev(vp, &dev)) != 0) - return status; - - *res = dev; - return 0; -} - -static int -dv_pager_paddr(struct vm_object *obj, paddr_t *paddr, vm_prot_t prot) -{ - struct device *dev; - int status; - - if (paddr == NULL) { - return -EINVAL; - } - - /* Try to fetch the device */ - if ((status = dv_get_dev(obj, &dev)) != 0) { - return status; - } - - if (dev->mmap == NULL) { - /* - * mmap() is not supported, this is not an object - * we can handle so try switching its operations - * to the vnode pager ops... - */ - obj->pgops = &g_vnode_pagerops; - return -ENOTSUP; - } - - *paddr = dev->mmap(dev, 0, prot); - return 0; -} - -static int -dv_pager_store(struct vm_object *obj, off_t off, size_t len, struct vm_page *pg) -{ - return -1; -} - -static int -dv_pager_get(struct vm_object *obj, off_t off, size_t len, struct vm_page *pg) -{ - return -1; -} diff --git a/sys/vm/vm_dynalloc.c b/sys/vm/vm_dynalloc.c deleted file mode 100644 index dea4460..0000000 --- a/sys/vm/vm_dynalloc.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include - -/* - * Dynamically allocates memory - * - * @sz: The amount of bytes to allocate - */ -void * -dynalloc(size_t sz) -{ - struct vm_ctx *vm_ctx = vm_get_ctx(); - void *tmp; - - spinlock_acquire(&vm_ctx->dynalloc_lock); - tmp = tlsf_malloc(vm_ctx->tlsf_ctx, sz); - spinlock_release(&vm_ctx->dynalloc_lock); - return tmp; -} - -void * -dynalloc_memalign(size_t sz, size_t align) -{ - struct vm_ctx *vm_ctx = vm_get_ctx(); - void *tmp; - - spinlock_acquire(&vm_ctx->dynalloc_lock); - tmp = tlsf_memalign(vm_ctx->tlsf_ctx, align, sz); - spinlock_release(&vm_ctx->dynalloc_lock); - return tmp; -} - -/* - * Reallocates memory pool created by `dynalloc()' - * - * @old_ptr: Pointer to old pool. - * @newsize: Size of new pool. - */ -void * -dynrealloc(void *old_ptr, size_t newsize) -{ - struct vm_ctx *vm_ctx = vm_get_ctx(); - void *tmp; - - spinlock_acquire(&vm_ctx->dynalloc_lock); - tmp = tlsf_realloc(vm_ctx->tlsf_ctx, old_ptr, newsize); - spinlock_release(&vm_ctx->dynalloc_lock); - return tmp; -} - -/* - * Free dynamically allocated memory - * - * @ptr: Pointer to base of memory. - */ -void -dynfree(void *ptr) -{ - struct vm_ctx *vm_ctx = vm_get_ctx(); - - spinlock_acquire(&vm_ctx->dynalloc_lock); - tlsf_free(vm_ctx->tlsf_ctx, ptr); - spinlock_release(&vm_ctx->dynalloc_lock); -} diff --git a/sys/vm/vm_fault.c b/sys/vm/vm_fault.c deleted file mode 100644 index 45def7f..0000000 --- a/sys/vm/vm_fault.c +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -static struct vm_mapping * -vm_mapq_search(vm_mapq_t *mq, vaddr_t addr) -{ - struct vm_mapping *mapping; - const struct vm_range *range; - - TAILQ_FOREACH(mapping, mq, link) { - range = &mapping->range; - if (addr >= range->start && addr <= range->end) { - return mapping; - } - } - - return NULL; -} - -static struct vm_mapping * -vm_find_mapping(vaddr_t addr) -{ - struct vm_mapping *mapping; - struct proc *td = this_td(); - vm_mapq_t *mapq; - - mapping = vm_mapping_fetch(&td->mapspace, addr); - if (mapping != NULL) - return mapping; - - /* Need to search other maps */ - for (size_t i = 0; i < MTAB_ENTRIES; ++i) { - mapq = &td->mapspace.mtab[i]; - mapping = vm_mapq_search(mapq, addr); - if (mapping != NULL) - return mapping; - } - - return NULL; -} - -static int -vm_demand_page(struct vm_mapping *mapping, vaddr_t va, vm_prot_t access_type) -{ - struct proc *td; - paddr_t pa_base; - - int s; - size_t granule = vm_get_page_size(); - - /* Allocate physical memory if needed */ - if (mapping->physmem_base == 0) { - pa_base = vm_alloc_pageframe(1); - mapping->physmem_base = pa_base; - } else { - pa_base = mapping->physmem_base; - } - - td = this_td(); - s = vm_map_create(td->addrsp, va, pa_base, access_type, granule); - return s; -} - -int -vm_fault(vaddr_t va, vm_prot_t access_type) -{ - struct vm_mapping *mapping; - struct vm_object *vmobj; - - int s = 0; - size_t granule = vm_get_page_size(); - vaddr_t va_base = va & ~(granule - 1); - - mapping = vm_find_mapping(va_base); - if (mapping == NULL) - return -1; - - if ((vmobj = mapping->vmobj) == NULL) - /* Virtual memory object non-existent */ - return -1; - if ((access_type & ~mapping->prot) != 0) - /* Invalid access type */ - return -1; - - vm_object_ref(vmobj); - - /* Can we perform demand paging? */ - if (vmobj->demand) { - s = vm_demand_page(mapping, va_base, access_type); - if (s != 0) - goto done; - } - -done: - /* Drop the vmobj ref */ - vm_object_unref(vmobj); - return s; -} diff --git a/sys/vm/vm_init.c b/sys/vm/vm_init.c deleted file mode 100644 index 3087e6e..0000000 --- a/sys/vm/vm_init.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -/* - * XXX: As of now, these are constant... It would be nice if we could - * have this value expanded upon request so we can keep it small - * to not hog up resources. Would make it more flexible too :^) - */ -#define DYNALLOC_POOL_SZ 0x400000 /* 4 MiB */ -#define DYNALLOC_POOL_PAGES (DYNALLOC_POOL_SZ / vm_get_page_size()) - -static volatile struct vas kernel_vas; - -/* - * TODO: Move this to a per CPU structure, this kinda sucks - * how it is right now... - */ -static struct vm_ctx bsp_vm_ctx = {0}; - -volatile struct limine_hhdm_request g_hhdm_request = { - .id = LIMINE_HHDM_REQUEST, - .revision = 0 -}; - -struct vm_ctx * -vm_get_ctx(void) -{ - return &bsp_vm_ctx; -} - -/* - * Return the kernel VAS. - */ -struct vas -vm_get_kvas(void) -{ - return kernel_vas; -} - -void -vm_init(void) -{ - void *pool_va; - - kernel_vas = pmap_read_vas(); - if (pmap_init(vm_get_ctx()) != 0) { - panic("Failed to init pmap layer\n"); - } - - /* Setup virtual memory context */ - bsp_vm_ctx.dynalloc_pool_sz = DYNALLOC_POOL_SZ; - bsp_vm_ctx.dynalloc_pool_phys = vm_alloc_pageframe(DYNALLOC_POOL_PAGES); - if (bsp_vm_ctx.dynalloc_pool_phys == 0) { - panic("Failed to allocate dynamic pool\n"); - } - pool_va = PHYS_TO_VIRT(bsp_vm_ctx.dynalloc_pool_phys); - bsp_vm_ctx.tlsf_ctx = tlsf_create_with_pool(pool_va, DYNALLOC_POOL_SZ); - __assert(bsp_vm_ctx.tlsf_ctx != 0); -} diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c deleted file mode 100644 index ca1d18a..0000000 --- a/sys/vm/vm_map.c +++ /dev/null @@ -1,540 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define ALLOC_MAPPING() dynalloc(sizeof(struct vm_mapping)) -#define DESTROY_MAPPING(MAPPING) dynfree(MAPPING) -#define MMAP_DEFAULT_OFF 0x3000000 - -static size_t -vm_hash_vaddr(vaddr_t va) { - va = (va ^ (va >> 30)) * (size_t)0xBF58476D1CE4E5B9; - va = (va ^ (va >> 27)) * (size_t)0x94D049BB133111EB; - va = va ^ (va >> 31); - return va; -} - -/* - * Fetches a suitable offset to be added to an - * address that will be mapped with mmap() to - * avoid clobbering the address space. - */ -static uintptr_t -vm_get_mapoff(struct proc *td) -{ - /* - * FIXME: For now we are adding a fixed offset to the - * address to be mapped. - * - * It would be best to ensure that it isn't in - * range of the process *just in case*. - */ - (void)td; - return MMAP_DEFAULT_OFF; -} - -/* - * Destroy a map queue. - */ -void -vm_free_mapq(vm_mapq_t *mapq) -{ - struct vm_mapping *map; - size_t map_pages, granule; - - granule = vm_get_page_size(); - TAILQ_FOREACH(map, mapq, link) { - map_pages = (map->range.end - map->range.start) / granule; - vm_free_pageframe(map->range.start, map_pages); - } - dynfree(map); -} - -/* - * Remove a mapping from a mapspace. - * - * @ms: Mapspace. - * @mapping: Mapping to remove. - */ -void -vm_mapspace_remove(struct vm_mapspace *ms, struct vm_mapping *mapping) -{ - size_t vhash; - vm_mapq_t *mapq; - - if (ms == NULL) - return; - - vhash = vm_hash_vaddr(mapping->range.start); - mapq = &ms->mtab[vhash % MTAB_ENTRIES]; - TAILQ_REMOVE(mapq, mapping, link); - --ms->map_count; -} - -/* - * Fetch a mapping from a mapspace. - * - * @ms: Mapspace. - * @va: Virtual address. - */ -struct vm_mapping * -vm_mapping_fetch(struct vm_mapspace *ms, vaddr_t va) -{ - size_t vhash; - const vm_mapq_t *mapq; - struct vm_mapping *map; - - if (ms == NULL) - return NULL; - - vhash = vm_hash_vaddr(va); - mapq = &ms->mtab[vhash % MTAB_ENTRIES]; - - TAILQ_FOREACH(map, mapq, link) { - if (map->vhash == vhash) { - return map; - } - } - - return NULL; -} - -/* - * Insert a mapping into a mapspace. - * - * @ms: Target mapspace. - * @mapping: Mapping to insert. - */ -void -vm_mapspace_insert(struct vm_mapspace *ms, struct vm_mapping *mapping) -{ - size_t vhash; - vm_mapq_t *q; - - if (mapping == NULL || ms == NULL) - return; - - vhash = vm_hash_vaddr(mapping->range.start); - mapping->vhash = vhash; - - q = &ms->mtab[vhash % MTAB_ENTRIES]; - TAILQ_INSERT_HEAD(q, mapping, link); - ++ms->map_count; -} - -/* - * Create a mapping (internal helper) - * - * @addr: Address to map. - * @physmem: Physical address, set to 0 to alloc one here - * @prot: Protection flags. - * - * Returns zero on failure. - */ -static paddr_t -vm_map(void *addr, paddr_t physmem, vm_prot_t prot, size_t len) -{ - struct proc *td = this_td(); - const size_t GRANULE = vm_get_page_size(); - - int status; - - /* Allocate the physical memory if needed */ - if (physmem == 0) - physmem = vm_alloc_pageframe(len / GRANULE); - - if (physmem == 0) - return 0; - - /* - * XXX: There is no need to worry about alignment yet - * as vm_map_create() handles that internally. - */ - prot |= PROT_USER; - status = vm_map_create(td->addrsp, (vaddr_t)addr, physmem, prot, len); - if (status != 0) { - vm_free_pageframe(physmem, len / GRANULE); - return 0; - } - - return physmem; -} - -/* - * Create a mapping backed by a file. - * - * @addr: Address to map. - * @prot: Protection flags. - * @len: Length of mapping. - * @off: Offset. - * @fd: File descriptor. - */ -static paddr_t -vm_fd_map(void *addr, vm_prot_t prot, size_t len, off_t off, int fd, - struct vm_mapping *mapping) -{ - paddr_t physmem = 0; - - int oflag; - struct filedesc *filedes; - struct vnode *vp; - - struct proc *td = this_td(); - struct vm_page pg = {0}; - - /* Attempt to get the vnode */ - filedes = fd_from_fdnum(td, fd); - if (filedes == NULL) - return 0; - if ((vp = filedes->vnode) == NULL) - return 0; - - /* Check the perms of the filedes */ - oflag = filedes->oflag; - if (__TEST(prot, PROT_WRITE) && oflag == O_RDONLY) - return 0; - if (!__TEST(prot, PROT_WRITE) && oflag == O_WRONLY) - return 0; - - /* Try to create the virtual memory object */ - if (vm_obj_init(&vp->vmobj, vp) != 0) - return 0; - - mapping->vmobj = vp->vmobj; - vm_object_ref(vp->vmobj); - - /* Try to fetch a physical address */ - if (vm_pager_paddr(vp->vmobj, &physmem, prot) != 0) { - vm_obj_destroy(vp->vmobj); - return 0; - } - - /* - * If the pager found a physical address for the object to - * be mapped to, then we start off with an anonymous mapping - * then connect it to the physical address (creates a shared mapping) - */ - if (physmem != 0) { - vm_map(addr, physmem, prot, len); - return physmem; - } - - /* - * If the pager could not find a physical address for - * the object to be mapped to, start of with just a plain - * anonymous mapping then page-in from whatever filesystem - * (creates a shared mapping) - */ - physmem = vm_map(addr, 0, prot, len); - pg.physaddr = physmem; - - if (vm_pager_get(vp->vmobj, off, len, &pg) != 0) { - vm_obj_destroy(vp->vmobj); - return 0; - } - - return physmem; -} - -static int -munmap(void *addr, size_t len) -{ - struct proc *td = this_td(); - struct vm_mapping *mapping; - struct vm_object *obj; - - struct vm_mapspace *ms; - size_t map_len, granule; - vaddr_t map_start, map_end; - - ms = &td->mapspace; - - granule = vm_get_page_size(); - mapping = vm_mapping_fetch(ms, (vaddr_t)addr); - if (mapping == NULL) { - return -1; - } - - spinlock_acquire(&td->mapspace_lock); - map_start = mapping->range.start; - map_end = mapping->range.end; - map_len = map_end - map_start; - - /* Try to release any virtual memory objects */ - if ((obj = mapping->vmobj) != NULL) { - /* - * Drop our ref and try to cleanup. If the refcount - * is > 0, something is still holding it and we can't - * do much. - */ - vm_object_unref(obj); - if (obj->ref == 0) { - vm_obj_destroy(obj); - } - } - - /* Release the mapping */ - vm_map_destroy(td->addrsp, map_start, map_len); - vm_free_pageframe(mapping->range.start, map_len / granule); - - /* Destroy the mapping descriptor */ - vm_mapspace_remove(ms, mapping); - dynfree(mapping); - spinlock_release(&td->mapspace_lock); - return 0; -} - -static void * -mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) -{ - const int PROT_MASK = PROT_WRITE | PROT_EXEC; - const size_t GRANULE = vm_get_page_size(); - uintptr_t map_end, map_start; - - struct proc *td = this_td(); - struct vm_mapping *mapping; - struct vm_object *vmobj; - - size_t misalign = ((vaddr_t)addr) & (GRANULE - 1); - off_t mapoff = vm_get_mapoff(td); - - paddr_t physmem = 0; - vaddr_t va = (vaddr_t)addr + mapoff; - - /* Ensure of valid prot flags */ - if ((prot & ~PROT_MASK) != 0) - return MAP_FAILED; - - /* Try to allocate a mapping */ - mapping = ALLOC_MAPPING(); - if (mapping == NULL) - return MAP_FAILED; - - /* Setup prot and mapping start */ - mapping->prot = prot | PROT_USER; - map_start = __ALIGN_DOWN(va, GRANULE); - - /* Ensure the length is aligned */ - len = __ALIGN_UP(len + misalign, GRANULE); - - /* - * Now we check what type of map request - * this is. - */ - if (__TEST(flags, MAP_ANONYMOUS)) { - /* Try to create a virtual memory object */ - if (vm_obj_init(&vmobj, NULL) != 0) - goto fail; - - /* - * If 'addr' is NULL, we'll just allocate physical - * memory right away. - */ - if (addr == NULL) - physmem = vm_alloc_pageframe(len / GRANULE); - - /* - * Enable demand paging for this object if - * `addr` is not NULL. - */ - if (addr != NULL) { - vmobj->is_anon = 1; - vmobj->demand = 1; - - mapping->vmobj = vmobj; - mapping->physmem_base = 0; - } else if (addr == NULL && physmem != 0) { - map_start = physmem + mapoff; - vm_map((void *)map_start, physmem, prot, len); - addr = (void *)physmem; - - vmobj->is_anon = 1; - vmobj->demand = 0; - mapping->vmobj = vmobj; - mapping->physmem_base = physmem; - } - - /* Did this work? */ - if (physmem == 0 && addr == NULL) { - goto fail; - } - } else if (__TEST(flags, MAP_SHARED)) { - physmem = vm_fd_map((void *)map_start, prot, len, off, fildes, mapping); - if (physmem == 0) { - goto fail; - } - } - - /* Setup map_end and map ranges */ - map_end = map_start + len; - mapping->range.start = map_start; - mapping->range.end = map_end; - mapping->physmem_base = physmem; - - /* Add to mapspace */ - spinlock_acquire(&td->mapspace_lock); - vm_mapspace_insert(&td->mapspace, mapping); - spinlock_release(&td->mapspace_lock); - return (void *)map_start; -fail: - DESTROY_MAPPING(mapping); - return MAP_FAILED; -} - -/* - * Internal routine for cleaning up. - * - * @va: VA to start unmapping at. - * @bytes_aligned: Amount of bytes to unmap. - * - * XXX DANGER!!: `bytes_aligned' is expected to be aligned by the - * machine's page granule. If this is not so, - * undefined behaviour will occur. This will - * be enforced via a panic. - */ -static void -vm_map_cleanup(struct vas vas, struct vm_ctx *ctx, vaddr_t va, - size_t bytes_aligned, size_t granule) -{ - __assert(bytes_aligned != 0); - __assert((bytes_aligned & (granule - 1)) == 0); - - for (size_t i = 0; i < bytes_aligned; i += 0x1000) { - if (pmap_unmap(ctx, vas, va + i) != 0) { - /* - * XXX: This shouldn't happen... If it somehow does, - * then this should be handled. - */ - panic("Could not cleanup!!!\n"); - } - } -} - -/* - * Create a virtual memory mappings in the current - * address space. - * - * @va: Virtual address. - * @pa: Physical address. - * @prot: Protection flags. - * @bytes: Amount of bytes to be mapped. This is aligned by the - * machine's page granule, typically a 4k boundary. - */ -int -vm_map_create(struct vas vas, vaddr_t va, paddr_t pa, vm_prot_t prot, size_t bytes) -{ - size_t granule = vm_get_page_size(); - size_t misalign = va & (granule - 1); - int s; - - struct vm_ctx *ctx = vm_get_ctx(); - - /* - * The amount of bytes to be mapped should fully span pages, - * so we ensure it is aligned by the page granularity. - */ - bytes = __ALIGN_UP(bytes + misalign, granule); - - /* Align VA/PA by granule */ - va = __ALIGN_DOWN(va, granule); - pa = __ALIGN_DOWN(pa, granule); - - if (bytes == 0) { - /* You can't map 0 pages, silly! */ - return -1; - } - - for (uintptr_t i = 0; i < bytes; i += granule) { - s = pmap_map(ctx, vas, va + i, pa + i, prot); - if (s != 0) { - /* Something went a bit wrong here, cleanup */ - vm_map_cleanup(vas, ctx, va, i, bytes); - return -1; - } - } - - return 0; -} - -/* - * Destroy a virtual memory mapping in the current - * address space. - */ -int -vm_map_destroy(struct vas vas, vaddr_t va, size_t bytes) -{ - struct vm_ctx *ctx = vm_get_ctx(); - size_t granule = vm_get_page_size(); - size_t misalign = va & (granule - 1); - int s; - - /* We want bytes to be aligned by the granule */ - bytes = __ALIGN_UP(bytes + misalign, granule); - - /* Align VA by granule */ - va = __ALIGN_DOWN(va, granule); - - if (bytes == 0) { - return -1; - } - - for (uintptr_t i = 0; i < bytes; i += granule) { - s = pmap_unmap(ctx, vas, va + i); - if (s != 0) { - return -1; - } - } - - return 0; -} - -uint64_t -sys_mmap(struct syscall_args *args) -{ - return (uintptr_t)mmap((void *)args->arg0, args->arg1, args->arg2, - args->arg3, args->arg4, args->arg5); -} - -uint64_t -sys_munmap(struct syscall_args *args) -{ - return munmap((void *)args->arg0, args->arg1); -} diff --git a/sys/vm/vm_obj.c b/sys/vm/vm_obj.c deleted file mode 100644 index b501c6b..0000000 --- a/sys/vm/vm_obj.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include - -static size_t obj_count = 0; - -static void -vm_set_pgops(struct vm_object *obj, struct vnode *vnode) -{ - if (vnode == NULL) { - obj->vnode = NULL; - return; - } - - /* Is this a device? */ - if (vnode->type == VCHR || vnode->type == VBLK) { - obj->pgops = &g_dev_pagerops; - } else { - obj->pgops = &g_vnode_pagerops; - } -} - -int -vm_obj_init(struct vm_object **res, struct vnode *vnode) -{ - struct vm_object *obj = dynalloc(sizeof(struct vm_object)); - - if (obj == NULL) { - return -ENOMEM; - } - - memset(obj, 0, sizeof(struct vm_object)); - obj->vnode = vnode; - obj->ref = 0; - - vm_set_pgops(obj, vnode); - *res = obj; - ++obj_count; - return 0; -} - -int -vm_obj_destroy(struct vm_object *obj) -{ - struct vnode *vp = obj->vnode; - - /* Check the ref count */ - if (obj->ref > 0) - return -EBUSY; - - if (vp != NULL) - vp->vmobj = NULL; - - dynfree(obj); - --obj_count; - return 0; -} - -size_t -vm_obj_count(void) -{ - return obj_count; -} diff --git a/sys/vm/vm_page.c b/sys/vm/vm_page.c deleted file mode 100644 index f496fee..0000000 --- a/sys/vm/vm_page.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -__MODULE_NAME("vm_page"); -__KERNEL_META("$Hyra$: vm_page.c, Ian Marco Moffett, " - "Virtual memory page specific operations"); - -/* - * Zero `page_count' pages. - * - * @page: First page to start zeroing at - * @page_count: Number of pages to zero. - */ -void -vm_zero_page(void *page, size_t page_count) -{ - const size_t PAGE_SIZE = vm_get_page_size(); - size_t bytes = page_count * PAGE_SIZE; - - /* - * This *should not* happen. Page sizes - * are usually 2^n but if this runs, something - * *very* weird is happening and either something - * broke badly. Or it's that damn technological - * degeneration that is getting out of hand within - * our world!! But really, page sizes *should* be a - * power of 2 and it would be pretty worrying if - * this branches causing a panic stating something - * that shouldn't happen, happened. - * - * The reason why it is wise for pages to be a power of - * 2 is so we can efficiently read/write to the page in - * fixed-sized power of 2 blocks cleanly without the risk - * of clobbering other pages we didn't intend to mess with. - */ - if ((PAGE_SIZE & 1) != 0) { - panic("Unexpected page size, not power of 2!\n"); - } - - memset64(page, 0, bytes/8); -} diff --git a/sys/vm/vm_pager.c b/sys/vm/vm_pager.c deleted file mode 100644 index 0046e9c..0000000 --- a/sys/vm/vm_pager.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include - -/* - * Get pages from backing store. - */ -int -vm_pager_get(struct vm_object *obj, off_t off, size_t len, struct vm_page *pg) -{ - struct vm_pagerops *pgops = obj->pgops; - - if (obj->is_anon) { - return -1; - } - - return pgops->get(obj, off, len, pg); -} - -/* - * Get physical address. - * - * TODO: Remove this and add demanding paging. - */ -int -vm_pager_paddr(struct vm_object *obj, paddr_t *paddr, vm_prot_t prot) -{ - struct vm_pagerops *pgops = obj->pgops; - - if (obj->is_anon) { - return -1; - } - - if (pgops->get_paddr == NULL) { - return 0; - } - - return pgops->get_paddr(obj, paddr, prot); -} diff --git a/sys/vm/vm_physseg.c b/sys/vm/vm_physseg.c deleted file mode 100644 index 88560e5..0000000 --- a/sys/vm/vm_physseg.c +++ /dev/null @@ -1,272 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -__MODULE_NAME("vm_physseg"); -__KERNEL_META("$Hyra$: vm_physseg.c, Ian Marco Moffett, " - "The Hyra physical memory manager"); - -#if defined(VM_PHYSSEG_DEBUG) -#define DPRINTF(...) KDEBUG(__VA_ARGS__) -#else -#define DPRINTF(...) __nothing -#endif /* defined(VM_PHYSSEG_DEBUG) */ - -static struct limine_memmap_request mmap_req = { - .id = LIMINE_MEMMAP_REQUEST, - .revision = 0 -}; - -static struct limine_memmap_response *resp = NULL; - -__used static const char *segment_name[] = { - [LIMINE_MEMMAP_USABLE] = "usable", - [LIMINE_MEMMAP_RESERVED] = "reserved", - [LIMINE_MEMMAP_ACPI_RECLAIMABLE] = "ACPI reclaimable", - [LIMINE_MEMMAP_ACPI_NVS] = "ACPI NVS", - [LIMINE_MEMMAP_BAD_MEMORY] = "bad", - [LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE] = "bootloader reclaimable", - [LIMINE_MEMMAP_KERNEL_AND_MODULES] = "kernel and modules", - [LIMINE_MEMMAP_FRAMEBUFFER] = "framebuffer" -}; - -static const int MAX_SEGMENTS = __ARRAY_COUNT(segment_name); - - -static bitmap_t bitmap = NULL; -static size_t pages_total = 0; -static size_t pages_reserved = 0; -static size_t last_used_idx = 0; -static size_t pages_allocated = 0; -static size_t bitmap_size = 0; -static size_t highest_frame_idx; -static size_t bitmap_free_start; /* Beginning bit of free region */ - -static void -vm_physseg_getstat(void) -{ - struct limine_memmap_entry *entry; - size_t entry_pages = 0; - - pages_total = 0; - pages_reserved = 0; - - for (size_t i = 0; i < resp->entry_count; ++i) { - entry = resp->entries[i]; - entry_pages = entry->length / vm_get_page_size(); - - /* Drop invalid entries */ - if (entry->type >= MAX_SEGMENTS) { - continue; - } - - pages_total += entry_pages; - - if (entry->type != LIMINE_MEMMAP_USABLE) { - pages_reserved += entry_pages; - continue; - } - } -} - -static void -vm_physseg_bitmap_alloc(void) -{ - struct limine_memmap_entry *entry; - uintptr_t highest_addr = 0; - - for (size_t i = 0; i < resp->entry_count; ++i) { - entry = resp->entries[i]; - - /* Drop any entries with an invalid type */ - if (entry->type >= MAX_SEGMENTS) { - continue; - } - - if (entry->type != LIMINE_MEMMAP_USABLE) { - /* This memory is not usable */ - continue; - } - - if (entry->length >= bitmap_size) { - bitmap = PHYS_TO_VIRT(entry->base); - memset(bitmap, 0xFF, bitmap_size); - entry->length -= bitmap_size; - entry->base += bitmap_size; - return; - } - - highest_addr = __MAX(highest_addr, entry->base + entry->length); - } -} - -static void -vm_physseg_bitmap_populate(void) -{ - struct limine_memmap_entry *entry; -#if defined(VM_PHYSSEG_DEBUG) - size_t start, end; -#endif /* defined(VM_PHYSSEG_DEBUG) */ - - for (size_t i = 0; i < resp->entry_count; ++i) { - entry = resp->entries[i]; - - /* Drop any entries with an invalid type */ - if (entry->type >= MAX_SEGMENTS) { - continue; - } - -#if defined(VM_PHYSSEG_DEBUG) - /* Dump the memory map if we are debugging */ - start = entry->base; - end = entry->base + entry->length; - DPRINTF("0x%x - 0x%x, size: 0x%x, type: %s\n", - start, end, entry->length, - segment_name[entry->type]); -#endif /* defined(VM_PHYSSEG_DEBUG) */ - - /* Don't set non-usable entries as free */ - if (entry->type != LIMINE_MEMMAP_USABLE) { - continue; - } - - /* Populate */ - if (bitmap_free_start == 0) { - bitmap_free_start = entry->base/0x1000; - } - for (size_t j = 0; j < entry->length; j += 0x1000) { - bitmap_unset_bit(bitmap, (entry->base + j) / 0x1000); - } - } -} - -static void -vm_physseg_bitmap_init(void) -{ - uintptr_t highest_addr; - struct limine_memmap_entry *entry; - - highest_addr = 0; - highest_frame_idx = 0; - - /* Find the highest entry */ - for (size_t i = 0; i < resp->entry_count; ++i) { - entry = resp->entries[i]; - - if (entry->type != LIMINE_MEMMAP_USABLE) { - /* Memeory not usable */ - continue; - } - - highest_addr = __MAX(highest_addr, entry->base + entry->length); - } - - highest_frame_idx = highest_addr / 0x1000; - bitmap_size = __ALIGN_UP(highest_frame_idx / 8, 0x1000); - - DPRINTF("Bitmap size: %d bytes\n", bitmap_size); - DPRINTF("Allocating and populating bitmap now...\n"); - - vm_physseg_bitmap_alloc(); - vm_physseg_bitmap_populate(); -} - -uintptr_t -vm_alloc_pageframe(size_t count) -{ - size_t pages = 0; - size_t tmp; - - while (last_used_idx < highest_frame_idx) { - if (!bitmap_test_bit(bitmap, last_used_idx++)) { - /* We have a free page */ - if (++pages != count) - continue; - - tmp = last_used_idx - count; - - for (size_t i = tmp; i < last_used_idx; ++i) - bitmap_set_bit(bitmap, i); - - pages_allocated += count; - return tmp * vm_get_page_size(); - } else { - pages = 0; - } - } - - return 0; -} - -/* - * Frees physical pageframes. - * - * @base: Base to start freeing at. - * @count: Number of pageframes to free. - */ -void -vm_free_pageframe(uintptr_t base, size_t count) -{ - const size_t PAGE_SIZE = vm_get_page_size(); - - for (uintptr_t p = base; p < base + (count*PAGE_SIZE); p += PAGE_SIZE) { - bitmap_unset_bit(bitmap, p/0x1000); - } - - pages_allocated -= count; -} - -void -vm_physseg_init(void) -{ - resp = mmap_req.response; - - vm_physseg_bitmap_init(); -} - -struct physmem_stat -vm_phys_memstat(void) -{ - size_t pagesize = vm_get_page_size(); - struct physmem_stat stat; - - vm_physseg_getstat(); - stat.total_kib = (pages_total * pagesize) / 1024; - stat.reserved_kib = (pages_reserved * pagesize) / 1024; - stat.alloc_kib = (pages_allocated * pagesize) / 1024; - stat.avl_kib = stat.total_kib - stat.alloc_kib; - return stat; -} diff --git a/sys/vm/vm_stat.c b/sys/vm/vm_stat.c deleted file mode 100644 index 6374699..0000000 --- a/sys/vm/vm_stat.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include - -struct vm_memstat -vm_memstat(void) -{ - struct vm_memstat stat; - - stat.pmem_stat = vm_phys_memstat(); - stat.vmobj_cnt = vm_obj_count(); - return stat; -} diff --git a/sys/vm/vm_vnode.c b/sys/vm/vm_vnode.c deleted file mode 100644 index e3d95ac..0000000 --- a/sys/vm/vm_vnode.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Hyra nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -static int vn_pager_get(struct vm_object *obj, off_t off, size_t len, - struct vm_page *pg); - -static int vn_pager_store(struct vm_object *obj, off_t off, size_t len, - struct vm_page *pg); - -/* Pager operations */ -struct vm_pagerops g_vnode_pagerops = { - .get = vn_pager_get, - .store = vn_pager_store -}; - -static inline void -vn_prep_sio(struct sio_txn *sio, char *pg, off_t off, size_t len) -{ - sio->len = __ALIGN_DOWN(len, vm_get_page_size()); - sio->buf = pg; - sio->offset = off; - sio->len = len; -} - -static int -vn_pager_io(struct vm_object *obj, off_t off, size_t len, - struct vm_page *pg, bool out) -{ - struct sio_txn sio; - struct vnode *vp; - ssize_t paged_bytes; - char *dest; - int res = 0; - - if (obj == NULL || pg == NULL) { - return -EIO; - } - - spinlock_acquire(&obj->lock); - vm_object_ref(obj); - dest = PHYS_TO_VIRT(pg->physaddr); - - /* Attempt to fetch the vnode */ - if ((vp = obj->vnode) == NULL) { - res = -EIO; - goto done; - } - - /* Prepare the SIO transaction */ - vn_prep_sio(&sio, dest, off, len); - if (sio.len == 0) { - res = -EIO; - goto done; - } - - /* Perform I/O on the backing store */ - paged_bytes = out ? vfs_write(vp, &sio) : vfs_read(vp, &sio); - if (paged_bytes < 0) { - /* Failure */ - res = paged_bytes; - goto done; - } -done: - vm_object_unref(obj); - spinlock_release(&obj->lock); - return res; -} - -static int -vn_pager_store(struct vm_object *obj, off_t off, size_t len, struct vm_page *pg) -{ - return vn_pager_io(obj, off, len, pg, true); -} - -static int -vn_pager_get(struct vm_object *obj, off_t off, size_t len, struct vm_page *pg) -{ - return vn_pager_io(obj, off, len, pg, false); -} -- cgit v1.2.3