The kernel manages the system resources, including file systems, processes, and physical devices.
The kernel provides applications with system services such as I/O management, virtual memory,
and scheduling. The kernel coordinates interactions of all user processes and system resources.
The kernel assigns priorities, services resource requests, and services hardware interrupts and exceptions.
The kernel schedules and switches threads, pages memory, and swaps processes.
Differences Between Kernel Modules and User Programs
This section discusses several important differences between kernel modules and user programs.
Execution Differences Between Kernel Modules and User Programs
The following characteristics of kernel modules highlight important differences between the execution of
kernel modules and the execution of user programs:
Kernel modules have separate address space. A module runs in kernel space. An application runs in user space. System software is protected from user programs. Kernel space and user space have their own memory address spaces. See User and Kernel Address Spaces on x86 and SPARC Machines for important information about address spaces.
Kernel modules have higher execution privilege. Code that runs in kernel space has greater privilege than code that runs in user space. Driver modules potentially have a much greater impact on the system than user programs. Test and debug your driver modules carefully and thoroughly to avoid adverse impact on the system. See Device Driver Testing Tips.
Kernel modules do not execute sequentially. A user program typically executes sequentially and performs a single task from beginning to end. A kernel module does not execute sequentially. A kernel module registers itself in order to serve future requests.
Kernel modules can be interrupted. More than one process can request your driver at the same time. An interrupt handler can request your driver at the same time that your driver is serving a system call. In a symmetric multiprocessor (SMP) system, your driver could be executing concurrently on more than one CPU.
Kernel modules must be preemptable. You cannot assume that your driver code is safe just because your driver code does not block. Design your driver assuming your driver might be preempted.
Kernel modules can share data. Different threads of an application program usually do not share data. By contrast, the data structures and routines that constitute a driver are shared by all threads that use the driver. Your driver must be able to handle contention issues that result from multiple requests. Design your driver data structures carefully to keep multiple threads of execution separate. Driver code must access shared data without corrupting the data. See Chapter 3, Multithreading, in Writing Device Drivers and Multithreaded Programming Guide.
Structural Differences Between Kernel Modules and User Programs
The following characteristics of kernel modules highlight important differences between the structure of
kernel modules and the structure of user programs:
Kernel modules do not define a main program. Kernel modules, including device drivers, have no main() routine. Instead, a kernel module is a collection of subroutines and data. A device driver is a kernel module that forms a software interface to an input/output (I/O) device. The subroutines in a device driver provide entry points to the device. The kernel uses a device number attribute to locate the open() routine and other routines of the correct device driver. See Device Drivers for more information on entry points. See Device Numbers for a description of device numbers.
Kernel modules are linked only to the kernel. Kernel modules do not link in the same libraries that user programs link in. The only functions a kernel module can call are functions that are exported by the kernel. If your driver references symbols that are not defined in the kernel, your driver will compile but will fail to load. Solaris OS driver modules should use prescribed DDI/DKI (Device Driver Interface, Driver-Kernel Interface) interfaces. When you use these standard interfaces you can upgrade to a new Solaris release or migrate to a new platform without recompiling your driver. For more information on the DDI, seeDDI/DKI Interfaces in Writing Device Drivers. Kernel modules can depend on other kernel modules by using the -N option during link editing. See the ld(1) man page for more information.
Kernel modules use different header files. Kernel modules require a different set of header files than user programs require. The required header files are listed in the man page for each function. See man pages section 9: DDI and DKI Kernel Functions for DDI/DKI functions, man pages section 9: DDI and DKI Driver Entry Points for entry points, and man pages section 9: DDI and DKI Properties and Data Structures for structures. Kernel modules can include header files that are shared by user programs if the user and kernel interfaces within such shared header files are defined conditionally using the _KERNEL macro.
Kernel modules should avoid global variables. Avoiding global variables in kernel modules is even more important than avoiding global variables in user programs. As much as possible, declare symbols as static. When you must use global symbols, give them a prefix that is unique within the kernel. Using this prefix for private symbols within the module also is a good practice.
Kernel modules can be customized for hardware. Kernel modules can dedicate process registers to specific roles. Kernel code can be optimized for a specific processor.
Kernel modules can be dynamically loaded. The collection of subroutines and data that constitute a device driver can be compiled into a single loadable module of object code. This loadable module can then be statically or dynamically linked into the kernel and unlinked from the kernel. You can add functionality to the kernel while the system is up and running. You can test new versions of your driver without rebooting your system.
Data Transfer Differences Between Kernel Modules and User Programs
Data transfer between a device and the system typically is slower than data
transfer within the CPU. Therefore, a driver typically suspends execution of the calling
thread until the data transfer is complete. While the thread that called the
driver is suspended, the CPU is free to execute other threads. When the
data transfer is complete, the device sends an interrupt. The driver handles the
interrupt that the driver receives from the device. The driver then tells the
CPU to resume execution of the calling thread. See Chapter 8, Interrupt Handlers, in Writing Device Drivers.
Drivers must work with user process (virtual) addresses, system (kernel) addresses, and I/O
bus addresses. Drivers sometimes copy data from one address space to another address
space and sometimes just manipulate address-mapping tables. See Bus Architectures in Writing Device Drivers.
User and Kernel Address Spaces on x86 and SPARC Machines
On SPARC machines, the system panics when a kernel module attempts to directly
access user address space. You must make sure your driver does not attempt
to directly access user address space on a SPARC machine.
On x86 machines, the system does not enter an error state when
a kernel module attempts to directly access user address space. You still should
make sure your driver does not attempt to directly access user address space
on an x86 machine. Drivers should be written to be as portable as
possible. Any driver that directly accesses user address space is a poorly written
Caution - A driver that works on an x86 machine might not work on
a SPARC machine because the driver might access an invalid address.
Do not access user data directly. A driver that directly accesses user address
space is using poor programming practice. Such a driver is not portable and
is not supportable. Use the ddi_copyin(9F) and ddi_copyout(9F) routines to transfer data
to and from user address space. These two routines are the only supported
interfaces for accessing user memory. Modifying Data Stored in Kernel Memory shows an example driver that uses ddi_copyin(9F) and
The mmap(2) system call maps pages of memory between a process's address space
and a file or shared memory object. In response to an mmap(2) system
call, the system calls the devmap(9E) entry point to map device memory into
user space. This information is then available for direct access by user applications.
A device driver is a loadable kernel module that manages data transfers between
a device and the OS. Loadable modules are loaded at boot time
or by request and are unloaded by request. A device driver is a
collection of C routines and data structures that can be accessed by other
kernel modules. These routines must use standard interfaces called entry points. Through the use of
entry points, the calling modules are shielded from the internal details of the
driver. See Device Driver Entry Points in Writing Device Drivers for more information on entry points.
A device driver declares its general entry points in its dev_ops(9S) structure. A
driver declares entry points for routines that are related to character or block
data in its cb_ops(9S) structure. Some entry points and structures that are common
to most drivers are shown in the following diagram.
Figure 1-1 Typical Device Driver Entry Points
The Solaris OS provides many driver entry points. Different types of devices require
different entry points in the driver. The following diagram shows some of the
available entry points, grouped by driver type. No single device driver would use
all the entry points shown in the diagram.
Figure 1-2 Entry Points for Different Types of Drivers
In the Solaris OS, drivers can manage physical devices, such as disk drives,
or software (pseudo) devices, such as bus nexus devices or ramdisk devices. In
the case of hardware devices, the device driver communicates with the hardware controller
that manages the device. The device driver shields the user application layer from
the details of a specific device so that application level or system calls
can be generic or device independent.
Drivers are accessed in the following situations:
System initialization. The kernel calls device drivers during system initialization to determine which devices are available and to initialize those devices.
System calls from user processes. The kernel calls a device driver to perform I/O operations on the device such as open(2), read(2), and ioctl(2).
User-level requests. The kernel calls device drivers to service requests from commands such as prtconf(1M).
Device interrupts. The kernel calls a device driver to handle interrupts generated by a device.
Bus reset. The kernel calls a device driver to re-initialize the driver, the device, or both when the bus is reset. The bus is the path from the CPU to the device.
The following diagram illustrates how a device driver interacts with the rest of
Figure 1-3 Typical Device Driver Interactions
Driver Directory Organization
Device drivers and other kernel modules are organized into the following directories in
the Solaris OS. See the kernel(1M) and system(4) man pages for more information
about kernel organization and how to add directories to your kernel module search
These modules are common across most platforms. Modules that are required for booting or for system initialization belong in this directory.
- /platform/`uname -i`/kernel
These modules are specific to the platform identified by the command uname -i.
- /platform/`uname -m`/kernel
These modules are specific to the platform identified by the command uname -m. These modules are specific to a hardware class but more generic than modules in the uname -i kernel directory.
These are user modules. Modules that are not essential to booting belong in this directory. This tutorial instructs you to put all your drivers in the /usr/kernel directory.
One benefit of organizing drivers into different directories is that you can selectively
load different groups of drivers on startup when you boot interactively at the
boot prompt as shown in the following example. See the boot(1M) man page for
Type b [file-name] [boot-flags] <ENTER> to boot with options
or i <ENTER> to enter boot interpreter
or <ENTER> to boot with defaults
<<< timeout in 5 seconds >>>
Select (b)oot or (i)nterpreter: b -a
bootpath: /[email protected],0/pci8086,[email protected]/pci8086,
Enter default directory for modules [/platform/i86pc/kernel /kernel
/usr/kernel]: /platform/i86pc/kernel /kernel
In this example, the /usr/kernel location is omitted from the list of directories
to search for modules to load. You might want to do this if
you have a driver in /usr/kernel that causes the kernel to panic during
startup or on attach. Instead of omitting all /usr/kernel modules, a better method
for testing drivers is to put them in their own directory. Use the
moddir kernel variable to add this test directory to your kernel modules search
path. The moddir kernel variable is described in kernel(1M) and system(4). Another method
for working with drivers that might have startup problems is described in Device Driver Testing Tips.