flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Interrupting in protected mode help

Author
Thread Post new topic Reply to topic
blueone



Joined: 01 Apr 2007
Posts: 10
blueone 01 Jan 2008, 10:30
I've read the different modes of cpu from real to long mode. I am using protected mode to build my own kernel but i need some help regarding interrupts.

As far i as i know, one can only use interrupt while in real/unreal mode. I am really getting confused about this because according to many tutorials that i've read before, you must switch to real mode first before interrupting i/o, disks, videos and other peripherals. Does it mean that i need to switch back to real mode first before i use interrupts? Im getting confused because i dont know if switching from PM to RM and RM to PM causes crash or SLOWS down a kernel which i am avoiding to happen. Like when a applications in my OS simultaneously reads file, other one flipped page of screen buffer and the other one do other interrupts, will cpu crash when continuously switching modes? Any idea how slow is switching? or some idea to handle interupts in a protected mode kernel?

Thanks in advance Razz
Post 01 Jan 2008, 10:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 01 Jan 2008, 10:46
blueone: I think you are trying to run when you can't yet walk. Try to slow down and make a RM OS as a starting point. You will learn a lot about things like interrupts by doing that. Then you can progress to a PM OS. Many of the same principles will apply and you only have the incremental step of the PM layer. After that, comes the full PM 32bit drivers, they can be applied one-by-one, each device is gradually supported by a PM driver until you suddenly find you have a full PM mode OS.

However, if you really want to carry on by jumping straight to PM interrupts and others things then have a look at the DOS mode code in the fasm download. It uses unreal mode and filters the interrupts through a layer into the 16bit DOS code. Try to understand it fully before you proceed further. You will need that base knowledge to help you understand what is happening inside the CPU.
Post 01 Jan 2008, 10:46
View user's profile Send private message Visit poster's website Reply with quote
blueone



Joined: 01 Apr 2007
Posts: 10
blueone 01 Jan 2008, 11:03
OH you again hehehe, you always respond to me fast Smile

Yes ive just started coding a bootsector and kernel last 3 days (causes sleepless nights when i get hooked into coding kernel) and there are several things that ive knew about operating systems from MBR to multi tasking.
Frankly, i really want to make a game without relying on any program that will use VESA as its video driver, capable of reading mouse, keyboard events and reading sectors. Thats all i want but i cant get it work together. Ive coded first in RM, when ive read about VESA which can display high resolution and directly write to screen buffer, it really made me happy because i dont rely now on directdraw blahblahs until i found out you cant access 4GB addresses on it till unreal mode has enlighten my mind causes me to rewrite a kernel again. And again, the 16-bitness of it made me sad, till ive read about protected mode. Everythings okay till i found out that i cant read sectors while in protected mode which made me suffer from choosing different modes i will use in my kernel.

Can you give an advice which mode fits perfectly on my need?
Direct screen buffer access
4GB addressing space
Keyboard and mouse handling

Thank you very much~ and happy new year man~ I wait for the wonderful reply from you Smile
Post 01 Jan 2008, 11:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 01 Jan 2008, 11:09
If you want to rely on the BIOS to do the I/O for you then you have no choice but to use 16bit mode for that I/O. At other times, either PM or unreal will give you 4GB memory access. The only way to avoid 16bit completely is to use 32bit I/O drivers in your OS.
Post 01 Jan 2008, 11:09
View user's profile Send private message Visit poster's website Reply with quote
blueone



Joined: 01 Apr 2007
Posts: 10
blueone 01 Jan 2008, 11:13
Ok, so does it mean i will do the switching thingys everytime i will handle the events? Ive never heard about 32bit I/O drivers regarding to kernels. Does it mean a interrupt table?
Post 01 Jan 2008, 11:13
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 01 Jan 2008, 11:18
blueone wrote:
Ok, so does it mean i will do the switching thingys everytime i will handle the events?
If you want to use the BIOS services then yes, they only work in 16bit mode.
blueone wrote:
Ive never heard about 32bit I/O drivers regarding to kernels. Does it mean a interrupt table?
Sure, the GDT and IDT are the basis of the PM system. If it is done right you never have to leave PM mode. But you must use all your own 32bit code for everything like HDD, FDD, mouse, keyboard, etc.
Post 01 Jan 2008, 11:18
View user's profile Send private message Visit poster's website Reply with quote
blueone



