Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-01

Building kernel modules using fpc (for fun)

27 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Lukasz Sokol

Hi,
http://wiki.freepascal.org/linux/kernel/module_development has some information, unfortunately after copy-pasting into relevant files, and running

make

i get

fpc kernel_module
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386 Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling kernel_module.pas
Compiling system.pas
kernel_module.pas(165,2) Note: Local variable "data" not used kernel_module.pas(166,2) Note: Local variable "cmd" not used kernel_module.pas(15,2) Note: Local variable "i" not used kernel_module.pas(15,5) Note: Local variable "j" not used kernel_module.pas(28,2) Note: Local variable "msg" not used kernel_module.pas(29,2) Note: Local variable "msg_Ptr" not used 471 lines compiled, 0.1 sec
6 note(s) issued
make -C /lib/modules/2.6.32-27-generic/build SUBDIRS=/home/lukasz/projects /kfpc
modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-27-generic' CC [M] /home/lukasz/projects/kfpc/kernel_module_info.o LD [M] /home/lukasz/projects/kfpc/kernel_pmodule.o Building modules, stage 2.
MODPOST 1 modules
WARNING: could not find /home/lukasz/projects/kfpc/.kernel_module.o.cmd for /home/lukasz/projects/kfpc/kernel_module.o
WARNING: "register_chrdev" [/home/lukasz/projects/kfpc/kernel_pmodule.ko] undefined!
WARNING: "proc_root" [/home/lukasz/projects/kfpc/kernel_pmodule.ko] undefined! WARNING: "unregister_chrdev" [/home/lukasz/projects/kfpc/kernel_pmodule.ko] undefined!
CC /home/lukasz/projects/kfpc/kernel_pmodule.mod.o LD [M] /home/lukasz/projects/kfpc/kernel_pmodule.ko make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-27-generic'

(fpc is
fpc
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386 default from ubuntu 10.04 LTS)

Issuing
make V=1

gives (cut out the fpc part) :

