Skip to content

Commit

Permalink
Corrigi alguns bugs com a heap, devido a inicialização estar errada.
Browse files Browse the repository at this point in the history
Me livrei de algumas coisas inuteis tb.
  • Loading branch information
psychomantys committed Mar 12, 2013
1 parent e015dae commit 1997a76
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 48 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ FILE(GLOB GENERIC_KERNEL_SOURCES
"src/kernel/*.s"
"src/kernel/*.cpp"
"src/kernel/kpp/*.cpp"
"src/kernel/fs/*.cpp"
# "src/kernel/*.c"
)

Expand Down
7 changes: 1 addition & 6 deletions src/kernel/arch/i386/start.s
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ multiboot_addr:
.globl multiboot_addr
.zero 4

end_malloc_addr:
.globl end_malloc_addr
.zero 4

start:
call disable_interrupts

movl %eax, multiboot_magic
movl %ebx, multiboot_addr

call disable_interrupts

call _at_global_begin
call main
call _at_global_end
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/boot/multiboot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct multiboot_header{
multiboot_uint32_t width;
multiboot_uint32_t height;
multiboot_uint32_t depth;
};
} __attribute__((packed)) ;

/* The symbol table for a.out. */
struct multiboot_aout_symbol_table
Expand Down Expand Up @@ -195,7 +195,7 @@ struct multiboot_info
multiboot_uint16_t vbe_interface_seg;
multiboot_uint16_t vbe_interface_off;
multiboot_uint16_t vbe_interface_len;
};
} __attribute__((packed)) ;
typedef struct multiboot_info multiboot_info_t;

struct multiboot_mmap_entry
Expand Down Expand Up @@ -225,7 +225,7 @@ typedef struct multiboot_mod_list multiboot_module_t;

#endif /* ! ASM_FILE */

extern struct multiboot_header *multiboot_addr;
extern multiboot_info_t *multiboot_addr;
extern uint32_t multiboot_magic;

#endif /* ! MULTIBOOT_HEADER */
10 changes: 6 additions & 4 deletions src/kernel/compiler/gcc/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
#include <kernel/debug.hpp>
#include <kernel/kmalloc.hpp>

#include <isa_specific_code.hpp>
#include <plataform_specific_code.hpp>

void *operator new[](unsigned int s){
void *p=(void*)(kmalloc(s));
if( ! p ){
kdebug(2,"Bad alloc %d\n",s);
kdebug(3,"Bad alloc %d\n",s);
}
// kprintf("p=%p\n",p);
return p;
}

void *operator new(unsigned int s){
void *p=(void*)(kmalloc(s));
if( ! p ){
kdebug(2,"Bad alloc %d\n",s);
}
kdebug(3,"Bad alloc %d\n",s);
}
return p;
}

Expand Down
4 changes: 3 additions & 1 deletion src/kernel/kheap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ uint32_t KHeap::find_smallest_hole(const uint32_t &size, const bool &page_align)

static KHeap *kheap_kfree_handler=0;

