1.3 Guidelines for Designing and Writing a Driver

When designing and writing your device driver, follow these guidelines:

Make every effort to make your device driver as small as possible; reserve system memory for applications.

Use multiple, discardable code segments to help reduce the amount of driver code needed in memory at any given time.

Use an automatic data segment only if necessary.

Make resources discardable, and lock them in memory only when needed.

Use the stack sparingly. Because device drivers use the stack of the application that initiated the call to the driver, there is no way for the driver to determine how much available space is on the stack.

Check for NULL pointers to avoid a general protection faults from using an invalid selector.

Check the segment limits when reading from or writing to allocated segments to avoid a general protection fault from attempting to access data beyond the end of a segment.

Use the __ahincr constant when creating selectors for huge memory (allocated memory greater than 64 kilobytes). Other methods of selector arithmetic can create invalid selectors and cause general protection faults.

Create code-segment aliases for any code to be executed from data segments. Attempting to call or jump to a data segment address generates a general protection fault.