Recent

Author Topic: Developing a Kernel Module  (Read 2907 times)

nicolap

  • New Member
  • *
  • Posts: 10
Developing a Kernel Module
« on: December 02, 2024, 05:37:05 pm »
Hi all,
I would like to write a Kernel Module in Freepascal, anyone has esperience?
All that I found is an outdated page stating that it's possibile.

I'm using fpc 3.2.2 on a Raspberry Pi Zero 2 W  :P
Thanks
      Nicola

MarkMLl

  • Hero Member
  • *****
  • Posts: 8082
Re: Developing a Kernel Module
« Reply #1 on: December 03, 2024, 09:13:27 am »
Frankly, I would advise you to not do this.

The internal kernel APIs are somewhat fluid, and development relies heavily on preconfigured tools and makefiles. You need only to watch the effort to integrate Rust with kernel development to see this.

To a very large extent, kernel modules (at least routine ones, e.g. to support custom hardware), are written with reference to existing ones. Attempting to use a language for which there is not already copious "prior art" in the field will cause nothing but grief.

If you attempt to reach out to the kernel developers you will get derision rather than support, and don't expect much better from the RPi community.

The only time I've needed a kernel module was when accessing temperature sensors etc. on a SPARC system, and that was around 20 years ago.

Unless you have a /very/ good reason, in any field it's best to study prior art and to comply with established conventions when extending it. In the case of the kernel that amounts to C or Rust, and realistic choices are not all that different when programming e.g. an embedded Arduino or similar single-board computer.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Developing a Kernel Module
« Reply #2 on: December 03, 2024, 11:31:23 am »
I would like to write a Kernel Module in Freepascal, anyone has esperience?
All that I found is an outdated page stating that it's possibile.

Possible, yeah.... Practical? No.

Frankly, I would advise you to not do this.

As would I, for all the reasons Mark shared.

Unless you are pursuing this as purely academic one time "adventure" (YOLO?), it is not worth it.

  • Your module will never be accepted by kernel.org
  • Your module likely won't compile after a few months (or weeks?) after you update kernel sources
  • If you decided to target a so called LTS kernel, you will have to rewrite much of your module later when the next LTS release comes out
  • Unless only you is going to use this on your own machines, it is unlikely that you will convince many others to run your exact LTS kernel variant and install your module
  • If you are doing this for a product you will need to hire a full time Linux developer (or more than one depending on the complexity of the module) who will need to interface with the kernel.org community. You likely won't find one willing to put up with the ridicule and derision from kernel.org over writing a Linux kernel module in Pascal.
  • Hardware vendors with out of tree Linux modules have dedicated Linux developer teams. (Ones that don't can't reasonably maintain them for long)
  • Developing a module for Linux is only practical in the long run for most people if the plan is to get the module into the mainline kernel, which means acceptance of your module by the kernel.org community.

