Updating TLB

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
nickie
Posts: 7
Joined: Fri Nov 16, 2012 11:22 am

Updating TLB

Post by nickie »

Hello people. I haven't been here for a while, but in the last fre days I tough I would give osdev a second chance and I really made some progress with my OS.

However I'm stuck at the most stupid problem I have ever had...
I cannot update the tlb. Below you can see my nasm code I call from C after I make some changes in the current paging structures.
I'm becomming paranoid so can you "confirm" if this code is valid?

Code: Select all

flush_pages:
	push ebp
	mov ebp, esp
	mov eax, cr3
	mov cr3, eax
	pop ebp
	ret
Thank you.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Updating TLB

Post by Nable »

1. Sorry but it seems to be a bad copy-paste. Why did you add frame-pointer operations when you don't use stack frame?
2. AFAIR, this trick flushes the whole TLB only on 80386. On later CPUs it doesn't flush information about pages with 'global' flag.
3. Complete TLB flush should be avoided as much as possible. It's better to use INVLPG for specific pages.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Updating TLB

Post by sortie »

Yep. Reloading CR3 with the same value does clear the TLB (you are effectively reloading the same address space). As Nabie warns, this doesn't clear global pages, but that's the purpose of global pages: That they don't get invalidated when switching address spaces, because they are, you know, global.

Beware the topic name: On some CPUs you manually 'update' the TLB in software, while on x86 this is entirely automatic. This topic should be called 'Invalidating TLB' or 'Flushing TLB' or something.
nickie
Posts: 7
Joined: Fri Nov 16, 2012 11:22 am

Re: Updating TLB

Post by nickie »

Thank you all.
It seems my tables are being updated. I guess my algorithm is writing at address that is not mapped.
This is not causing fault.

Code: Select all

	void* phys = kalloc_frame();
	set_page(0x2000, phys, get_current_directory());
	int* a = 0x2000;
	*a = 10;
But this is, even if I haven't manually updated the tlb( soon, I will update it with set_page, too ):

Code: Select all

	void* phys = kalloc_frame();
	//set_page(0x2000, phys, get_current_directory());
	int* a = 0x2000;
	*a = 10;
Post Reply