Developing a Kernel Module lazarus / 66-Unix ---------------------------------------------------------------------- nicolap · 2024-12-02 16:37 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  Thanks       Nicola ---------------------------------------------------------------------- MarkMLl · 2024-12-03 08:13 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 ---------------------------------------------------------------------- Bogen85 · 2024-12-03 10:31 Quote from: nicolap on December 02, 2024, 05:37:05 pmI 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. Quote from: MarkMLl on December 03, 2024, 09:13:27 amFrankly, 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.orgYour module likely won't compile after a few months (or weeks?) after you update kernel sourcesIf 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 outUnless 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 moduleIf 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... https://ebpf.io/what-is-ebpf/ Of course to be feasible this would require a pas2ebpf (similar to what pas2js does)...https://www.kernel.org/doc/html/v6.0/driver-api/uio-howto.html Linux user-space I/O, this is the most practical option in my opinion if you really need this ---------------------------------------------------------------------- Bogen85 · 2024-12-03 13:16 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 · 2024-12-03 13:43 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 ---------------------------------------------------------------------- nicolap · 2024-12-03 14:09 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 · 2024-12-03 14:28 Quote from: nicolap on December 03, 2024, 03:09:06 pmThanks 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 · 2024-12-03 14:53 Quote from: Bogen85 on December 03, 2024, 03:28:38 pmCool! 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 ---------------------------------------------------------------------- PascalDragon · 2024-12-03 20:31 Quote from: nicolap on December 02, 2024, 05:37:05 pmI 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 · 2024-12-04 02:01 Quote from: MarkMLl on December 03, 2024, 03:53:56 pmQuote from: Bogen85 on December 03, 2024, 03:28:38 pmCool! 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 · 2024-12-04 02:06 Quote from: nicolap on December 03, 2024, 03:09:06 pmThanks 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.. ---------------------------------------------------------------------- Bogen85 · 2024-12-04 02:42 Quote from: Joanna from IRC on December 04, 2024, 03:06:52 amQuote from: nicolap on December 03, 2024, 03:09:06 pmThanks 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. ---------------------------------------------------------------------- Thaddy · 2024-12-04 04:56 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. ---------------------------------------------------------------------- MarkMLl · 2024-12-04 08:28 Quote from: Bogen85 on December 04, 2024, 03:01:23 amAlso 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 ---------------------------------------------------------------------- nicolap · 2024-12-04 09:53 Quote from: MarkMLl on December 04, 2024, 09:28:16 amRegrettably, 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" 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       Nicola ---------------------------------------------------------------------- Thaddy · 2024-12-04 09:56 I agree with you. Responses to the contrary were a bit short sighted  . (as per my previous response) But also note that for the device you use you have to tie the kernel version to your module. That is good advice. ---------------------------------------------------------------------- Joanna · 2024-12-04 10:16 It seems like he is writing a kernel module for his own use. If he wants to do it in pascal, more power to him. If nothing else it will make him better at programming in pascal. If he succeeds he will create something new and useful.  Either way I’m glad he will try. ---------------------------------------------------------------------- Bogen85 · 2024-12-04 10:17 Quote from: nicolap on December 04, 2024, 10:53:57 amI'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. If on the critical and security fixes only phase of a long term release for a kernel package from an Enterprise focused Linux distribution then "evolving" is on a much slower track, so much of that maintenance effort is mitigated. Also, you had mentioned ultrasound device. While all Linux kernel internal interfaces are subject to change, a device like that is not subject to the bleeding edge in making things like networking, video, and filesystems for performant, and since not on the network side of things, not part of the code that needs to be in flux to respond to security threats. On the other hand, Internet/Cloud is just one part of public facing networks.... (a private and so called secured wireless network is still public facing, unless the device is in a completely RF shielded from all directions underground bunker.) https://www.nist.gov/itl/applied-cybersecurity/nist-cybersecurity-iot- program But that is off topic for this thread. Quote from: nicolap on December 04, 2024, 10:53:57 amI 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" Since this discussion is in this forum, that is a given (not for kernel.org adoption). I will reiterate what I said earlier. You do understand the maintenance and other developments risks. Not everyone does, as many are naive about many things Linux. Quote from: Bogen85 on December 03, 2024, 03:28:38 pmCool! 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. ---------------------------------------------------------------------- Joanna · 2024-12-04 10:21 I’m just happy he wants to use pascal. I’m sure he will figure out the details on how to do it. It’s his project after all. ---------------------------------------------------------------------- MarkMLl · 2024-12-04 10:21 Quote from: nicolap on December 04, 2024, 10:53:57 amThis is nonsense. I do not consider to insert the driver in the kernel.org repository. Suit yourself, but don't misquote me. I said "...interpreted as trying in insinuate Pascal into the kernel...", and current events show how hostile the kernel developers are inclined to be to anything other than C. MarkMLl ---------------------------------------------------------------------- nicolap · 2024-12-08 11:15 Hi all, after a week of war I won the first battle! I was able to build a kernel module that uses a function written in pascal. Here the long story: https://stackoverflow.com/questions/79258798/using-a-function-written- in-other-language-than-c-in-kernel-module There I omitted a little thing...  just to filter out messages like "it's not possible"... I attached the .pas sources. Very minimal. Now I can write the rest of the code!      Nicola ---------------------------------------------------------------------- Thaddy · 2024-12-08 13:18 as I wrote, you have a perfectly valid reason to pursue this and you succeeded. Compliments. ---------------------------------------------------------------------- Joanna · 2024-12-08 14:16 nicolap Congratulations on your coding. Can you do me a favor and put the cone into the forums where I can read it easier? ---------------------------------------------------------------------- nicolap · 2024-12-08 14:39 Quote from: Joanna from IRC on December 08, 2024, 03:16:28 pmnicolap Congratulations on your coding. Can you do me a favor and put the cone into the forums where I can read it easier?  Let me a couple of days that I can write something more interesting! ---------------------------------------------------------------------- Joanna · 2024-12-08 15:35 I will look forward to it.