Maintaining a windows kernel module is far easier. (And I'm not a windows or a Microsoft advocate). Dealing with driver signing (self signed or WHQL) is far easier (in the short and long term) than dealing with kernel.org and other issues surrounding a Linux kernel module that will always be out of tree.

However, if writing a kernel module (or something akin to it) in Pascal is still something you really want to do, there are a couple more "practical" (you decide how practical...) approaches you could take...


« Last Edit: December 03, 2024, 02:36:11 pm by Bogen85 »

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Developing a Kernel Module
« Reply #3 on: December 03, 2024, 02:16:11 pm »
Apart from the "never be accepted" and "derision and ridicule" because the Linux kernel module is being done in Pascal, everything else applies to any out of kernel.org kernel module source. In other words, any kernel module that is not intended to be handed over to kernel.org when it gets running initially, where the discussions to do this are started when the kernel module is started be be developed.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8082
Re: Developing a Kernel Module
« Reply #4 on: December 03, 2024, 02:43:18 pm »
It might be possible to isolate some of the /technical/ process- i.e. the API against which a kernel module is to be linked- by using DKMS. I've used that for a couple of modules in my Github repo with success, but never got around to following up upstream changes somebody later made (which of course implies that intimacy with Git is a prerequisite for anything kernel-related).

That doesn't help with the /understanding/ process. I intentionally held off commenting in case anybody in this community was sufficiently confident that such a thing could be done: while I see https://wiki.lazarus.freepascal.org/linux/kernel/module_development (which is presumably the page to which OP refers) I'd point out that it's had no significant attention since 2011 which is /yonks/ as far as Linux is concerned.

OP: what /exactly/ are you trying to do, why do you believe you need a kernel module, and why do you think it's plausible to write it using FPC?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

nicolap

  • New Member
  • *
  • Posts: 10
Re: Developing a Kernel Module
« Reply #5 on: December 03, 2024, 03:09:06 pm »
Thanks all for the replies!
I already wrote a kernel module in C and it works. It's for an ultrasound device.
At this moment ther driver is minimal and I would like to extend the functionality.
I "don't like" C and love Pascal so I thought that I can try!

I will report my efforts results!
       Nicola

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Developing a Kernel Module
« Reply #6 on: December 03, 2024, 03:28:38 pm »
Thanks all for the replies!
I already wrote a kernel module in C and it works. It's for an ultrasound device.
At this moment ther driver is minimal and I would like to extend the functionality.
I "don't like" C and love Pascal so I thought that I can try!

I will report my efforts results!
       Nicola

Cool! And please don't take what I wrote to completely dissuade you. I'm just being realistic as to long term maintenance of out of tree Linux kernel modules.

As long as you isolate as much as possible the parts of your module that interact with the Linux kernel ABI and the the parts of your module that provide the core functionality, you can mitigate long term maintenance risks.

Modules that will never be included in the kernel.org source tree need clean layering being the Linux kernel ABI and the rest of your code. As long as you take some time up front to define a clean API (exposing the kernel functionality) that won't need to change for the parts written in Pascal, you will make things easier for yourself in the long run. However, expect the glue layer to change.

This not only applies to modules written in a language not accepted by kernel.org, it applies to all modules that never be adopted by kernel.org.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8082
Re: Developing a Kernel Module
« Reply #7 on: December 03, 2024, 03:53:56 pm »
Cool! And please don't take what I wrote to completely dissuade you. I'm just being realistic as to long term maintenance of out of tree Linux kernel modules.

I broadly agree. But "not liking C" isn't sufficient argument to dismiss it as the kernel's "lingua franca": there are plenty of people who don't like English but use it perforce.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5796
  • Compiler Developer
Re: Developing a Kernel Module
« Reply #8 on: December 03, 2024, 09:31:33 pm »
I would like to write a Kernel Module in Freepascal, anyone has esperience?
All that I found is an outdated page stating that it's possibile.

Probably the whole experience that is available on using FPC for Linux kernel module development is concentrated in this page (even though the last changes had been 13 years ago). Feel free to experiement with it and to update that page.

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Developing a Kernel Module
« Reply #9 on: December 04, 2024, 03:01:23 am »
Cool! And please don't take what I wrote to completely dissuade you. I'm just being realistic as to long term maintenance of out of tree Linux kernel modules.

I broadly agree. But "not liking C" isn't sufficient argument to dismiss it as the kernel's "lingua franca": there are plenty of people who don't like English but use it perforce.

MarkMLl

Personally I'd rather not have work on any Linux kernel modules (did a few times in the past).

Also personally I'd not even consider using a "not used here" language (like Pascal).

There are enough hassles to deal with from kernel.org and from organizations whose Linux kernel trees diverged from kernel.org.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1265
Re: Developing a Kernel Module
« Reply #10 on: December 04, 2024, 03:06:52 am »
Thanks all for the replies!
I already wrote a kernel module in C and it works. It's for an ultrasound device.
At this moment ther driver is minimal and I would like to extend the functionality.
I "don't like" C and love Pascal so I thought that I can try!

I will report my efforts results!
       Nicola
Please share your progress, I’d love to see the naysayers proven wrong..
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Developing a Kernel Module
« Reply #11 on: December 04, 2024, 03:42:15 am »
Thanks all for the replies!
I already wrote a kernel module in C and it works. It's for an ultrasound device.
At this moment ther driver is minimal and I would like to extend the functionality.
I "don't like" C and love Pascal so I thought that I can try!

I will report my efforts results!
       Nicola
Please share your progress, I’d love to see the naysayers proven wrong..

Doing the initial development for a Linux kernel module is not the hard part. The hard part is continually fixing it due to kernel source updates. And that is even for "these languages are used in the Linux kernel".

Proven wrong would be that the Linux kernel source tree the module is built against is updated every week, and the module updated as needed. And that after 5-10 years that was never a hassle to do so.

And no, you just can't stay on some Linux kernel version forever, that is not how using Linux works. And the longer you go between updating and fixing, the harder it gets.

My being a "naysayer" in this thread is primarily because I know how hard maintaining out of tree kernel modules is. This being done in Pascal is secondary. The Linux kernel was not designed to be friendly to any languages other than C (and now Rust, but this is still difficult), but that can be overcome to some degree.

That (out of tree) has always been a difficult area in Linux. No one is going to prove us "naysayers" wrong.

Yeah, if you are Oracle, AMD, Nvidia, etc, and want to have full time Linux kernel developers, that is one thing. But that is not what this thread is about.
« Last Edit: December 04, 2024, 03:47:24 am by Bogen85 »

Thaddy

  • Hero Member
  • *****
  • Posts: 16305
  • Censorship about opinions does not belong here.
Re: Developing a Kernel Module
« Reply #12 on: December 04, 2024, 05:56:45 am »
To a certain extend it is, because linux <>desktop/server.
The ability to write kernel modules in a preferred language is a great and viable option for example on IOT platforms where you can tie the device to a kernel version. This already happens a lot with cheap Android devices, like tablets and phones: only the big brands do kernel updates.
Besides, the linux kernels on Android devices are usually already full of kernel modules that have never seen kernel.org.
So, for Android or IOT devices it should not be a problem, these are not the same as servers and desktops.
For a $15 device like the Raspberry Pi Zero 2 W it is an excellent option.
I have a lot of Pi's running, including the above, but I only update those running as server or desktop.
« Last Edit: December 04, 2024, 06:14:30 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8082
Re: Developing a Kernel Module
« Reply #13 on: December 04, 2024, 09:28:16 am »
Also personally I'd not even consider using a "not used here" language (like Pascal).

Regrettably, that is true. And I very much don't like saying this explicitly, but there's a risk that somebody saying in public "FPC can be used to write kernel modules!" would be interpreted as trying in insinuate Pascal into the kernel: and that would be to this community's disadvantage since recent events have highlighted the hostility towards "anything non-C" exhibited by even some of the more mature kernel maintainers.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

nicolap

  • New Member
  • *
  • Posts: 10
Re: Developing a Kernel Module
« Reply #14 on: December 04, 2024, 10:53:57 am »
Regrettably, that is true. And I very much don't like saying this explicitly, but there's a risk that somebody saying in public "FPC can be used to write kernel modules!" would be interpreted as trying in insinuate Pascal into the kernel: and that would be to this community's disadvantage since recent events have highlighted the hostility towards "anything non-C" exhibited by even some of the more mature kernel maintainers.

MarkMLl
This is nonsense.

I do not consider to insert the driver in the kernel.org repository. I know well how it "works".
And all we know that, as someone sayd: "The Linusx kernel was not designed to be friendly" O:-)
I'm also well aware of the cost and the difficulty of maintaining the code for the "evolving" kernel...
But, as other sayd, it's for an IOT project, rare update, not connected to internet/cloud, with very low security needs.

For now I'm having problems with Makefile (no comment) that doesn't knows Freepascal and with ld (the linker) that report different errors in different places: different from reality >:D
      Nicola

 

TinyPortal © 2005-2018