OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 12:34 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Variable is not changed after an assignment
PostPosted: Wed Sep 14, 2022 12:50 pm 
Offline

Joined: Wed Sep 14, 2022 12:42 pm
Posts: 10
Hi all,

I am writing an OS for learning purpose
I have an issue when making an assignment in C++. The code is below

Quote:
void* MemoryManager::makeMemoryEntry(void* prevAddress, uint16 size) {
Printer::printlnAddress((uint64)this);
MemoryEntry *prev = (MemoryEntry*)prevAddress;
MemoryEntry *entry;
void* entryAddress = prevAddress + sizeof(MemoryEntry) + prev->size;
entry = (MemoryEntry*)entryAddress;
entry->size = size;
entry->next = prev->next;
if (entry->next != prev->next) {
Printer::println("W T F",5);
}
uint64 *next = (uint64*)entryAddress;
prev->next = entry;
return entry;
}


In the assignment "entry->next = prev->next;". It does not work, entry->next keep the old value. Here is the source code https://github.com/nguyenzung/kos/blob/ ... anager.cpp
Many thanks


Last edited by zungnguyen on Wed Sep 14, 2022 8:40 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Wed Sep 14, 2022 8:20 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
What are the values of prevAddress and entryAddress? Are those addresses marked as usable memory in your memory map?


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Wed Sep 14, 2022 8:36 pm 
Offline

Joined: Wed Sep 14, 2022 12:42 pm
Posts: 10
Octocontrabass wrote:
What are the values of prevAddress and entryAddress? Are those addresses marked as usable memory in your memory map?


They are stored on heap space. Their value is set correctly. entryAddress is address of the memory that the OS provide when user call malloc, prevAddress is provided chuck that will point to the entryAddress. And it is in range of the memory map. I created 64MB for initialized.
I found another situation. In my construct. I can set the variable if changing the code from " first = (MemoryEntry*) this->makeFirstMemoryEntry(0xffff);" to " first = (MemoryEntry*) this->makeFirstMemoryEntry(0xafff);". If the value is less than 0xafff. The stuff works correctly

I also turn off the interrupt handler to make sure there is only this function is running.

Quote:
MemoryManager::MemoryManager() {
kernelHeapAddress = heapBase;
kernelStackBase = stackBase;
Printer::printlnAddress((uint64)kernelHeapAddress);
first = (MemoryEntry*) this->makeFirstMemoryEntry(0xffff);
MemoryManager::setInstance(this);
}


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Wed Sep 14, 2022 10:30 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
zungnguyen wrote:
They are stored on heap space.

What range of addresses is your heap space?

zungnguyen wrote:
Their value is set correctly.

What values specifically?

zungnguyen wrote:
And it is in range of the memory map.

Can I see your memory map?


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Thu Sep 15, 2022 12:32 am 
Offline

Joined: Wed Sep 14, 2022 12:42 pm
Posts: 10
Quote:
What range of addresses is your heap space?

My heap space start at: 0x93000
My stack top at 0x4093000
I define like that [Heap] -> My running OS <- [stack top]. There are 64M for my OS running.
Quote:
section .bss
global stack_base
global heap_base
heap_base: resb 64*1024*1024
stack_base:
stack_size equ $ - heap_base



Quote:
What values specifically?

The prevAddress is 0xA3009
The entryAddress is 0xB3012. I expect the entryAddress should be 0xA3009 + sizeof(MemoryEntry) + allocated size of prev, should be 0xA3009 + 10 + 6
Maybe the entryAddress is protected by another stuff, so that i cannot change the value.

zungnguyen wrote:
And it is in range of the memory map.

Quote:
Can I see your memory map?
Here my mapping code: https://github.com/nguyenzung/kos/blob/ ... ctedMode.s
I index for around 256MB

I found that in linker script, my old code is:
Quote:
ENTRY(start)
SECTIONS
{
. = 50000;
.text : { KEEP(*(.multiboot)) *(.text) }
.data : { *(.rodata*) *(.data*) KEEP(*(.init_array))}
.bss : { *(.bss) }
}

If i change to:
Quote:
ENTRY(start)
SECTIONS
{
. = 1M;
.text : { KEEP(*(.multiboot)) *(.text) }
.data : { *(.rodata*) *(.data*) KEEP(*(.init_array))}
.bss : { *(.bss) }
}

It seems now work correctly. But i am not sure about the root cause.


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Thu Sep 15, 2022 9:09 am 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 852
You are mapping addresses 0 to 0x200000 to the first two megabytes of the physical address space. So, for example, virtual address 0xa3009 gets translated as physical address 0xa3009, which is of course not what you want, as it does not even point to system memory. You need to set up a mapping from the executable's linked address to the address where it actually resides in memory.


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Thu Sep 15, 2022 11:13 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
zungnguyen wrote:
But i am not sure about the root cause.

There was no RAM at the addresses you were trying to use.

GRUB provides a memory map. You need to use that memory map to find available RAM.


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Thu Sep 15, 2022 9:13 pm 
Offline

Joined: Wed Sep 14, 2022 12:42 pm
Posts: 10
Quote:
You are mapping addresses 0 to 0x200000 to the first two megabytes of the physical address space. So, for example, virtual address 0xa3009 gets translated as physical address 0xa3009, which is of course not what you want, as it does not even point to system memory. You need to set up a mapping from the executable's linked address to the address where it actually resides in memory.


Actually, i mapped 256MB for the kernel. I just change the start address in linker at 1M to solve the problem. Thanks!

Octocontrabass wrote:
zungnguyen wrote:
But i am not sure about the root cause.

There was no RAM at the addresses you were trying to use.

GRUB provides a memory map. You need to use that memory map to find available RAM.


Thanks!


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Fri Sep 16, 2022 6:10 am 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 852
The problem isn't really solved, since the Multiboot2 header doesn't inform the loader of where the image needs to be loaded. This is going to cause surprises later on.


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Fri Sep 16, 2022 8:44 am 
Offline
Member
Member

Joined: Wed Oct 01, 2008 1:55 pm
Posts: 3191
I think 0xA0000 to 0xFFFFF is reserved for BIOS, and so will not work as RAM. Of course, assuming linear addresses are equal to physical. In the absence of memory information, you should put the heap at 0x110000 or something.


Top
 Profile  
 
 Post subject: Re: Variable is not changed after an assignment
PostPosted: Fri Sep 16, 2022 12:52 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Gigasoft wrote:
The problem isn't really solved, since the Multiboot2 header doesn't inform the loader of where the image needs to be loaded.

The loader uses the ELF header.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Sa41848 and 108 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group