Joined: 01 Apr 2007
Posts: 10
blueone 01 Jan 2008, 11:26
Ooohh, do you have an idea if mode switching is SLOW or likely crash anything?

Ive read google about IDTs but im confused about what that really is. Is that a table of address to whom processor will call in case of an interrupt? I feel 80% right but i still need help if it is what i really need.

[enable a20]
[i load the idt table]
[enter PM]

mouse_handler:
draw cursor in x,y
iret

keyboard_handler:
handle keyevents
iret

Will cpu provide me the x,y coordinates in mouse handler? or ascii code in keyboard handler? where the values reside? eax, ecx or sumthing like what?

Thanks for replying!!!
Post 01 Jan 2008, 11:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 01 Jan 2008, 12:24
You will need to study each device to learn how to do the I/O for it. They are the instructions IN and OUT. CPU won't provide you with anything except the interface to the device. It is up to you to control the device with your IN/OUT codes.
Post 01 Jan 2008, 12:24
View user's profile Send private message Visit poster's website Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio 01 Jan 2008, 15:36
blueone wrote:

Frankly, i really want to make a game without relying on any program that will use VESA as its video driver

Try to do the game first,and if you find it was very easy then try to write the OS.
Post 01 Jan 2008, 15:36
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 01 Jan 2008, 19:09
try to code the game before to code the os..? :-s
i think the question is not game coding or os coding...

idt, to speak human, is a table.
each element of the idt is an interupt descriptor, a 64bit structure composed by size, base, controls flieds. 8bytes or 4words or 2dwords or 1qword. that isan interrupt descriptor. 64 bit
element 0/offset 0 is int 0 ;64bit
element 1/offset 8 is int 1 ;64bit
...

ivt is the interrupt vector table and is ued under real mode.
real mode vectors are 4 bytes leng. composed by a segment and an offset
element 0/offset 0 is int 0 ;32bit
element 1/offset 4 is int 1 ;32bit
...

to call the function pointed to by the descriptor/vector, simple:
int xxx ; xxx is the interrupt number, (descriptor/vector)..
irqx ; irq is an insynchronous signal on a pin.

pm int and rm int are the same. only the addressing mode inside he code and the descriptor format are differents.

so, a pure hardcontrol, without memory access, int is exactly the same function under PM and RM

exemple:
;out dx,al function
int255: ;doesn't exists anywhere, just an exemple.
;eax=data to out
;edx=i/o port
out dx,al
iret
Post 01 Jan 2008, 19:09
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 01 Jan 2008, 19:22
Heres some pointer, first you have interrupts in both real and pmode, But you can not use BIOS interrupts in pmode, unless you go back to realmode do the interrupt and than go back to pmode or set up a v86.

Now you say you want to use vesa for your games, right vesa need setting up in realmode, so you have 2 choosers
1. Set veas up to the mode you want in realmode on booting your OS, then stay in that mode when in pmode

2. Go back and forth to change modes.

Now once setup you can use vesa from pmode if you have vesa2 (most modern PC do ) because it need to support LFB.
Now you also need to load your game from the floppy or hdd, So again you have 2 choosers
1. load a small game from realmode using BIOS
2. Use unreal-mode to load a big game
3. Go to and from pmode to realmode to use BIOS int 13h function to load program
4. Write a pmode driver, so you can load program from pmode.

Now going to and from pmode to real mode will not slow or crash your game, as you only use it to switch modes, which if you want to switch's mode you only do it once at start of game and once at end of game, it will take less than a second.

I started by making bootable games its a good way to learn.
But as was pointed out, you should start by coding a game to work on a OS and try and code built in functions.

But to get you started have a look at CdPod my entry in the 512b compo, it demo high res vesa and pmode and basic GUI, plus CD player, that fits into the bootsector
of a floppy (less than 512bytes).
http://www.dex4u.com/ASMcompo512b/CdPod.zip
Also see Tomasz Grysztar bootable tetris game, that fits into boot sector (less than 512bytes)
http://www.dex4u.com/ASMcompo512b/TETROS.ZIP

