problems with .bss and my memory manager..

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

problems with .bss and my memory manager..

Post by earlz »

I have 2 prioblems..

1. My .bss section doesn't work and I have no idea why, here's my makefile and linker script..

Code: Select all

link command is ld -T link.ld -o jouleos.bin asm.o .objs/kernel/main.o..(more objects)

linker script:
ENTRY(asm.o)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    _code = .;
    *(.text)
    . = ALIGN(4096);
  }
  .data : AT(phys + (_data - _code))
  {
    _data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (_bss - _code))
  {
    _bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}

btw using MinGW


2. Even when I force it to not use .bss for variables my memory manager still doesn't completely work!
theirs really not an easy way to put it..

Code: Select all



inline unsigned char SetDataBit(void *data,unsigned int bit,unsigned int set){ //set should be 1 to set the bit to 1, or 0 to set the bit to 0
    unsigned int *ptr=data; //we do int for 32bit optimization
    ptr=ptr+(bit/32);
    if(set==0){
        *ptr=*ptr&(0xFFFFFFFF^(1<<(bit%32)));
    }else{
        *ptr=*ptr|(1<<(bit%32));
    }

    return *ptr;
}

inline unsigned char GetDataBit(void *data,unsigned int bit){
    unsigned int *ptr=data;
    ptr=ptr+(bit/32);
    return *ptr&(1<<(bit%32));
}

void SetDataBits(void *data,unsigned int base,unsigned int limit,unsigned int set){
    for(base=base;base<=limit;base++){
        SetDataBit(data,base,set);
    }
}

//set should be 1 if your searching for 0 bits and want to set them to 1 or 0 if your looking for 1 bits and want to set them to 0
// the 0 bit should be SET, as if it tries to set this bit and returns it, then it will make the caller think that their was no space
//otherwise though this returns 0 for no space, or the base bit set
unsigned int SetFreeBits(void *data,unsigned int base,unsigned int limit,unsigned int number,unsigned int set){ //set determines which way you want to set it..
    unsigned int tmp;
    unsigned int i;
    unsigned char base_for;
    unsigned char is_free=0;
    tmp=number;
    for(i=base;i<=limit;i++){
        if(GetDataBit(data,i)==(set^1)){
            if(is_free==0){base_for=i;}
            is_free=1;
            }else{
            base_for=0;
            is_free=0;
            }
        if(is_free==1){tmp--;}else{tmp=number;}
        if(tmp==0){
            SetDataBits(data,base_for,base_for+number,set);
            return base_for;
        }
    }
    return 0;
}


//this override functions let you alloc or free a range of addresses..
unsigned int override_AllocCore(AddressSpace *space, unsigned int base,unsigned int number){
    if((base+number)>space->limit){return 0;}
    SetDataBits(space->address,base,number,1);
    return base;
}
unsigned int FreeCore(AddressSpace *space,unsigned int base,unsigned int number){
    if((base+number)>space->limit){return 0;}
    SetDataBits(space->address,base,number,0);
    return base;
}


unsigned int AllocCore(AddressSpace *space, unsigned int number){
    return SetFreeBits(space->address,0,space->limit,number,1); //yea simple..
}
void SetupAddressSpace(AddressSpace *space,void *address,unsigned int limit,unsigned char uses_paging,void *owner){
    space->address=address;
    memset(space->address,0,(limit/32)+1);
    //putc_tmp('2');
    space->limit=limit;
    space->page_addressing=uses_paging;
    space->owner=owner;
    AllocCore(space,1); //allocate first bit..
}
unsigned int virtual_address[32768]={0};
unsigned int physical_address[32768]={0};

AddressSpace physical_memory;
AddressSpace virtual_memory;

void InitMemMan(){
    //putc_tmp("h");
    //I 0xFFFF FFFF isn't right!?
    SetupAddressSpace(&physical_memory,physical_address,0xFFFFF,1,0); //eventually change the limit to the size of physical memory..
    SetupAddressSpace(&virtual_memory,virtual_address,0xFFFFF,1,0);
    override_AllocCore(&physical_memory,0,0x20000/4096); //allocate first 2 megs
    //This below is only for testing
   if(AllcCore(&virtual_address,20)==0){__asm("hlt");}else{putc_tmp('h');}


}


when I tested it under windows, it triple faulted in GetDataBit() whenever it tried to get the contents of ptr(using *ptr) and then return it..(it was called by AllocCore)
User avatar
gaf
Member
Member
Posts: 349
Joined: Thu Oct 21, 2004 11:00 pm
Location: Munich, Germany

Post by gaf »

Maybe another asterix already solves your first issue:

Code: Select all

.bss : AT(phys + (_bss - _code)) 
  { 
    _bss = .; 
    *(.bss*) 
    . = ALIGN(4096); 
  }
cheers,
gaf
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

omg! I feel so stupid!!! stupid stupid stupid!!

I have spent 2 hours trying to fix the memory manager when the memory manager is doing everything right(as it's suppose to)...
I have been calling AllocCore() with physical_memory (which is a struct, and not pointer to a struct) when I should have been using &physical_memory...

/me kicks self in face

edit:
I still have the .bss problem though.. (gaf, that thing didn't work)


edit2:
I don't know if it matters but mingw's linker by default outputs .exe(PE) and if I put --oformat binary in it then it says "PE operations on non-PE file" so I just let it put it out in PE and then use objcopy to convert it to binary, would that affect the sections any?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

hckr83 wrote:omg! I feel so stupid!!! stupid stupid stupid!!

I have spent 2 hours trying to fix the memory manager when the memory manager is doing everything right(as it's suppose to)...
I have been calling AllocCore() with physical_memory (which is a struct, and not pointer to a struct) when I should have been using &physical_memory...
Try enabling a lot more warnings. Warnings aren't the compiler whining that you can't program, it's the compiler telling you that something looks fishy and that you might've overlooked it.
User avatar
gaf
Member
Member
Posts: 349
Joined: Thu Oct 21, 2004 11:00 pm
Location: Munich, Germany

Post by gaf »

I just let it put it out in PE and then use objcopy to convert it to binary, would that affect the sections any?
The idea behind the bss section is to reduce the executable size by creating unitialized variables at runtime. As the value of all unitialized data is initially zero the bss just stores the total amount of this data. The zeroes therefore aren't included in the executable but get created by the loader when the application is started.

As the "binary" format doesn't include any headers the loader can't possible know how big your bss section must be. It's thus your job to set the section that is meant to be used for uninitialized variables to zero. A loop copying zeroes to the area between "_bss" and "end" should already do the job.

regards,
gaf
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

I think it's mingw though..I tried just have my linker script say to set .text and erased eevrything about .bss and .data and it still said "pe operations on a non-pe file" so appearntly you can't set sections on binary files?
TheQuux
Member
Member
Posts: 73
Joined: Sun Oct 22, 2006 6:49 pm

Post by TheQuux »

In order to use things like sections, you need a format with headers, which flat binary certainly isn't. If you're using Grub, make sure your multiboot header is correct (you need to specify the address section), or just use ELF (which will require a custom toolchain... look up gcc and binutils.)

As for the .bss section and why it's not working, executable formats require load time support. Unfortunately, as it's not likely that Grub (or whatever bootloader you're using) understands PE, your bss section is essentially doing nothing (and, with flat binary, it certainly isn't). Essentially, addresses in the .bss section point past the end of the kernel, so they aren't necessarily zero. (as gaf said)

hope that helped.
My project: Xenon
Post Reply