Multitasking in flat memory model

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
yee1
Member
Member
Posts: 42
Joined: Sun Feb 10, 2013 4:02 pm

Multitasking in flat memory model

Post by yee1 »

Hey,

I want to use flat memory model so there is 1 big segment where
CS=DS and memory size is 4 GB.

But... how about multitasking here ? While I was using tiny memory model (segments) i was preparing new TSS record for GDT table and load it with lgdt instruction. How about tiny memory model ? So far I have found out that I should use paging mechanism (CR3). But what about:
- privilege levels that before I had supported by TSS descriptor
- memory protection, example: TSS1 can not access TSS2 data

How to deal with it ? Please give me some explanations, thank you very much!
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Multitasking in flat memory model

Post by iansjack »

Privelege levels - just use two: kernel and userland.

Memory protection - paging provides that. A task can only see memory that is mapped in its page table.
yee1
Member
Member
Posts: 42
Joined: Sun Feb 10, 2013 4:02 pm

Re: Multitasking in flat memory model

Post by yee1 »

iansjack wrote:Privelege levels - just use two: kernel and userland.

Memory protection - paging provides that. A task can only see memory that is mapped in its page table.
Memory protection ok

Privelege levels why?

What if I want user application (example task TSS1) to access text video memory at 0xB8000 to write some message and others like access some sort of pc peripherials via task (userland), what then ? If TSS1 may only access some memory provided by paging mechanism then how to get access to 0xB8000 and others memory's area if needed ? How to provide that ?


1 more question. How to jump to new task when no TSS descriptors in GDTR ?
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Multitasking in flat memory model

Post by sortie »

It's called system calls and abstractions. A kernel shouldn't allow programs raw access to the hardware, indeed that would be insane as they would have to provide all the kernel services themselves, poor. (Microkernel drivers are special) Instead, all programs would work through the kernel's designated services.

Paging is what allows memory protection. And yes, you can use paging to allow programs access to memory mapped IO buffers such as 0xB8000.

Repeat after me: Segmentation bad, paging awesome. Paging is doubleplusgood!

Also note that hardware task switching with the TSS is obsolete; it has been removed from 64-bit long mode on x86_64 CPUs. No modern operating system uses it - instead it does software context switching, which reportedly is much faster. It may also help you understand the process as software context switching means you do things yourself.
User avatar
summersong
Member
Member
Posts: 32
Joined: Sat Mar 26, 2011 5:26 am
Location: Moscow

Re: Multitasking in flat memory model

Post by summersong »

I think about that.

Code: Select all

org 0 ; <------ PROBLEM
mov esi,msg_text
int 90h ; syscall

mov esi,msg_text
mov edi,msg_buf
mov ecx,6
rep movsb

jmp $

msg_buf db 'task X',0
msg_text db 'task 1',0
You can do something like this:

Code: Select all

org 0
mov [reloc_offs],esi ; esi is setting by kernel and passed to task

mov esi,msg_text
add esi,[reloc_offs]
int 90h ; syscall

mov esi,msg_text
add esi,[reloc_offs]
mov edi,msg_buf
add edi,[reloc_offs]
mov ecx,6
rep movsb

jmp $

reloc_offs dd 0
msg_buf db 'task X',0
msg_text db 'task 1',0
All apps must be use reloc_offs. I think, that's isn't right.
Post Reply