Certainly! Below is an example of how you might implement simple memory allocation (malloc), freeing (free), and memory counting (count_memory) functions in C with inline GCC assembly using AT&T syntax for an x86 operating system running in 32-bit protected mode.
#include <stdint.h>
#include <stddef.h>
#define HEAP_START 0x100000 // Heap starts at 1MB
#define HEAP_END 0x200000 // Heap ends at 2MB
static uint32_t heap_start = HEAP_START;
static uint32_t heap_end = HEAP_END;
static uint32_t heap_current = HEAP_START;
typedef struct mem_block {
size_t size;
struct mem_block* next;
int free;
} mem_block_t;
static mem_block_t* free_list = NULL;
/* Inline assembly functions to read and write to I/O ports */
static inline uint8_t inb(uint16_t port) {
uint8_t ret;
__asm__ volatile ("inb %1, %0"
: "=a"(ret)
: "dN"(port));
return ret;
}
static inline void outb(uint16_t port, uint8_t val) {
__asm__ volatile ("outb %0, %1"
:
: "a"(val), "dN"(port));
}
/* Function to read CMOS registers */
static inline uint8_t cmos_read(uint8_t reg) {
/* Disable NMI (Non-Maskable Interrupt) by setting the top bit (bit 7) */
outb(0x70, 0x80 | reg);
uint8_t value = inb(0x71);
return value;
}
/* Function to count total memory in bytes */
size_t count_memory() {
uint8_t base_low = cmos_read(0x15);
uint8_t base_high = cmos_read(0x16);
uint8_t ext_low = cmos_read(0x17);
uint8_t ext_high = cmos_read(0x18);
size_t base_mem_kb = ((size_t)base_high << 8) | base_low;
size_t extended_mem_kb = ((size_t)ext_high << 8) | ext_low;
size_t total_mem_kb = base_mem_kb + extended_mem_kb;
return total_mem_kb * 1024; // Return total memory in bytes
}
/* Memory allocation function */
void* malloc(size_t size) {
mem_block_t* block;
/* Align size to multiple of 8 bytes using inline assembly */
size_t aligned_size;
__asm__ volatile (
"addl $7, %[size]\n\t"
"andl $-8, %[size]\n\t"
: [size] "=r"(aligned_size)
: "0"(size)
);
size = aligned_size;
if (free_list) {
/* Find a free block */
block = free_list;
mem_block_t* prev = NULL;
while (block && !(block->free && block->size >= size)) {
prev = block;
block = block->next;
}
if (block) {
/* Found a suitable free block */
block->free = 0;
/* Split block if it's significantly bigger than requested size */
size_t remaining_size = block->size - size - sizeof(mem_block_t);
if (remaining_size > sizeof(mem_block_t) + 8) {
mem_block_t* new_block = (mem_block_t*)((uint8_t*)block + sizeof(mem_block_t) + size);
new_block->size = remaining_size;
new_block->free = 1;
new_block->next = block->next;
block->size = size;
block->next = new_block;
}
return (void*)((uint8_t*)block + sizeof(mem_block_t));
} else {
/* No suitable block found, create a new one */
uint32_t heap_current_new = (uint32_t)prev + sizeof(mem_block_t) + prev->size;
if (heap_current_new + sizeof(mem_block_t) + size > heap_end) {
/* Out of memory */
return NULL;
}
block = (mem_block_t*)heap_current_new;
block->size = size;
block->free = 0;
block->next = NULL;
prev->next = block;
return (void*)((uint8_t*)block + sizeof(mem_block_t));
}
} else {
/* First allocation */
block = (mem_block_t*)heap_current;
block->size = size;
block->free = 0;
block->next = NULL;
free_list = block;
heap_current += sizeof(mem_block_t) + size;
return (void*)((uint8_t*)block + sizeof(mem_block_t));
}
}
/* Memory freeing function */
void free(void* ptr) {
if (!ptr)
return;
mem_block_t* block = (mem_block_t*)((uint8_t*)ptr - sizeof(mem_block_t));
/* Atomically set block->free = 1 using inline assembly */
__asm__ volatile("movl $1, %0"
: "=m"(block->free)
:
: "memory");
/* Coalesce adjacent free blocks */
mem_block_t* current = free_list;
while (current) {
if (current->free) {
/* Look ahead to see if next block is free */
while (current->next && current->next->free) {
current->size += sizeof(mem_block_t) + current->next->size;
current->next = current->next->next;
}
}
current = current->next;
}
}
Explanation:
-
Counting Memory (count_memory):
- Uses inline assembly to perform I/O port reads and writes to access CMOS registers.
- Reads base and extended memory sizes from CMOS addresses 0x15-0x18.
- Computes total memory in bytes.
-
Memory Allocation (malloc):
- Aligns the requested size to an 8-byte boundary using inline assembly.
- Manages a linked list of memory blocks (
mem_block_t) to keep track of allocated and free blocks.
- If a suitable free block is found, it reuses it; otherwise, it extends the heap.
- Splits larger blocks to minimize waste.
-
Memory Freeing (free):
- Marks the block as free using an atomic operation in inline assembly.
- Coalesces adjacent free blocks to prevent fragmentation.
Notes:
Compilation:
- To compile this code, you would typically use GCC with appropriate flags to produce a flat binary or an OS kernel image.
- Example:
gcc -m32 -ffreestanding -c your_code.c -o your_code.o
Disclaimer:
- Writing an operating system involves many low-level details and considerations that go beyond this simple example.
- Ensure that you have appropriate privilege levels and that your code runs in the correct context when accessing hardware directly.
I hope this helps you with your operating system development!