PS: I was called ASHLEY4 when i made CdPod.
Post 01 Jan 2008, 19:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 02 Jan 2008, 07:12
blueone: Just my personal opinion here, I've yet to be convinced that unreal mode has any advantage over PM. I think many people use it because it is a neat trick to play on the CPU. But it seems to give nothing extra that PM doesn't do and also it doesn't seem any easier or simpler to operate than PM. Indeed to enter unreal you have to first go into PM and then jump back to unreal. Just kind of makes we think "why bother"
Post 02 Jan 2008, 07:12
View user's profile Send private message Visit poster's website Reply with quote
Plue



Joined: 15 Dec 2005
Posts: 151
Plue 03 Jan 2008, 22:27
My personal opinion is that you should not start with real mode, but go straight to protected mode. It's not at all hard if you let GRUB switch to protected mode for you.

Quote:
As far i as i know, one can only use interrupt while in real/unreal mode.
That is not true. You can't use the BIOS interrupt when you're in protected more, but that's just one interrupt, not interrupts in general. (And of course, you can't use the DOS/Linux interrupt when you're making your own OS.)

Quote:
Yes ive just started coding a bootsector and kernel last 3 days
Forget about the bootsector, it's a big waste of time if you want to write a kernel. (Of course, if you want to write a bootsector then it's not a waste of time, but it's take a long time before you get to your kernel then.)

Quote:
Frankly, i really want to make a game without relying on any program that will use VESA as its video driver, capable of reading mouse, keyboard events and reading sectors. Thats all i want but i cant get it work together.
That's all you want? I thought you wanted an operating system?

Quote:
Can you give an advice which mode fits perfectly on my need?
Direct screen buffer access
4GB addressing space
Keyboard and mouse handling
1. Setup VESA in 16-bit real mode
2. Switch to protected mode for 32-bit code with 4 gb addressing space

Quote:
Ok, so does it mean i will do the switching thingys everytime i will handle the events?
You don't want to do that. It's totally overkill.

Quote:
Ive read google about IDTs but im confused about what that really is. Is that a table of address to whom processor will call in case of an interrupt?
Yes, but a bit more than the address. It also contains some other things like which segment that address is in (should of course be a code segment).
Code:
; idt_entry is
;     0 base_low.w
;     2 code_segment_selector.w
;     4 always0.b
;     5 flags.b
;     6 base_high.w
; end idt_entry = 8 bytes    


Quote:
Code:
[enable a20] 
[i load the idt table] 
[enter PM]    
Load the IDT table after you entered protected mode.

_________________
Roses are red
Violets are blue
Some poems rhyme
And some don't.
Post 03 Jan 2008, 22:27
View user's profile Send private message Reply with quote
blueone



Joined: 01 Apr 2007
Posts: 10
blueone 06 Jan 2008, 01:01
Ok thanks for the replies. Regarding in the 32 bit hdd mouse and keyboard drivers, can anyone explain me a bit how i can access the sectors without 16bit ints of BIOS? Wats d use of IDT table? is it for drivers? After several articles ive read, i cant still get it out cuz i cant understand english very much. Thanks again. Sad
Post 06 Jan 2008, 01:01
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 06 Jan 2008, 01:19
i don't know exactlly, but, for the moment i seek a method to access via PIO and DMA
Post 06 Jan 2008, 01:19
View user's profile Send private message Visit poster's website Reply with quote
tom tobias



Joined: 09 Sep 2003
Posts: 1320
Location: usa
tom tobias 07 Jan 2008, 11:03
blueone wrote:
Wats d use of IDT table? is it for drivers? After several articles ive read, i cant still get it out cuz i cant understand english very much.

http://www.intel.com/design/processor/manuals/253668.pdf = volume 3A of 5 manuals. Thousands of pages!
Chapters 2 and 5 of volume 3A are the ones you need. In particular, section 2.4, which covers all four of the memory management registers, including, on page 17 of chapter 2, IDTR:
"...holds the base address and 16 bit table limit of the IDT"
IDT itself is described in section 5.10
Hope this helps a little.
Post 07 Jan 2008, 11:03
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.