/* * Creates a semaphore and initializes it with the specified integer value. */ struct ksema *ksema_init(unsigned int initval) { struct ksema *sem = __kmalloc(sizeof(struct ksema)); if (sem) sem->value = initval; return sem; }
///////////////////////////File operations/////////////////////// ssize_t control_file_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) { void* p = __kmalloc(10, GFP_KERNEL); if(p == NULL) return -ENOMEM; kfree(p); return count; }
UIComponent::UIComponent(Painter *painter, Rect bounds) : painter(painter) , bounds(bounds) , dirty(true) { children = (LinkedList *)__kmalloc(sizeof(LinkedList)); list_init(children, __kfree); }
/********************************************************************* * Replacement functions *********************************************************************/ static void* repl___kmalloc(size_t size, gfp_t flags, struct kedr_function_call_info* call_info) { void* ret_val; target_in_init = (kedr_target_module_in_init() ? 1 : 0); ret_val = __kmalloc(size, flags); return ret_val; }
/********************************************************************* * "kmalloc" group *********************************************************************/ static void * repl___kmalloc(size_t size, gfp_t flags) { void *ret_val; ret_val = __kmalloc(size, flags); if (!ZERO_OR_NULL_PTR(ret_val)) klc_add_alloc(ret_val, size, stack_depth); return ret_val; }
/* A wrapper around __kmalloc() that just records the arguments, calls __kmalloc, records and returns its result. */ void* icpt_watcher_kmalloc(size_t size, gfp_t flags) { void* ret = NULL; printk(KERN_ALERT "[icpt_watcher] __kmalloc() called with size = %lu, flags = %lu\n", (unsigned long)size, (unsigned long)flags); ret = __kmalloc(size, flags); printk(KERN_ALERT "[icpt_watcher] __kmalloc() returned %p\n", ret); return ret; }
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent) { struct proc_dir_entry *pde = __kmalloc(sizeof(struct proc_dir_entry), 0); memset(pde, 0, sizeof(*pde)); pde->name = name; if(parent) { pde->next = parent->subdir; parent->subdir = pde; } return pde; }
char *kstrdup(const char *s, gfp_t gfp) { size_t len; char *buf; if (!s) return NULL; len = strlen(s) + 1; buf = __kmalloc(len, gfp); if (buf) memcpy(buf, s, len); return buf; }
/* Definitions of replacement functions */ static void* repl___kmalloc(size_t size, gfp_t flags) { void* result = kedr_fsim_simulate_kmalloc(size) ? NULL : __kmalloc(size, flags); printk( KERN_INFO "[cp_fsym1] Called: " "__kmalloc(%zu, %x), result: %p, in init: %s\n", size, (unsigned int)flags, result, (kedr_target_module_in_init() ? "yes" : "no") ); return result; }
void *krealloc(const void *p, size_t new_size, gfp_t flags) { void *ret; if (!new_size) { kfree(p); return (void*)16; } ret = __kmalloc(new_size, flags); if (ret && p != ret) kfree(p); return ret; }
struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent) { struct proc_dir_entry *pde = __kmalloc(sizeof(struct proc_dir_entry), 0); memset(pde, 0, sizeof(*pde)); pde->name = name; // XXX: is this ok?!?!?! //strdup(name); pde->mode = mode; if(parent) { pde->next = parent->subdir; parent->subdir = pde; pde->parent = parent; } return pde; }
Status _elf_load_from_file(Pcb* pcb, const char* file_name) { // Need to copy the file_name into kernel land...because we're killing userland! const char* temp = file_name; file_name = (const char *)__kmalloc(_kstrlen(temp) + 1); _kmemcpy((void *)file_name, (void *)temp, _kstrlen(temp)+1); // Copy the null terminator as well serial_printf("---Elf: attempting to open: %s\n", file_name); if (pcb == NULL || file_name == NULL) { return BAD_PARAM; } // Try to open the file Elf32_Ehdr* elf32_hdr = (Elf32_Ehdr *)__kmalloc(sizeof(Elf32_Ehdr)); serial_printf("ELF header location: %x\n", elf32_hdr); Uint bytes_read = 0; VFSStatus vfs_status = raw_read(file_name, (void *)elf32_hdr, &bytes_read, 0, sizeof(Elf32_Ehdr)); if (vfs_status != FS_E_OK /* Couldn't read the file */ || bytes_read < sizeof(Elf32_Ehdr) /* Clearly not an ELF file */ || elf32_hdr->e_magic != ELF_MAGIC_NUM /* Need the magic number! */ || elf32_hdr->e_type != ET_EXEC /* Don't support relocatable or dynamic files yet */ || elf32_hdr->e_machine != EM_386 /* Make sure it's for our architecture */ || elf32_hdr->e_entry == 0x0 /* Need an entry point */ || elf32_hdr->e_version != EV_CURRENT /* We don't support extensions right now */ || elf32_hdr->e_phoff == 0 /* If there are no program headers, what do we load? */ || elf32_hdr->e_phnum == 0) /* ... */ // || elf32_hdr->e_ehsize != sizeof(Elf32_Ehdr)) /* The header size should match our struct */ { if (vfs_status != FS_E_OK) { serial_printf("RETURN VALUE: %x\n", vfs_status); _kpanic("ELF", "Failed to open file successfully\n", 0); } if (bytes_read < sizeof(Elf32_Ehdr)) _kpanic("ELF", "Read too small of a file!\n", 0); if (elf32_hdr->e_magic != ELF_MAGIC_NUM) _kpanic("ELF", "Bad magic number!\n", 0); if (elf32_hdr->e_type != ET_EXEC) _kpanic("ELF", "Not an executable ELF!\n", 0); if (elf32_hdr->e_machine != EM_386) _kpanic("ELF", "Not a i386 ELF!\n", 0); if (elf32_hdr->e_entry == 0x0) _kpanic("ELF", "Bad entry point!\n", 0); if (elf32_hdr->e_version != EV_CURRENT) _kpanic("ELF", "Don't support non-current versions!\n", 0); if (elf32_hdr->e_phoff == 0) _kpanic("ELF", "No program headers found!\n", 0); if (elf32_hdr->e_phnum == 0) _kpanic("ELF", "Zero program headers!\n", 0); _kpanic("ELF", "Couldn't open file!\n", 0); // Problem opening the file __kfree(elf32_hdr); return BAD_PARAM; } if (sizeof(Elf32_Phdr) != elf32_hdr->e_phentsize) { _kpanic("ELF", "program header size is different!\n", 0); } /* Okay lets start reading in and setting up the ELF file */ // We need a new buffer of size of (e_phentsize * e_phnum) Uint32 pheader_tbl_size = sizeof(Elf32_Phdr) * elf32_hdr->e_phnum; Elf32_Phdr* pheaders = (Elf32_Phdr *)__kmalloc(pheader_tbl_size); serial_printf("---ELF: program headers location: %x\n", pheaders); serial_printf("ELF: Reading program headers\n"); vfs_status = raw_read(file_name, (void *)pheaders, &bytes_read, elf32_hdr->e_phoff, pheader_tbl_size); if (vfs_status != FS_E_OK || bytes_read < pheader_tbl_size) { _kpanic("ELF", "error reading file!\n", 0); __kfree(pheaders); __kfree(elf32_hdr); return BAD_PARAM; } serial_printf("ELF: resetting page directory\n"); // Cleanup the old processes page directory, we're replacing everything __virt_reset_page_directory(); serial_printf("ELF: About to read the program sections\n"); /* We need to load all of the program sections now */ for (Int32 i = 0; i < elf32_hdr->e_phnum; ++i) { Elf32_Phdr* cur_phdr = &(pheaders[i]); if (cur_phdr->p_type == PT_LOAD) { if (cur_phdr->p_vaddr >= KERNEL_LINK_ADDR || cur_phdr->p_vaddr < 0x100000) { _kpanic("ELF", "An ELF with bad addresses loaded", 0); } serial_printf("\tELF: loading program section: %d at %x size: %x\n", i, cur_phdr->p_vaddr, cur_phdr->p_memsz); if (cur_phdr->p_memsz == 0) { serial_printf("\tELF: empty section, skipping\n"); continue; } // This is a loadable section //if (cur_phdr->p_align > 1) // _kpanic("ELF", "ELF loader doesn't support aligned program segments\n", 0); // Map these pages into memory! void* start_address = (void *)cur_phdr->p_vaddr; void* end_address = (void *)(start_address + cur_phdr->p_memsz); for (; start_address < end_address; start_address += PAGE_SIZE) { Uint32 flags = PG_USER; if ((cur_phdr->p_flags & PF_WRITE) > 0) { flags |= PG_READ_WRITE; } serial_printf("Checking address: %x\n", __virt_get_phys_addr(start_address)); serial_printf("Start address: %x\n", start_address); if (__virt_get_phys_addr(start_address) == (void *)0xFFFFFFFF) { serial_printf("ELF: Mapping page: %x - flags: %x\n", start_address, flags); __virt_map_page(__phys_get_free_4k(), start_address, flags); serial_printf("ELF: Done mapping page\n"); } else { serial_printf("Address: %x already mapped\n", start_address); } } serial_printf("ELF: about to memcpy program section: %x of size %d\n", cur_phdr->p_vaddr, cur_phdr->p_memsz); // Lets zero it out, we only need to zero the remaining bytes, p_filesz // may be zero for data sections, in this case the memory should be zeroed _kmemclr((void *)(cur_phdr->p_vaddr + (cur_phdr->p_memsz - cur_phdr->p_filesz)), cur_phdr->p_memsz - cur_phdr->p_filesz); serial_printf("ELF: done memory copying: %s\n", file_name); // Now we have to read it in from the file if (cur_phdr->p_filesz > 0) { serial_printf("\tAt offset: %x\n", cur_phdr->p_offset); vfs_status = raw_read(file_name, (void *)cur_phdr->p_vaddr, &bytes_read, cur_phdr->p_offset, cur_phdr->p_filesz); serial_printf("Read: %d - File size: %d\n", bytes_read, cur_phdr->p_filesz); if (bytes_read != cur_phdr->p_filesz) { _kpanic("ELF", "Failed to read data from the filesystem", 0); } if (vfs_status != FS_E_OK) { // TODO - cleanup if error _kpanic("ELF", "failed to read program section\n", 0); } //asm volatile("hlt"); } } else { serial_printf("\tELF: Non-loadable section: %d at %x size: %x type: %d\n", i, cur_phdr->p_vaddr, cur_phdr->p_memsz, cur_phdr->p_type); } } // Setup the PCB information // Allocate a stack and map some pages for it #define USER_STACK_LOCATION 0x2000000 #define USER_STACK_SIZE 0x4000 /* 16 KiB */ serial_printf("ELF: Allocating stack\n"); void* stack_start = (void *)USER_STACK_LOCATION; void* stack_end = (void *)(USER_STACK_LOCATION + USER_STACK_SIZE); for (; stack_start < stack_end; stack_start += PAGE_SIZE) { __virt_map_page(__phys_get_free_4k(), stack_start, PG_READ_WRITE | PG_USER); } _kmemclr((void *)USER_STACK_LOCATION, USER_STACK_SIZE); // Throw exit as the return address as a safe guard serial_printf("ELF: setting up context\n"); // Setup the context Context* context = ((Context *)(USER_STACK_LOCATION+USER_STACK_SIZE-4)) - 1; serial_printf("Context location: %x\n", context); pcb->context = context; context->esp = (Uint32)(((Uint32 *)context) - 1); context->ebp = (USER_STACK_LOCATION+USER_STACK_SIZE)-4; context->cs = GDT_CODE; context->ss = GDT_STACK; context->ds = GDT_DATA; context->es = GDT_DATA; context->fs = GDT_DATA; context->gs = GDT_DATA; serial_printf("ELF: setting entry point: %x\n", elf32_hdr->e_entry); // Entry point context->eip = elf32_hdr->e_entry; // Setup the rest of the PCB pcb->context->eflags = DEFAULT_EFLAGS; serial_printf("ELF: about to return\n"); __kfree(pheaders); __kfree(elf32_hdr); __kfree((void *)file_name); return SUCCESS; }
void* operator new[](size_t size) { return __kmalloc(size); }
Region::Region(void) { // Initialize the window list rect_list = (LinkedList *)__kmalloc(sizeof(LinkedList)); list_init(rect_list, __kfree); }
/** * @brief Allocate a chuck of memory of size <size> * * @return * NULL if the memory could not be allocated * otherwise return the address of the chunk */ void *kmalloc(size_t size) { return __kmalloc(size); }