void kheap_kfree(void *p){
void kheap_kfree(uint32_t p){
kdebug(8,"KHeap *kheap_kfree_handler=%p\n",kheap_kfree_handler);
kheap_kfree_handler->free(p);
}
Expand All @@ -81,6 +81,8 @@ KHeap::KHeap(Paging &paging ) :
void KHeap::install(uint32_t start, const uint32_t &end, const uint32_t &max, const bool &supervisor, const bool &readonly)
{
index=new Ordered_array<header_t*>( (void*)(start), KHEAP_INDEX_SIZE );
//// index=(Ordered_array<header_t*>*)( kmalloc( sizeof(index) ) );
//// index->init( (void*)(start), KHEAP_INDEX_SIZE );
// heap_t *heap = (heap_t*)( kmalloc(sizeof(heap_t)) );

// All our assumptions are made on startAddress and endAddress being page-aligned.
Expand Down
3 changes: 3 additions & 0 deletions src/kernel/kheap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class KHeap{
Releases a block allocated with 'alloc'.
**/
void free(void *p);
/* ~KHeap(){
delete index;
}*/
};

#endif /* ----- #ifndef kheap_INC ----- */
Expand Down
32 changes: 21 additions & 11 deletions src/kernel/kmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,34 @@
#include <kernel/monitor.hpp>
#include <kernel/boot/multiboot.hpp>

uint32_t KMalloc::static_end_kmalloc_addr;

static kmalloc_handler_t kmalloc_handler=0;
static kfree_handler_t kfree_handler=0;

inline uint32_t kmalloc_basic(const uint32_t &sz, uint32_t *phys, const bool &align){
if( align && (end_malloc_addr & 0xFFFFF000) ){ // If the address is not already page-aligned
if( align && ( end_malloc_addr()&0xFFFFF000 ) ){ // If the address is not already page-aligned
// Align it.
end_malloc_addr &= 0xFFFFF000;
end_malloc_addr += 0x1000;
set_end_malloc_addr( end_malloc_addr()&0xFFFFF000 );
set_end_malloc_addr( end_malloc_addr()+0x1000 );
}
if (phys){
*phys = end_malloc_addr;
*phys=end_malloc_addr();
}
uint32_t tmp = end_malloc_addr;
end_malloc_addr += sz;
uint32_t tmp=end_malloc_addr();
set_end_malloc_addr( end_malloc_addr()+sz );
return tmp;
}

inline uint32_t kmalloc_int(const uint32_t &sz, uint32_t *phys, const bool &align){
uint32_t ret;

if( kmalloc_handler ){
ret=kmalloc_handler(sz, phys, align);
// kprintf("kmalloc_int: Alloc! %p\n",ret);
return ret;
}else{
ret=kmalloc_basic(sz, phys, align);
}
ret=kmalloc_basic(sz, phys, align);
// kprintf("kmalloc_int: Alloc! %p\n",ret);
// kprintf("kmalloc_int: sz=%p ret=%p kmalloc_handler=%p\n",sz,ret,&kmalloc_handler);
return ret;
}

Expand All @@ -56,7 +58,7 @@ inline void kfree_basic(uint32_t addr){

inline void kfree_int(const uint32_t sz){
kdebug(8,"free_int:\n");
if( sz<=end_malloc_addr ){
if( sz<=end_malloc_addr() ){
kdebug(2,"kfree_int: Not free %u<=%u!\n",sz,end_malloc_addr);
return;
}
Expand Down Expand Up @@ -109,5 +111,13 @@ extern "C" {
kfree_handler_t kfree_get_handler(){
return kfree_handler;
}
uint32_t end_malloc_addr(){
return KMalloc::static_end_kmalloc_addr;
// return static_end_kmalloc_addr;
}
void set_end_malloc_addr(uint32_t p){
KMalloc::static_end_kmalloc_addr=p;
// static_end_kmalloc_addr=p;
}
}

19 changes: 17 additions & 2 deletions src/kernel/kmalloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@

#include <stdint.h>
#include <kernel/boot/multiboot.hpp>
#include <kernel/kpp/utils.hpp>

class KMalloc{
declare_a(uint32_t,end_of_memory);

private:
public:
KMalloc( const uint32_t &p ){
end_of_memory(p);
static_end_kmalloc_addr=p;
}
static uint32_t static_end_kmalloc_addr;
};

extern "C" {
extern uint32_t end_malloc_addr;

typedef uint32_t (*kmalloc_handler_t)(const uint32_t &sz, uint32_t *phys, const bool &align);
typedef uint32_t (*kfree_handler_t)(const uint32_t addr);
typedef void (*kfree_handler_t)(const uint32_t addr);

// page aligned.
uint32_t kmalloc_a(const uint32_t &sz);
Expand All @@ -29,6 +41,9 @@ extern "C" {

void kfree_set_handler(kfree_handler_t new_handler);
kfree_handler_t kfree_get_handler();

uint32_t end_malloc_addr();
void set_end_malloc_addr(uint32_t p);
}

#endif /* ----- #ifndef kmalloc_INC ----- */
Expand Down
9 changes: 9 additions & 0 deletions src/kernel/kpp/ordered_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Ordered_array{
// typedef value_type data_t;
Ordered_array( const size_t &max_size );
Ordered_array( const void *addr, const size_t &max_size );
void init( const void *addr, const size_t &max_size );
data_t &operator[]( const size_t &pos );
void remove( size_t index );
void insert(const data_t &data);
Expand All @@ -43,6 +44,14 @@ Ordered_array<data_t>::Ordered_array( const void* addr, const size_t &max_size )
this->max_size(max_size);
}

template<class data_t>
void Ordered_array<data_t>::init( const void* addr, const size_t &max_size ){
base=(data_t*)(addr);
memset( base, 0, max_size*sizeof(base));
size(0);
this->max_size(max_size);
}

template<class data_t>
data_t &Ordered_array<data_t>::operator[]( const size_t &pos ){
ASSERT( pos<size() );
Expand Down
30 changes: 16 additions & 14 deletions src/kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ class t{
}
};

KMalloc auxxx( end_malloc_addr() );

t x1(1);

GDT gdt;
IDT idt;

Paging paging(0x2000000, idt, KHEAP_START, KHEAP_INITIAL_SIZE);
KHeap kheap(paging);

IRQ irq( idt );
Timer pit( irq );
Keyboard kb( pit, irq );

int main(){
Paging paging(0x1000000, idt, KHEAP_START, KHEAP_INITIAL_SIZE);
KHeap kheap(paging);

int main(){
if( multiboot_magic!=MULTIBOOT_BOOTLOADER_MAGIC ){
kprintf("multiboot Magic number wrong! (%p)\n", multiboot_addr);
halt_machine();
Expand All @@ -54,15 +55,17 @@ int main(){
idt.install();

char *a1 = new char[8];
paging.install();

kheap.install(KHEAP_START, KHEAP_START+KHEAP_INITIAL_SIZE, 0xCFFFF000, false, false);

irq.install();
pit.install();
kb.install();

paging.install();

kheap.install(KHEAP_START, KHEAP_START+KHEAP_INITIAL_SIZE, 0xCFFFF000, false, false);

enable_interrupts();
// kb.getch();

/*
uint32_t *ptr = (uint32_t*)0xA0000000;
Expand All @@ -86,7 +89,6 @@ int main(){
char *x=new char[10];
kprintf("x=%p\n",x);
// DQueue<char> s;
// kb.getch();
x[0]='A';
x[1]='A';
x[2]='A';
Expand All @@ -102,8 +104,8 @@ int main(){
s.push('\n');
s.push('\0');

x1.print();
x1.print();
// x1.print();
// x1.print();
for( int i=0 ; not s.is_empty() ; ++i ){
x[i]=s.pop() ;
}
Expand All @@ -127,13 +129,13 @@ int main(){

t x2(2);
t x3(3);
x1.print();
// x1.print();
x3.print();

t *a=new t[2];
int *p_t2=new int[40];

x1.print();
// x1.print();
x3.print();

kprintf("Vai um delete?\n");
Expand All @@ -147,10 +149,10 @@ int main(){

kprintf("Foi passou delete!!\n");

x1.print();
// x1.print();
x2.print();

kprintf("Final do main kernel!!!!! %p\n",&x1);
// kprintf("Final do main kernel!!!!! %p\n",&x1);
kprintf("Final do main kernel!!!!!\n");

t tfim(15);
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/paging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void Paging::alloc_frame(page_t *page, const bool &is_kernel, const bool &is_wri
return; // Frame was already allocated, return straight away.
}else{
size_t idx = frames.first(); // idx is now the index of the first free frame.
if( idx==(size_t)-1 ){
if( idx==(size_t)(-1) ){
// PANIC is just a macro that prints a message to the screen then hits an infinite loop.
PANIC("No free frames!");
}
Expand Down
7 changes: 3 additions & 4 deletions src/kernel/paging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,25 @@ class Paging{
const uint32_t &KHEAP_INITIAL_SIZE
) :
frame_size(0x1000),
frame_qtd(0x2000000/frame_size),
frame_qtd(mem_size/frame_size),
frames(frame_qtd),
idt(idt)
{
kernel_directory = (page_directory_t*)kmalloc_a(sizeof(page_directory_t));
memset(kernel_directory, 0, sizeof(page_directory_t));
current_directory = kernel_directory;

uint32_t i=0;

// Map some pages in the kernel heap area.
// Here we call get_page but not alloc_frame. This causes page_table_t's
// to be created where necessary. We can't allocate frames yet because they
// they need to be identity mapped first below, and yet we can't increase
// placement_address between identity mapping and enabling the heap!
uint32_t i=0;
for (i = KHEAP_START; i<KHEAP_START+KHEAP_INITIAL_SIZE; i += 0x1000)
get_page(i, 1, kernel_directory);

i=0;
while( i<end_malloc_addr+0x1000 ){
while( i<end_malloc_addr()+0x1000 ){
// Kernel code is readable but not writeable from userspace.
alloc_frame( get_page(i, 1, kernel_directory), false, false);
i += this->frame_size;
Expand Down
Loading

0 comments on commit 1997a76

Please sign in to comment.