Page 1 of 1

Drivers!

Posted: Tue Jul 13, 2010 10:49 am
by TomMan
Hi, I am currently developing an OS but I am wondering what is the best way of implementing driver support into my kernel?

Re: Drivers!

Posted: Tue Jul 13, 2010 11:53 am
by Gigasoft
Well, everyone probably has a different answer to this, but the way it's roughly going to work in my system is:
- The boot loader remains in memory while the system is booting, and the system can invoke it to load additional files.
- The system loads the machine configuration file, which has information about devices that are installed on the system. It has also enough information to determine which drivers are necessary to continue booting (for example, drivers for the PCI bus, the IDE controller and the FAT file system).
- The system loads these drivers, but does not start the devices until it has loaded all of them.
- The system then takes control away from the BIOS and starts each needed device.
- The rest of the drivers are loaded when needed.

For each installed driver, the machine configuration file links each type of device (such as a PCI function with a specific vendor and device ID) with the driver that will handle it. It also contains nodes for specific device instances, so that it is possible to use another driver for one instance while using the default driver for the others. Each driver exposes an entry point which is called when the driver is loaded, and another entry point that is called to have it handle a specific device. The driver communicates with the parent bus through the passed device pointer. The driver then registers one or more interfaces on the device. Other components use these interfaces to communicate with the driver. User applications can also open these interfaces and access them using appropriate APIs.

Re: Drivers!

Posted: Tue Jul 13, 2010 1:40 pm
by piranha
The drivers need a way of communicating with hardware, the kernel and user programs. Those are pretty much the basic requirements. There are several ways to communicate with hardware:
1. Do it directly. This requires drivers to be run in ring 0, or 1 or 2 or whatever.
2. Through the kernel. The drivers ask the kernel to do specific communications. Unfortunately, while this is safer, this is slower.

Communications with user programs: The driver could create files that are accessible to the user programs (much like /dev/ in *nix), and have call-backs registered with the kernel. There are, of course, many other ways for this to work, but thats just one idea.

Essentially, the main question is how do I get runnable code in a file to interact with the kernel in a way to make hardware usable? My personal favorite method is kernel modules. They are fairly simple to implement, and they also allow for the kernel to load and link them into a symbol table so that they can interact with the kernel. They can then run code required for interacting with hardware and user programs. This is the system that I use.

The disadvantage to modules is that they are less safe. They can easily cripple the entire system. So be careful...

User programs can also work, as long as the kernel exposes enough calls to allow for a program to manage hardware and act as a driver. However, I think that this is more complex than need be. Any method is better than just building them right into the kernel though, since that causes bloat and many other problems.

With modules though, you need a driver inside the kernel to be able to read them off of a storage device. But that driver has to be loaded as well. This is usually accomplished with a ramdisk loaded by the bootloader and it's address passed to the kernel. The kernel then contains a basic driver to read from the ramdisk - enough to load a hard drive/floppy drive driver and a FS driver so that the system can load more drivers as need be.

-JL

Re: Drivers!

Posted: Wed Jul 14, 2010 1:42 pm
by bewing
Drivers also probably have to have a generic method of being able to communicate with other drivers.
This is one of the basic ways of forming driver stacks -- such as the TCP/IP stack or the USB stack.

Unfortunately, the question "what is the best way to implement drivers" is one of the biggest unknown questions in OS development. There may be such a way, but everyone here is guessing just as much as you are. You just have to wing it.