Advertisement
Guest User

kfpc

a guest
Jan 16th, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. --- system.pas
  2.  
  3. (* Free Pascal Demo Kernel Module
  4. * System Unit Code
  5. * Based on work by Mazen Neifer, originally posted on
  6. * http://wiki.freepascal.org/linux/kernel/module_development
  7. * Stripped down to bare minimum needed for usable Hello World!
  8. * Some definitions also rearranged.
  9. * (c) Lukasz Sokol, 2011 License : GPL
  10. *)
  11. unit system;
  12. {$TYPEINFO OFF}
  13. interface
  14. {Paѕcal common type aliases}
  15. type
  16. {Basic embedded types}
  17. u8 = Byte;
  18. u16 = Word;
  19. u32 = LongWord;
  20. u64 = QWord;
  21. s8 = ShortInt;
  22. s16 = SmallInt;
  23. s32 = LongInt;
  24. s64 = Int64;
  25. {Integer types}
  26. DWord = LongWord;
  27. Cardinal = LongWord;
  28. Integer = SmallInt;
  29. UInt64 = QWord;
  30. {$ifdef CPU64}
  31. SizeInt = Int64;
  32. SizeUInt = QWord;
  33. PtrInt = Int64;
  34. PtrUInt = QWord;
  35. ValSInt = int64;
  36. ValUInt = qword;
  37. {$endif CPU64}
  38. {$ifdef CPU32}
  39. SizeInt = Longint;
  40. SizeUInt = DWord;
  41. PtrInt = Longint;
  42. PtrUInt = DWord;
  43. ValSInt = Longint;
  44. ValUInt = Cardinal;
  45. {$endif CPU32}
  46. {Zero - terminated strings }
  47. PChar = ^Char;
  48. PPChar = ^PChar;
  49. {Pointers}
  50. PSmallInt = ^Smallint;
  51. PShortInt = ^Shortint;
  52. PInteger = ^Integer;
  53. PByte = ^Byte;
  54. PWord = ^word;
  55. PDWord = ^DWord;
  56. PLongWord = ^LongWord;
  57. PLongint = ^Longint;
  58. PCardinal = ^Cardinal;
  59. PQWord = ^QWord;
  60. PInt64 = ^Int64;
  61. PPtrInt = ^PtrInt;
  62. PPtrUInt = ^PtrUInt;
  63. PSizeInt = ^SizeInt;
  64. PPointer = ^Pointer;
  65. PPPointer = ^PPointer;
  66. PBoolean = ^Boolean;
  67. PWordBool = ^WordBool;
  68. PLongBool = ^LongBool;
  69. {Other types}
  70. HRESULT = type Longint;
  71. TDateTime = type Double;
  72. TError = type Longint;
  73. const
  74. EPERM = 1;{Operation not permitted}
  75. ENOENT = 2;{No such file or directory}
  76. ESRCH = 3;{No such process}
  77. EINTR = 4;{Interrupted system call}
  78. EIO = 5;{I/O error}
  79. ENXIO = 6;{No such device or address}
  80. E2BIG = 7;{Argument list too long}
  81. ENOEXEC = 8;{Exec format error}
  82. EBADF = 9;{Bad file number}
  83. ECHILD = 10;{No child processes}
  84. EAGAIN = 11;{Try again}
  85. ENOMEM = 12;{Out of memory}
  86. EACCES = 13;{Permission denied}
  87. EFAULT = 14;{Bad address}
  88. ENOTBLK = 15;{Block device required}
  89. EBUSY = 16;{Device or resource busy}
  90. EEXIST = 17;{File exists}
  91. EXDEV = 18;{Cross-device link}
  92. ENODEV = 19;{No such device}
  93. ENOTDIR = 20;{Not a directory}
  94. EISDIR = 21;{Is a directory}
  95. EINVAL = 22;{Invalid argument}
  96. ENFILE = 23;{File table overflow}
  97. EMFILE = 24;{Too many open files}
  98. ENOTTY = 25;{Not a typewriter}
  99. ETXTBSY = 26;{Text file busy}
  100. EFBIG = 27;{File too large}
  101. ENOSPC = 28;{No space left on device}
  102. ESPIPE = 29;{Illegal seek}
  103. EROFS = 30;{Read-only file system}
  104. EMLINK = 31;{Too many links}
  105. EPIPE = 32;{Broken pipe}
  106. EDOM = 33;{Math argument out of domain of func}
  107. ERANGE = 34;{Math result not representable}
  108. type
  109. {Kernel types}
  110. mode_t = Word;
  111. nlink_t = DWord;
  112. uid_t = Word;
  113. gid_t = Word;
  114. off_t = LongInt;
  115. loff_t = Int64;
  116. Ploff_t = ^loff_t;
  117. size_t = DWord;
  118. ssize_t = LongInt;
  119.  
  120. implementation
  121.  
  122. end.
  123.  
  124. ---
  125.  
  126. --- kernel_module.pas
  127.  
  128. (* Free Pascal Demo Kernel Module
  129. * Pascal Module Code
  130. * Based on work by Mazen Neifer, originally posted on
  131. * http://wiki.freepascal.org/linux/kernel/module_development
  132. * Stripped down to bare minimum needed for usable Hello World!
  133. * Some definitions also rearranged.
  134. * (c) Lukasz Sokol, 2011 License : GPL
  135. *)
  136.  
  137. unit kernel_module;
  138. {$TYPEINFO OFF}
  139. interface
  140.  
  141. procedure printk(fmt:PChar); cdecl;
  142.  
  143.  
  144. implementation
  145. const
  146. KERN_INFO='KERNEL:INFO:';
  147. KERN_ALERT='KERNEL:ALERT:';
  148.  
  149. {$LINK kernel_module_info}
  150.  
  151. procedure printk(fmt:PChar); cdecl; external;
  152.  
  153.  
  154.  
  155.  
  156. {This function is called when the module is loaded}
  157. function init_module: Integer; cdecl; export;
  158. begin
  159. printk(KERN_INFO + 'Hello World!'+#10+#0);
  160. init_module := 0;
  161. end;
  162.  
  163. {This function is called when the module is unloaded}
  164. procedure cleanup_module; cdecl; export;
  165. begin
  166. printk(KERN_INFO + 'Bye-bye!'+#10+#0);
  167. end;
  168.  
  169. end.
  170.  
  171.  
  172. ---
  173.  
  174. --- kernel_module_info.c
  175. /* Free Pascal Demo Kernel Module
  176. * c code that is linked into Pascal after compile - needed for symbols
  177. * Based on work by Mazen Neifer, originally posted on
  178. * http://wiki.freepascal.org/linux/kernel/module_development
  179. * Stripped down to bare minimum needed for usable Hello World!
  180. * Some definitions also rearranged.
  181. * (c) Lukasz Sokol, 2011 License : GPL
  182. */
  183.  
  184. #include <linux/module.h>
  185.  
  186. MODULE_DESCRIPTION("Test Module written using FPC");
  187. MODULE_AUTHOR("Lukasz Sokol (fork)");
  188. MODULE_LICENSE("GPL");
  189.  
  190. ---
  191.  
  192. --- Makefile
  193.  
  194. # Free Pascal Demo Kernel Module
  195. # * the Makefile
  196. # * Based on work by Mazen Neifer, originally posted on
  197. # * http://wiki.freepascal.org/linux/kernel/module_development
  198. # * Stripped down to bare minimum needed for usable Hello World!
  199. # * Some definitions also rearranged.
  200. # * (c) Lukasz Sokol, 2011 License : GPL
  201. # *)
  202.  
  203. obj-m := kernel_pmodule.o
  204. kernel_pmodule-objs := kernel_module_info.o kernel_module.o system.o
  205. KDIR := /lib/modules/$(shell uname -r)/build
  206. PWD := $(shell pwd)
  207.  
  208. default:kernel_module.o
  209. $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
  210.  
  211. kernel_module.o: system.pas kernel_module.pas
  212. fpc kernel_module
  213.  
  214. clean:
  215. ${RM} *.ko *.mod.c *.o *.ppu Module.symvers .*.ko.cmd .*.o.cmd
  216. ${RM} -r .tmp_versions
  217.  
  218. ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement