Hello World =============================== .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Version Info ` * :ref:`Learnings in this section ` * :ref:`Setup development environment ` * :ref:`Complete program : hello.c ` * :ref:`Makefile ` * :ref:`Compile Program ` * :ref:`Load the module ` * :ref:`Check if the module is loaded successfully ` * :ref:`Check what happened on module load ` * :ref:`Unload the module ` * :ref:`Check if the module is unloaded successfully ` * :ref:`Check what happened on module unload ` .. _FreebsdDeviceDriver_basic_fdd_hello_world_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Freebsd 14.1.0 =============================== ======================================= .. _FreebsdDeviceDriver_basic_fdd_hello_world_1: .. tab-set:: .. tab-item:: Learnings in this section * In this program, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to write a sample kernel module ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to write Makefile to compile driver module ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to check if module is loaded successfully ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to check if module is unloaded successfully ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to check prints from kernel modules ? .. _FreebsdDeviceDriver_basic_fdd_hello_world_2: .. tab-set:: .. tab-item:: Setup development environment * Create directory using mkdir command .. code-block:: c test:~$ pwd /home/test/ test:~$ mkdir basic_device_drivers test:~$ cd basic_device_drivers test:~$ mkdir hello_world test:~$ cd hello_world test:~$ pwd /home/test/basic_device_drivers/hello_world .. _FreebsdDeviceDriver_basic_fdd_hello_world_3: .. tab-set:: .. tab-item:: hello.c .. literalinclude:: hello_world/hello.c :language: c :linenos: .. _FreebsdDeviceDriver_basic_fdd_hello_world_4: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: hello_world/Makefile :language: c :linenos: .. _FreebsdDeviceDriver_basic_fdd_hello_world_5: .. tab-set:: .. tab-item:: Compile Program .. code-block:: c :linenos: :emphasize-lines: 1, 5, 9, 11 $ pwd basic_device_drivers/hello_world $ ls hello.c Makefile $ make $ ls .depend.hello.o export_syms hello.c hello.o hello_world.ko i386 machine Makefile opt_global.h x86 .. _FreebsdDeviceDriver_basic_fdd_hello_world_6: .. tab-set:: .. tab-item:: Load the module * Load the module using kldload .. code-block:: c :linenos: :emphasize-lines: 1 $ kldload ./hello_world.ko Loaded ./kthread_module.ko, id=8 .. _FreebsdDeviceDriver_basic_fdd_hello_world_7: .. tab-set:: .. tab-item:: Check if the module is loaded successfully * Run kldstat command to check the loaded modules .. code-block:: c :linenos: :emphasize-lines: 1, 10 $ kldstat Id Refs Address Size Name 1 29 0xffffffff80200000 1f6c698 kernel 2 1 0xffffffff8216d000 7850 cryptodev.ko 3 1 0xffffffff82176000 772c70 zfs.ko 4 1 0xffffffff83218000 3220 intpm.ko 5 1 0xffffffff8321c000 2178 smbus.ko 6 1 0xffffffff8321f000 3360 uhid.ko 7 1 0xffffffff83223000 3360 wmt.ko 8 1 0xffffffff83227000 209c hello_world.ko .. _FreebsdDeviceDriver_basic_fdd_hello_world_8: .. tab-set:: .. tab-item:: Check what happened on module load * Run dmesg command to check the kernel logs .. code-block:: c :linenos: :emphasize-lines: 1 $ dmesg Hello KLD loaded. .. _FreebsdDeviceDriver_basic_fdd_hello_world_9: .. tab-set:: .. tab-item:: Unload the module * Unload the module using kldunload .. code-block:: c :linenos: :emphasize-lines: 1 $ kldunload -v ./hello_world.ko Unloading hello_world.ko, id=8 .. _FreebsdDeviceDriver_basic_fdd_hello_world_10: .. tab-set:: .. tab-item:: Check if the module is unloaded successfully * Run kldstat command to check the loaded modules .. code-block:: c :linenos: :emphasize-lines: 1 $ kldstat Id Refs Address Size Name 1 27 0xffffffff80200000 1f6c698 kernel 2 1 0xffffffff8216d000 7850 cryptodev.ko 3 1 0xffffffff82176000 772c70 zfs.ko 4 1 0xffffffff83218000 3220 intpm.ko 5 1 0xffffffff8321c000 2178 smbus.ko 6 1 0xffffffff8321f000 3360 uhid.ko 7 1 0xffffffff83223000 3360 wmt.ko .. _FreebsdDeviceDriver_basic_fdd_hello_world_11: .. tab-set:: .. tab-item:: Check what happened on module unload * Run dmesg command to check the kernel logs .. code-block:: c :linenos: :emphasize-lines: 1 $ dmesg Hello KLD unloaded