Device Driver Tutorial

Exit Print View

Updated: July 2014
 
 

Building a Driver

This section tells you how to compile and link a driver for different architectures.

Make sure you have installed the Oracle Solaris OS at the Developer level or above.

A 64-bit kernel cannot use a 32-bit driver. A 64-bit kernel can use only 64-bit drivers. All parts of any particular program must use the same data model. A device driver is not a complete program. The kernel is a complete program. A driver is a part of the kernel program. If you want your device to work with the Oracle Solaris OS in 32-bit mode and in 64-bit mode, then you must provide both a 32-bit driver and a 64-bit driver.

By default, compilation on the operating system yields a 32-bit result on every architecture. To obtain a 64-bit result, use the compilation options specified in this section for 64-bit architectures.

Use the prtconf(1M) command with the –x option to determine whether the firmware on this system is 64-bit ready.

Compiling with Oracle Solaris Studio

    Use the –D_KERNEL option to indicate that this code defines a kernel module.

  • If you are compiling for a 64-bit SPARC architecture using Sun Studio 9, Sun Studio 10, or Sun Studio 11, use the –xarch=v9 option:

    % cc -D_KERNEL -xarch=v9 -c mydriver.c
    % ld -r -o mydriver mydriver.o

    If you are compiling for a 64-bit SPARC architecture using Oracle Solaris Studio 12, use the –m64 option:

    % cc -D_KERNEL -m64 -c mydriver.c
    % ld -r -o mydriver mydriver.o
  • If you are compiling for a 64-bit x86 architecture using Sun Studio 10 or Sun Studio 11, use both the –xarch=amd64 option and the –xmodel=kernel option:

    % cc -D_KERNEL -xarch=amd64 -xmodel=kernel -c mydriver.c
    % ld -r -o mydriver mydriver.o

    If you are compiling for a 64-bit x86 architecture using Oracle Solaris Studio 12, use the –m64 option, the –xarch=sse2a option, and the –xmodel=kernel option:

    % cc -D_KERNEL -m64 -xarch=sse2a -xmodel=kernel -c mydriver.c
    % ld -r -o mydriver mydriver.o
  • If you are compiling for a 32-bit architecture, use the following build commands:

    % cc -D_KERNEL -c mydriver.c
    % ld -r -o mydriver mydriver.o

Note - Sun Studio 9 does not support 64-bit x86 architectures. Use Sun Studio 10, Sun Studio 11, or Oracle Solaris Studio 12 to compile and debug drivers for 64-bit x86 architectures.

For more information on compile and link options, see the Oracle Solaris Studio 12.3 Command-line Reference and the Oracle Solaris Studio 12.2: C User's Guide . To learn more about Oracle Solaris Studio, go to http://www.oracle.com/technetwork/server-storage/solarisstudio/overview/index.html.

Compiling with the GNU C Compiler

    Use the –D_KERNEL option to indicate that this code defines a kernel module. These examples show options that are required for correct functionality of the result.

  • If you are compiling for a 64-bit SPARC architecture, use the following build commands:

    % gcc -D_KERNEL -m64 -mcpu=v9 -mcmodel=medlow -fno-pic -mno-fpu
    -ffreestanding -nodefaultlibs -c mydriver.c
    % ld -r -o mydriver mydriver.o

    You might also want to use the –mtune=ultrasparc option and the –O2 option.

  • If you are compiling for a 64-bit x86 architecture, use the following build commands:

    % gcc -D_KERNEL -m64 -mcmodel=kernel -mno-red-zone -ffreestanding
    -nodefaultlibs -c mydriver.c
    % ld -r -o mydriver mydriver.o

    You might also want to use the –mtune=opteron option and the –O2 option.

  • If you are compiling for a 32-bit architecture, use the following build commands:

    % gcc -D_KERNEL -ffreestanding -nodefaultlibs -c mydriver.c
    % ld -r -o mydriver mydriver.o

For more information on these and other options, see the gcc(1) man page. See also the GCC web site at http://gcc.gnu.org/.