Writing Device Drivers for Oracle® Solaris 11.2

Exit Print View

Updated: September 2014
 
 

Compiling and Linking the Driver

You need to compile each driver source file and link the resulting object files into a driver module. The Oracle Solaris OS is compatible with both the Oracle Solaris Studio C compiler and the GNU C compiler from the Free Software Foundation, Inc. The examples in this section use the Oracle Solaris Studio C compiler unless otherwise noted. For information on the Oracle Solaris Studio C compiler, see the Oracle Solaris Studio 12.3: C User???s Guide and the Oracle Solaris Studio Documentation. For more information on compile and link options, see the Oracle Solaris Studio 12.3 Command-Line Reference documentation. The GNU C compiler is supplied in the /usr/sfw directory. For information on the GNU C compiler, see http://gcc.gnu.org/ or check the man pages in /usr/sfw/man.

The example below shows a driver that is called xx with two C source files. A driver module that is called xx is generated. The driver that is created in this example is for a 32-bit kernel. You must use ld –r even if your driver has only one object module.

% cc -D_KERNEL -c xx1.c
% cc -D_KERNEL -c xx2.c
% ld -r -o xx xx1.o xx2.o

The _KERNEL symbol must be defined to indicate that this code defines a kernel module. No other symbols should be defined, except for driver private symbols. The DEBUG symbol can be defined to enable any calls to ASSERT(9F).

Table 22-1  Compiler Options for SPARC and x86 64–bit Architectures
Compiler
SPARC 64
x86 64
Studio 9
cc -D_KERNEL -xarch=v9 -c xx.c
Not Supported
Studio 10
cc -D_KERNEL -xarch=v9 -c xx.c
cc -D_KERNEL -xarch=amd64 -xmodel=kernel -c xx.c
Studio 11
cc -D_KERNEL -xarch=v9 -c xx.c
cc -D_KERNEL -xarch=amd64 -xmodel=kernel -c xx.c
Oracle Solaris Studio 12
cc -D_KERNEL -m64 -c xx.c
cc -D_KERNEL -m64 -xmodel=kernel -c xx.c

The –xmodel option enables the compiler to modify the form of 64-bit objects for Oracle Solaris x86 platforms and should only be specified for the compilation of such objects. For more information see the description of the –xmodeloption in the Oracle Solaris Studio 12.3: C User's Guide.


Caution

Caution  -  If you are compiling for a 32-bit or 64-bit x86 architecture using Oracle Solaris Studio compilers, you need to make sure your compilation does not produce MMX or SSE instructions. MMX and SSE instructions are not supported in the x86 kernel. Use of MMX or SSE instructions triggers a kernel panic and therefore should not be used.


The following table lists the considerations and compiler options to generate code without MMX or SSE instructions for the x86 architecture.

Table 22-2  Considerations and Compiler Options to Generate Code Without MMX or SSE Instructions for the x86 Architecture
Oracle Solaris Studio Compiler Version
Compiling for 64-bit
Compiling for 32-bit
12.3 or older
  • Do not use floating point types in the source code.

  • Switch off microvectorization by using the -xvector=no compiler option.

By default, compilers produce code without MMX or SSE instructions. Therefore, do not add the –xarch option for MMX and SSE2 instructions such as sse2, sse2a, and so on. For the list of all the –xarch values see Oracle Solaris Studio 12.3: C User's Guide.
12.4 or newer
To avoid the generation of SSE2 instructions use the -xregs=no%float compiler option.
By default, compilers use the -xarch=sse2 option and add the SSE2 instructions. Therefore, use the -xregs=no%float compiler option.

After the driver is stable, you might want to add optimization flags to build a production quality driver. See the cc(1) man page in Oracle Solaris Studio 12.3 Command-Line Reference for specific information on optimizations in the Oracle Solaris Studio C compiler.

Global variables should be treated as volatile in device drivers. The volatile tag is discussed in greater detail in Declaring a Variable Volatile. Use of the flag depends on the platform. See the man pages.