make -C /lib/modules/2.6.32-27-generic/build SUBDIRS=/home/lukasz/projects/kfpc modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-27-generic' test -e include/linux/autoconf.h -a -e include/config/auto.conf || ( \ echo; \
echo " ERROR: Kernel configuration is invalid."; \ echo " include/linux/autoconf.h or include/config/auto.conf are missing."; \
echo " Run 'make oldconfig && make prepare' on kernel src to fix it."; \ echo; \
/bin/false)
mkdir -p /home/lukasz/projects/kfpc/.tmp_versions ; rm -f /home/lukasz/projects/kfpc/.tmp_versions/*
make -f scripts/Makefile.build obj=/home/lukasz/projects/kfpc gcc -Wp,-MD,/home/lukasz/projects/kfpc/.kernel_module_info.o.d -nostdinc -isystem /usr/lib/gcc/i486-linux-gnu/4.4.3/include -Iinclude -I/usr/src/linux-headers-2.6.32-27-generic/arch/x86/include -include include/linux/autoconf.h -Iubuntu/include -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -fno-delete-null-pointer-checks -O2 -m32 -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2 -march=i586 -mtune=generic -maccumulate-outgoing-args -Wa,-mtune=generic32 -ffreestanding -fstack-protector -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -Wframe-larger-than=1024 -fno-omit-frame-pointer -fno-optimize-sibling-calls -pg -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fno-dwarf2-cfi-asm -fconserve-stack -DMODULE -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(kernel_module_info)" -D"KBUILD_MODNAME=KBUILD_STR(kernel_pmodule)" -c -o /home/lukasz/projects/kfpc/.tmp_kernel_module_info.o /home/lukasz/projects/kfpc/kernel_module_info.c
set -e ; perl /usr/src/linux-headers-2.6.32-27-generic/scripts/recordmcount.pl "i386" "32" "objdump" "objcopy" "gcc" "ld" "nm" "" "" "1" "/home/lukasz/projects/kfpc/kernel_module_info.o"; ld -m elf_i386 -r -o /home/lukasz/projects/kfpc/kernel_pmodule.o /home/lukasz/projects/kfpc/kernel_module_info.o
/home/lukasz/projects/kfpc/kernel_module.o /home/lukasz/projects/kfpc/system.o (cat /dev/null; echo kernel//home/lukasz/projects/kfpc/kernel_pmodule.ko;) > /home/lukasz/projects/kfpc/modules.order
make -f /usr/src/linux-headers-2.6.32-27-generic/scripts/Makefile.modpost scripts/mod/modpost -m -a -i
/usr/src/linux-headers-2.6.32-27-generic/Module.symvers -I /home/lukasz/projects/kfpc/Module.symvers -o
/home/lukasz/projects/kfpc/Module.symvers -S -w -s WARNING: could not find /home/lukasz/projects/kfpc/.kernel_module.o.cmd for /home/lukasz/projects/kfpc/kernel_module.o
WARNING: "register_chrdev" [/home/lukasz/projects/kfpc/kernel_pmodule.ko] undefined!
WARNING: "proc_root" [/home/lukasz/projects/kfpc/kernel_pmodule.ko] undefined!
WARNING: "unregister_chrdev" [/home/lukasz/projects/kfpc/kernel_pmodule.ko] undefined!
gcc -Wp,-MD,/home/lukasz/projects/kfpc/.kernel_pmodule.mod.o.d -nostdinc -isystem /usr/lib/gcc/i486-linux-gnu/4.4.3/include -Iinclude -I/usr/src/linux-headers-2.6.32-27-generic/arch/x86/include -include include/linux/autoconf.h -Iubuntu/include -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -fno-delete-null-pointer-checks -O2 -m32 -msoft-float -mregparm=3 -freg-struct-return -mpreferred-stack-boundary=2 -march=i586 -mtune=generic -maccumulate-outgoing-args -Wa,-mtune=generic32 -ffreestanding -fstack-protector
-DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -Wframe-larger-than=1024 -fno-omit-frame-pointer -fno-optimize-sibling-calls -pg
-Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fno-dwarf2-cfi-asm -fconserve-stack -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(kernel_pmodule.mod)" -D"KBUILD_MODNAME=KBUILD_STR(kernel_pmodule)" -DMODULE -c -o /home/lukasz/projects/kfpc/kernel_pmodule.mod.o
/home/lukasz/projects/kfpc/kernel_pmodule.mod.c
ld -r -m elf_i386 -T
/usr/src/linux-headers-2.6.32-27-generic/scripts/module-common.lds --build-id -o
/home/lukasz/projects/kfpc/kernel_pmodule.ko
/home/lukasz/projects/kfpc/kernel_pmodule.o
/home/lukasz/projects/kfpc/kernel_pmodule.mod.o
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-27-generic'

Is this something I should ask here or at kernel-newbies ? :)

Lukasz

Reply by email

Lukasz Sokol
to Lukasz Sokol

Lukasz Sokol writes:

ah and sorry some clarifications :
- i posted using gmane web interface that forces me to wrap lines at 80 chars manually
- what happens after: if I tell my system to
sudo insmod kernel_pmodule

results in

insmod: error inserting 'kernel_pmodule.ko': -1 Unknown symbol in module

and

[16646.934537] kernel_pmodule: Unknown symbol unregister_chrdev [16646.934835] kernel_pmodule: Unknown symbol proc_root [16646.935174] kernel_pmodule: Unknown symbol register_chrdev

in dmesg

(i.e. the same that the modpost was warning about) and the module is not loaded)

Thanks for your attention,
Lukasz
(yes this is for fun :)

Reply by email

Jonas Maebe
to Lukasz Sokol
Lukasz Sokol wrote 7 lines
insmod: error inserting 'kernel_pmodule.ko': -1 Unknown symbol in module and [16646.934537] kernel_pmodule: Unknown symbol unregister_chrdev [16646.934835] kernel_pmodule: Unknown symbol proc_root [16646.935174] kernel_pmodule: Unknown symbol register_chrdev

Since the Linux kernel breaks ABI compatibility on purpose all the time, any source code example of a kernel module is probably only going to to work with a few kernel versions. E.g., in the current kernel register_chrdev() is a "static inline" function, which means that it does not exist as a separate function/symbol inside the final compiled kernel.

Jonas

Reply by email

Henry Vermaak
to Sven Barth: I might not be able to help you regarding this topic, but I believe that you'll get a…
Sven Barth wrote 6 lines
On 15.01.2011 20:32, Lukasz Sokol wrote: > > Is this something I should ask here or at kernel-newbies ? :) I might not be able to help you regarding this topic, but I believe that you'll get a wall of "use C, god damnit" if you ask there :P

Heh, well, :) It's pretty cool for simple drivers. I don't think you can get very far without translating a huge amount of header files and nasty macros, though.

Henry

Reply by email

Lukasz Sokol
to Henry Vermaak: Have you done this? It's required for building modules out of tree, iirc. Henry
Henry Vermaak wrote
On 15 January 2011 19:32, Lukasz Sokol wrote: >        echo "         Run 'make oldconfig && make prepare' on kernel src to

fix it.";  \

Have you done this? It's required for building modules out of tree, iirc.

No but I have installed the kernel-headers and source for actually running ubuntu kernel... Does that make a difference ?

Anyway it seems like the example provided is wrong (see Jonas' reply ;) for it uses a function that is currently static inline...

Lukasz.
(but I still thinking of having fun with this ;)

Reply by email

Lukasz Sokol
to Henry Vermaak: Heh, well, :) It's pretty cool for simple drivers. I don't think you can get very far…
Henry Vermaak wrote
On 15 January 2011 19:48, Sven Barth wrote: > I might not be able to help you regarding this topic, but I believe that > you'll get a wall of "use C, god damnit" if you ask there :P

@Sven : it's for fun. Whether I'd be having fun is probably debatable but it may be fun for many ;) And then, you know the steps : first they laugh at you...

Show quoted text 6 lines
Heh, well, :) It's pretty cool for simple drivers. I don't think you can get very far without translating a huge amount of header files and nasty macros, though. Henry

Yeah, see Jonas' reply about breaking ABI all the time ;) (but then, hey, somebody wrote ctopas or whatsitcalled...)

Maybe somebody could produce an example that is at least insmodable? (even e.g. hello world) :)

Lukasz

Reply by email

Lukasz Sokol
to Jonas Maebe: Since the Linux kernel breaks ABI compatibility on purpose all the time, any source code…
Jonas Maebe wrote 5 lines
> [16646.934537] kernel_pmodule: Unknown symbol unregister_chrdev > [16646.934835] kernel_pmodule: Unknown symbol proc_root > [16646.935174] kernel_pmodule: Unknown symbol register_chrdev Since the Linux kernel breaks ABI compatibility on purpose all the time, any

source code example of a kernel

module is probably only going to to work with a few kernel versions. E.g., in

the current kernel

register_chrdev() is a "static inline" function, which means that it does not

exist as a separate

function/symbol inside the final compiled kernel. Jonas

Hello Jonas, thanks for reply :)
Seems like the instructions don't just have a very fortunate example maybe?

(let's see if I can understand enough just to produce an insmodable 'hello world' example... )

Lukasz

Reply by email

Henry Vermaak
to Lukasz Sokol: ah and sorry some clarifications : - i posted using gmane web interface that forces me to…
Lukasz Sokol wrote 17 lines
Lukasz Sokol writes: ah and sorry some clarifications : - i posted using gmane web interface that forces me to wrap lines at 80 chars manually - what happens after: if I tell my system to sudo insmod kernel_pmodule results in insmod: error inserting 'kernel_pmodule.ko': -1 Unknown symbol in module and [16646.934537] kernel_pmodule: Unknown symbol unregister_chrdev [16646.934835] kernel_pmodule: Unknown symbol proc_root [16646.935174] kernel_pmodule: Unknown symbol register_chrdev

You'll have to modify your kernel rtl. For example, register_chrdev now calls __register_chrdev (include/linux/fs.h).

Henry

Reply by email

Sven Barth
to Henry Vermaak: Heh, well, :) It's pretty cool for simple drivers. I don't think you can get very far…
Henry Vermaak wrote 11 lines
On 15 January 2011 19:48, Sven Barth wrote: > On 15.01.2011 20:32, Lukasz Sokol wrote: >> >> Is this something I should ask here or at kernel-newbies ? :) > > I might not be able to help you regarding this topic, but I believe that > you'll get a wall of "use C, god damnit" if you ask there :P Heh, well, :) It's pretty cool for simple drivers. I don't think you can get very far without translating a huge amount of header files and nasty macros, though.

I don't say that writing kernel modules in Pascal is bad (I even wrote the equivalent for Windows NT), but I bet that the guys on a Linux kernel list will think otherwise. :P

Regards,
Sven

Reply by email

Sven Barth
to Lukasz Sokol: @Sven : it's for fun. Whether I'd be having fun is probably debatable but it may be fun…
Lukasz Sokol wrote 9 lines
Henry Vermaak writes: > > On 15 January 2011 19:48, Sven Barth wrote: >> I might not be able to help you regarding this topic, but I believe that >> you'll get a wall of "use C, god damnit" if you ask there :P @Sven : it's for fun. Whether I'd be having fun is probably debatable but it may be fun for many ;) And then, you know the steps : first they laugh at you...

Yes, I don't want to spoil your fun. I even do such things myself sometimes. See the port to Native NT (which also allows you basically to write Windows NT drivers). But the developers on a Linux kernel list might not be that open minded ^^

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Jonas Maebe: It is simply for an older kernel version. The unfortunate part is that it doesn't mention…
Jonas Maebe wrote 5 lines
On 15 Jan 2011, at 21:16, Lukasz Sokol wrote: > Seems like the instructions don't just have a very fortunate example maybe? It is simply for an older kernel version. The unfortunate part is that it doesn't mention which kernel version it's for.

Google suggests that he did the work in late '07. Since he's a Debian package maintainer etc. I'd expect him to have been using at least Etch i.e. kernel 2.6.18.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Lukasz Sokol
to Lukasz Sokol: http://wiki.freepascal.org/linux/kernel/module_development has some information,…

Hey,

take a look.

I've basically removed all the calls that were referring to creating, reading and writing the procfs file.

Only left init_module and cleanup_module so I only left trivial Hello World and Bye-bye messages.
In fact I think even that should be made par with current linux standard. But that's for another few hours of fun ;)
Plus it prints the intro and outro messages in new lines ;)

http://pastebin.com/9D9ye7Pt (contains both system.pas and kernel_module.pas)

This builds and insmods and rmmods ;) still but need the kernel_module_info.c file and Makefile from the original description.

Amends and new ideas welcome.

Lukasz

Reply by email

Paul Breneman
to Sven Barth: Yes, I don't want to spoil your fun. I even do such things myself sometimes. See the port…
Sven Barth wrote 18 lines
On 15.01.2011 21:10, Lukasz Sokol wrote: > Henry Vermaak writes: > >> >> On 15 January 2011 19:48, Sven Barth wrote: >>> I might not be able to help you regarding this topic, but I believe >>> that >>> you'll get a wall of "use C, god damnit" if you ask there :P > > @Sven : it's for fun. Whether I'd be having fun is probably debatable > but it may > be fun for many ;) And then, you know the steps : first they laugh at > you... Yes, I don't want to spoil your fun. I even do such things myself sometimes. See the port to Native NT (which also allows you basically to write Windows NT drivers). But the developers on a Linux kernel list might not be that open minded ^^

I have hopes that ReactOS might be usable for some embedded stuff, and it is good to know if I need any custom drivers that something exists in FPC. :)

Reply by email

Paul Breneman
to Lukasz Sokol: take a look. I've basically removed all the calls that were referring to creating, reading
Lukasz Sokol wrote 19 lines
Hey, take a look. I've basically removed all the calls that were referring to creating, reading and writing the procfs file. Only left init_module and cleanup_module so I only left trivial Hello World and Bye-bye messages. In fact I think even that should be made par with current linux standard. But that's for another few hours of fun ;) Plus it prints the intro and outro messages in new lines ;) http://pastebin.com/9D9ye7Pt (contains both system.pas and kernel_module.pas) This builds and insmods and rmmods ;) still but need the kernel_module_info.c file and Makefile from the original description. Amends and new ideas welcome.

This looks *very* interesting. I'd like to add it (with *big* red flags and notes not to try it on a production PC) to this page: http://www.turbocontrol.com/helloworld.htm

Reply by email

Mark Morgan Lloyd
to Paul Breneman
Paul Breneman wrote 27 lines
Lukasz Sokol wrote: > Hey, > > take a look. > > I've basically removed all the calls that were referring to creating, > reading > and writing the procfs file. > > Only left init_module and cleanup_module so I only left trivial Hello > World and > Bye-bye messages. > In fact I think even that should be made par with current linux > standard. But > that's for another few hours of fun ;) > Plus it prints the intro and outro messages in new lines ;) > > http://pastebin.com/9D9ye7Pt (contains both system.pas and > kernel_module.pas) > > This builds and insmods and rmmods ;) still but need the > kernel_module_info.c > file and Makefile from the original description. > > Amends and new ideas welcome. This looks *very* interesting.

Indeed.

I'd like to add it (with *big* red flags and notes not to try it on a production PC) to this page: http://www.turbocontrol.com/helloworld.htm

Indeed. Note that the original author suggested that running it in a UML (User Mode Linux) guest would be appropriate, I've used UML in the past and found it very useful (although with comparatively slow disc access, and limited to x86).

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Paul Breneman: I have hopes that ReactOS might be usable for some embedded stuff, and it is good to know…
Paul Breneman wrote 23 lines
Sven Barth wrote: > On 15.01.2011 21:10, Lukasz Sokol wrote: >> Henry Vermaak writes: >> >>> >>> On 15 January 2011 19:48, Sven Barth wrote: >>>> I might not be able to help you regarding this topic, but I believe >>>> that >>>> you'll get a wall of "use C, god damnit" if you ask there :P >> >> @Sven : it's for fun. Whether I'd be having fun is probably debatable >> but it may >> be fun for many ;) And then, you know the steps : first they laugh at >> you... > > Yes, I don't want to spoil your fun. I even do such things myself > sometimes. See the port to Native NT (which also allows you basically > to write Windows NT drivers). But the developers on a Linux kernel > list might not be that open minded ^^ I have hopes that ReactOS might be usable for some embedded stuff, and it is good to know if I need any custom drivers that something exists in FPC. :)

It's not fully implemented currently. The last big update from me was to get the RTL nearly completly running for user mode applications. I haven't tested it inside kernel mode yet. But even if it works I wouldn't recommend to use the functions provided by the RTL inside a device driver, because you have to less control over what happens inside those functions (especially regarding paged vs. non-paged memory).

The state of the kernel mode part of Native NT is currently this:
* only legacy type device drivers tested (none of the more current driver frameworks)
* no usage of the kernel's exception handling (also applies to user mode); currently you'd need to do this manually in assembler
* no thread related functions
* only x86 currently

It should be not a big problem to enable x86_64, but currently I don't have a machine to test this (when I upgrade my "server" I hope to be able to run x86_64 Windows in a VM).

I also plan to add ARM support once ReactOS' ARM port can boot to user mode (I'd need to test this...)

You might want to keep an eye on this wiki page:
http://wiki.freepascal.org/Target_NativeNT

Regards,
Sven

Reply by email

Lukasz Sokol
to Lukasz Sokol: take a look. I've basically removed all the calls that were referring to creating, reading

Lukasz Sokol writes:

I have posted
http://pastebin.com/rM5GKB1q

which is the new version of http://pastebin.com/9D9ye7Pt.

This contains all 4 files needed for compilation, also moved the {$LINK } to the kernel_module.pas (and also the printk() declaration moved there; Added some of my touch to the files but went extra mile to keep the original author (hopefully) happy.

It builds and insmods/rrmmods, printing Hello World and Bye-bye into dmesg ;)

(this is trivial enough code that I don't think it should eat my computer, so I didn't bother with UML ;) or just like living dangerously... )

Why I decided to take the printk() definition out of system.pas ?

Well it seems to me more like 'Pascal way' that way. I think my next move would be to put all the available versions of printk() (and all you can think of that should be associated with it, but not module specific) into linux/printk.pas or whatever would be appropriate and find out if it can be just added into the 'uses' section of the main kernel module pascal code... (implementation/uses, probably ?)

(That way it should also be easier to track the never-changing-story of kernel internals ;)
But that's for another hour of fun next time round ;)

For now,
enjoy and you have some fun too ;)
Lukasz

Reply by email

Lukasz Sokol
to Paul Breneman: This looks *very* interesting. I'd like to add it (with *big* red flags and notes not to…
Paul Breneman wrote
This looks *very* interesting. I'd like to add it (with *big* red flags and notes not to try it on a production PC) to this page: http://www.turbocontrol.com/helloworld.htm

Yes please, you're welcome :) from my side no contradictions - you may like to talk to the man who started it ;) There is still some stuff I'd like to do to it, but a link to original instructions (fpc wiki) first, and the link to the pastebin after that, would probably do all right ;)

I intend to play a bit more with it anyway, it is not finished to the point I'd
like to have it;

But there is something to start with.

Lukasz

Reply by email

Paul Breneman
to Lukasz Sokol: I decided to put the result of my fiddling back onto the wiki…
Lukasz Sokol wrote 9 lines
Lukasz Sokol writes: I decided to put the result of my fiddling back onto the wiki http://wiki.freepascal.org/linux/kernel/module_development as the pastebin seems to have disappeared mysteriously. Enjoy! Lukasz

I couldn't find anything but the above wiki page which is almost 5 years old. I might want to make an example for this wiki page: http://wiki.freepascal.org/Small_Virtual_Machines

Any suggestions or links?

Regards,
Paul
www.ControlPascal.com

Reply by email

Lukasz Sokol
to Paul Breneman

Hello Paul,

Paul Breneman wrote 16 lines
On 01/23/2011 08:15 AM, Lukasz Sokol wrote: > Lukasz Sokol writes: > > I decided to put the result of my fiddling back onto the wiki > http://wiki.freepascal.org/linux/kernel/module_development as the pastebin > seems to have disappeared mysteriously. > > Enjoy! > > Lukasz I couldn't find anything but the above wiki page which is almost 5 years old. I might want to make an example for this wiki page: http://wiki.freepascal.org/Small_Virtual_Machines Any suggestions or links?

Wow... it's been years since I did anything in this area.

All I ever tried or knew, and the example module using printk, is only on that one page.

Contributors are welcome...

(I never had communicated anything with the original author, Mazen Neifer, if he's still around - which I think so - he could link / keyword you more, I suppose)

Regards, Paul www.ControlPascal.com

Lukasz

Reply by email