Writting drivers

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
User avatar
crackers
Member
Member
Posts: 27
Joined: Wed Nov 15, 2006 6:31 am

Writting drivers

Post by crackers »

I want to add to my OS drivers which would be in seperate files, not as a part of kernel code. So I've created one driver for test and started to wonder what parameters should I give to linker since driver can be loaded into any place in memory. Code of functions it's not a problem since all calls, jmp's within driver are relative (not absolute). Problem is how can I post data from one function to another. For example let's say that we have driver that is responsible for reading data and there are 2 functions. One is a main loop that is run as a seperate task and second one is function that is being called by system to read or write data. How this second function can pass informations to first one?
1.One solution would be to link all drivers with the same address and in kernel set pages for each task related with each driver. With this it's possible to use global variables in drivers. But this solution is a little bit complex from kernel point of view since there is a need for creating page tables and what's more important before executing any driver function I would have to switch to task that would see driver at good location in memory.
2.Second solution would be to pass address of some common buffer to each function of driver. I think it's better than first solution but it's uncomfortable to pass these informations for all calls.
3.Third solution I thought about is most interesting in my opinion. I'm adding bogus function to each driver, than I'm calculating it's position (which is rather fast operation) and at the end I'm using it as a global variable (by overwritting or reading it). I've already implemented it and it's working fine, but I wanted to get others opinion. Maybe I'm not seeing something that will cause problems in the future.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Have you considered using relocation information? that way you can set the addresses at load time to whatever you want among any number of drivers without the need for elaborate hacks.

Besides, you should be aware that normal x86 code is not position-independent. References to data/bss space are absolute rather than relative and hence if the binary is loaded to a place it is not expected it will access the wrong memory areas.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
crackers
Member
Member
Posts: 27
Joined: Wed Nov 15, 2006 6:31 am

Post by crackers »

Combuster wrote:Have you considered using relocation information? that way you can set the addresses at load time to whatever you want among any number of drivers without the need for elaborate hacks.
Relocation informations? Could you tell me what is that ?
Combuster wrote: Besides, you should be aware that normal x86 code is not position-independent. References to data/bss space are absolute rather than relative and hence if the binary is loaded to a place it is not expected it will access the wrong memory areas.
Yes I'm aware of it, but for gcc I'm getting good results - I've checked generated code and run through it with bochs debuger, but I will have to look if there is some magical switch in gcc that would force generation of relative addresses for instructions.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

crackers wrote:Relocation informations? Could you tell me what is that ?
http://en.wikipedia.org/wiki/Relocation
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
crackers
Member
Member
Posts: 27
Joined: Wed Nov 15, 2006 6:31 am

Post by crackers »

So, if I understood corectly, there is additional table in such files that is being used as a reference and it's filled by kernel after loading executable file but before using it. If that's how it works than I'm wonedring how to determine where this table starts and where code is starting (I guess only one of these values can be at fixed position). Beside of that it's only moving problem to other place (instead of doing changes in driver I have to do it in kernel by filling this reference table).
TomTom
Posts: 23
Joined: Tue May 01, 2007 2:03 am
Location: USA

Post by TomTom »

That's correct. You're basically reading a table with offsets where your executable image loader needs to patch pointers. Once that is done you can execute the code.
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

crackers wrote:Beside of that it's only moving problem to other place (instead of doing changes in driver I have to do it in kernel by filling this reference table).
If your OS is going to load and run applications it is going to need a loader which can fix up executable files anyway.
User avatar
crackers
Member
Member
Posts: 27
Joined: Wed Nov 15, 2006 6:31 am

Post by crackers »

mathematician wrote:
crackers wrote:Beside of that it's only moving problem to other place (instead of doing changes in driver I have to do it in kernel by filling this reference table).
If your OS is going to load and run applications it is going to need a loader which can fix up executable files anyway.
Hmmmm why? Of course loader is needed but I can't see why it have to do any changes in loaded programs?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Well, it's going to at least need to relocate and zero sections of the executable as required...
User avatar
crackers
Member
Member
Posts: 27
Joined: Wed Nov 15, 2006 6:31 am

Post by crackers »

AJ wrote:Well, it's going to at least need to relocate and zero sections of the executable as required...
Zero sections ? Hmmm I guess it's another new word for me :D In my previous OS I was executing programs (only COM's) with only good paging mechanism so I still don't understand why I must interfere within loaded code.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

If you are loading e.g. a COFF executable, you will have at least 3 sections: .data, .text and .bss.

* The .bss contains uninitialised data and must be zeroed - it does not actually exist in the file but you will have a table with data about where the section should start and how long it is.

* .text contains your code - supposing you load your executable file at a location somewhere in the kernel's page space, you will need to copy x number of bytes to location y from the executable file in RAM to wherever the file is going to execute from (you can then jump to the entry point at the appropriate time).

* .data should be treated as .text - it needs relocating.

There may be other sections e.g. .ctors and .dtors which need locating as appropriate.

Because the BSS is not present in the file and .text and .data are not necessarily aligned, you will find that a COFF (or other format) executable will be a lot smaller than its' flat equivalent. You also don't have to assume nearly as much as with a flat binary (for one thing, the entry point can be anywhere in the file).

HTH
Adam
User avatar
crackers
Member
Member
Posts: 27
Joined: Wed Nov 15, 2006 6:31 am

Post by crackers »

AJ wrote: * .data should be treated as .text - it needs relocating.
I think that in normal tasks it's a better idea to just use paging - that way you don't have to do any changes in loaded code. Beside, is it always easy to change such informations in file regarding of compiler and linker that were used ?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

No - you link at a certain address and the relocation information information in the COFF file is based on that base address. You don't have to change information so much as obeying the built in rules.

Sorry - I seem to have sidetracked you a bit as you were originally asking about dynamic linking which I have no idea about - maybe someone else is better placed to get this back on topic again...

Cheers,
Adam
Post Reply