LinuxKPI: Linux Drivers on FreeBSD

By Jake Freeland on July 5, 2022
Edited on July 8, 2022

In last week's Google Summer of Code update, I wrote that the next post would focus on the details of compiling Intel's igt-gpu-tools on a FreeBSD system. The first paragraph will still follow this theme, but the remainder of the article will highlight my experience with FreeBSD's LinuxKPI compatibility layer and rewriting GPL code under a permissive license. Before I start, it is important to note that I am not a legal professional and this is not legal advice.

A Successful Compilation

A good first step when porting any program over to FreeBSD is removing incompatible code and compiling. I am working with igt-gpu-tools, a Linux program, so simple #ifdef __linux__ preprocessor conditionals serve as a great way to avoid compiling Linux-specific code while keeping it intact for future reference. I made sure to leave comments next to modifications following this format: /* TODO: FreeBSD - [reason for modification]. This makes it easy to grep -r "TODO: FreeBSD" and retrieve every file that needs work. After igt-gpu-tools compiled successfully, I used the grep output to create a prioritized TODO list. Code that contributed to igt-gpu-tool's core functionality received a high priority while trivial features were given a low priority. FreeBSD drm-kmod debugfs support took the crown for the most urgent task, but took me down a new rabbit hole.

LinuxKPI Makes FreeBSD Driver Development Easier

LinuxKPI is a small compatibility layer that allows Linux drivers to run in a FreeBSD environment with minor modification. The drm-kmod package, for example, includes Intel and AMD Linux graphics driver code, but is able to compile and run properly on FreeBSD using LinuxKPI. Even though drm-kmod provides a working driver, there exist FreeBSD specific bugs, performance hits, and missing features as a result of LinuxKPI's small codebase. To put it simply, LinuxKPI is incomplete and drm-debugfs support is one of many missing features. This drm-debugfs support is essential to igt-gpu-tools, so my next task was compiling drm-kmod with the DEBUG_FS configuration flag enabled.

The Battle Against GPL

Although GPL has its place in the open source software community, FreeBSD openly strives to eliminate its presence in the SRC tree. Since LinuxKPI is in that SRC tree, all GPLv2 licensed Linux code must be rewritten under FreeBSD's 2-clause BSD License. The most logical way to legally re-engineer the code is to follow the clean room design process. This is undoubtedly daunting, but can be accomplished by analyzing the GPL code, writing function specifications, and getting someone else to implement those specifications. The legality of this process rests in a grey area but serves as one of the only reliable ways to recreate GPLv2 licensed code. I am currently in the process of drafting specifications and rewriting functions to get drm-kmod's debugfs functionality working.

igt-gpu-tools and debugfs

Since igt-gpu-tools monitors and tests kernel-level drm driver behavior, debugfs serves as the perfect companion to send these test results into the user space. After debugfs support is built into drm-kmod through LinuxKPI, this functionality can be verified. I will make sure to document my difficulties and report back when I have relevant information.

Thanks to FreeBSD's Mark Johnston for guiding me through this material. If you liked this content or you're interested in following my Google Summer of Code 2022 blog series, click the gsoc2022 tag below.


Tags: freebsd gsoc2022