void *cgc_malloc_realloc(malloc_t *heap, void *ptr, cgc_size_t n) { void *new_ptr; if (n > MAX_SIZE) return NULL; if (n == 0) { malloc_free(heap, ptr); return NULL; } if (ptr == NULL) return cgc_malloc_alloc(heap, n); if (cgc_malloc_size(heap, ptr) >= n) return ptr; new_ptr = cgc_malloc_alloc(heap, n); if (new_ptr == NULL) return NULL; cgc_memcpy(new_ptr, ptr, cgc_malloc_size(heap, ptr)); malloc_free(heap, ptr); return new_ptr; }
void symbol_restore(struct kernsym *sym) { bool pte_ro; if (sym->hijacked) { set_addr_rw((unsigned long) sym->addr, &pte_ro); memcpy(sym->addr, &sym->orig_start_bytes[0], OP_JMP_SIZE); set_addr_ro((unsigned long) sym->addr, pte_ro); sym->hijacked = false; malloc_free(sym->new_addr); } if (sym->name_alloc) { malloc_free(sym->name); sym->name_alloc = false; } return; }
void schedule() { struct pcb_s* pcb; struct pcb_s* pcb_init; pcb_init = current_process; pcb = current_process; /* Start by eliminating all zombies (rhaaaaa !) */ while (pcb->next->state == TERMINATED) { /* If no process to be executed -> note that and leave loop */ if (pcb->next == pcb) { pcb = NULL; break; } else { /* Particular case of the head */ if (pcb->next == ready_queue) ready_queue = pcb; /* Remove pcb from the list (FIXME : could be done after the loop) */ pcb->next = pcb->next->next; /* Free memory */ malloc_free((char*) pcb->next->stack_base); malloc_free((char*) pcb->next); /* Get next process */ pcb = pcb->next; } } if (pcb != NULL) { /* On parcours la liste jusqu'à trouver un processus non bloqué */ pcb = pcb->next; while(pcb->state == WAITING && pcb != pcb_init) pcb = pcb->next; /* Si tous les processus sont bloqués -> on le note */ if(pcb->state == WAITING) pcb = NULL; } if(pcb == NULL) { /* Si pas de processus à élire -> idle */ ready_queue = NULL; current_process = &idle; } else { /* Sinon -> le processus élu est trouve par l'algorithme RMS*/ select_next(pcb); //current_process = pcb; } }
int mutex_free(struct mutex_s * mutex) { /* Vérification des paramètres */ if (mutex == NULL) { return -1; } malloc_free(mutex); return 0; }
struct mutex_s * mutex_create() { struct mutex_s * mutex = (struct mutex_s *) malloc_alloc( sizeof(struct mutex_s)); if (mutex == NULL) { return NULL; } if (mutex_init(mutex) == -1) { malloc_free(mutex); return NULL; } return mutex; }
void sem_up(struct sem_s* sem) { DISABLE_IRQ(); if (sem->waiting > 0) { // We take the first waiting process struct waiting_process* first_process = sem->queue; // Remove it from the top of the waiting list sem->queue = first_process->next; sem->waiting -= 1; // Put the process back in the active list put_back(first_process->process); // And free the allocated memory to its position in the queue malloc_free((uint32_t*) first_process); } else { sem->val += 1; } ENABLE_IRQ(); }
void cgc_free(void *ptr) { return malloc_free(&g_heap, ptr); }
int symbol_hijack(struct kernsym *sym, const char *symbol_name, unsigned long *code) { int ret; unsigned long orig_addr; unsigned long dest_addr; unsigned long end_addr; u32 *poffset; struct insn insn; bool pte_ro; ret = find_symbol_address(sym, symbol_name); if (IN_ERR(ret)) return ret; if (*(u8 *)sym->addr == OP_JMP_REL32) { printk(PKPRE "error: %s already appears to be hijacked\n", symbol_name); return -EFAULT; } sym->new_addr = malloc(sym->size); if (sym->new_addr == NULL) { printk(PKPRE "Failed to allocate buffer of size %lu for %s\n", sym->size, sym->name); return -ENOMEM; } memset(sym->new_addr, 0, (size_t)sym->size); if (sym->size < OP_JMP_SIZE) { ret = -EFAULT; goto out_error; } orig_addr = (unsigned long)sym->addr; dest_addr = (unsigned long)sym->new_addr; end_addr = orig_addr + sym->size; while (end_addr > orig_addr && *(u8 *)(end_addr - 1) == '\0') --end_addr; if (orig_addr == end_addr) { printk(PKPRE "A spurious symbol \"%s\" (address: %p) seems to contain only zeros\n", sym->name, sym->addr); ret = -EILSEQ; goto out_error; } while (orig_addr < end_addr) { tpe_insn_init(&insn, (void *)orig_addr); tpe_insn_get_length(&insn); if (insn.length == 0) { printk(PKPRE "Failed to decode instruction at %p (%s+0x%lx)\n", (const void *)orig_addr, sym->name, orig_addr - (unsigned long)sym->addr); ret = -EILSEQ; goto out_error; } copy_and_fixup_insn(&insn, (void *)dest_addr, sym); orig_addr += insn.length; dest_addr += insn.length; } sym->new_size = dest_addr - (unsigned long)sym->new_addr; sym->run = sym->new_addr; set_addr_rw((unsigned long) sym->addr, &pte_ro); memcpy(&sym->orig_start_bytes[0], sym->addr, OP_JMP_SIZE); *(u8 *)sym->addr = OP_JMP_REL32; poffset = (u32 *)((unsigned long)sym->addr + 1); *poffset = CODE_OFFSET_FROM_ADDR((unsigned long)sym->addr, OP_JMP_SIZE, (unsigned long)code); set_addr_ro((unsigned long) sym->addr, pte_ro); sym->hijacked = true; return 0; out_error: malloc_free(sym->new_addr); return ret; }