{"id":16002693,"url":"https://github.com/blaind/rust_linux_kernel_example","last_synced_at":"2025-03-27T11:32:14.159Z","repository":{"id":143458215,"uuid":"562463128","full_name":"blaind/rust_linux_kernel_example","owner":"blaind","description":"Easily build and debug a Rust Linux Kernel module using qemu and gdb","archived":false,"fork":false,"pushed_at":"2022-11-07T17:45:07.000Z","size":1096,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-06T19:21:39.401Z","etag":null,"topics":["linux-kernel","linux-kernel-module","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blaind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-06T12:51:10.000Z","updated_at":"2025-03-04T21:11:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"4a2bd4e4-e2f0-474f-a081-e72b29eb0f2f","html_url":"https://github.com/blaind/rust_linux_kernel_example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaind%2Frust_linux_kernel_example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaind%2Frust_linux_kernel_example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaind%2Frust_linux_kernel_example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaind%2Frust_linux_kernel_example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blaind","download_url":"https://codeload.github.com/blaind/rust_linux_kernel_example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245137446,"owners_count":20566755,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linux-kernel","linux-kernel-module","rust"],"created_at":"2024-10-08T10:03:01.019Z","updated_at":"2025-03-27T11:32:14.152Z","avatar_url":"https://github.com/blaind.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rust - Linux kernel module example\n\nThis example shows how to build a Rust Linux kernel module, and debug it using gdb.\n\nThis should enable relatively rapid iteration when developing (e.g. edit module file, run `make debug_kernel`, attach gdb with `make gdb`, repeat)\n\n## Prequisites\n\nBuild dependencies:\n\n    sudo apt-get install make gcc cpio\n\nFetch Rust-For-Linux:\n\n    git submodule update --init\n\nRust-For-Linux prequisites, see [quick-start.rst](https://github.com/Rust-for-Linux/linux/blob/d9b2e84c0700782f26c9558a3eaacbe1f78c01e8/Documentation/rust/quick-start.rst). In brief (note! this overrides rustup config and installs specific version of bindgen)\n\n    cd linux\n    rustup override set $(scripts/min-tool-version.sh rustc)\n    rustup component add rust-src\n    cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen\n    make LLVM=1 rustavailable\n    # should print: \"Rust is available!\"\n\n## Building\n\nBuild the kernel module. Source located at `sample_module` path.\n\nThis uses `kernel-config` as a baseline config\n\n    make build_kernel\n\nThis will take a while\n\n## Running without gdb\n\nDo-it-all-command, which\n\n- builds the module (`rust_minimal.rs`)\n- builds init-binary\n- creates initramfs\n- launches qemu\n\n(see Makefile for details)\n\nRun it:\n\n    make run_kernel\n\nThis should open up a busybox shell, with the module already loaded.\n\n## Running with gdb\n\n### 1. Init, build module \u0026 Start qemu\n\nDo-it-all-command, which\n\n- builds the module (`rust_minimal.rs`)\n- builds init-binary\n- creates initramfs\n- launches qemu\n\n(see Makefile for details)\n\nRun it:\n\n    make debug_kernel\n\nIt should print a few lines, and be in idle mode. Leave it running\n\n### 2. Attach gdb to the qemu instance\n\nOpen another terminal window, and attach gdb to the emulator:\n\n    make gdb\n\nNow, you should see the qemu-kernel booting up and emulator eventually breaking in the rust module init\n\nEmulator (debug_kernel) should print:\n\n    ...\n    [    1.210080] Run /init as init process\n    init - entered main()\n    init - loading kernel module (iteration=0)\n    init - module file fd=3\n\ngdb should print:\n\n    Hardware assisted breakpoint 1 at 0xffffffffa0000474: file samples/rust/rust_minimal.rs, line 21.\n\n    Breakpoint 1, rust_minimal::{impl#0}::init (_name=..., _module=\u003coptimized out\u003e) at samples/rust/rust_minimal.rs:21\n    21\t        pr_info!(\"Rust minimal sample (init)\\n\");\n    (gdb)\n\n## Troubleshooting\n\n### Finding module address\n\nHopefully the module address stays the same (for symbols). If not, this may help (but please make a pull request for documenting a proper way to find the address)\n\nLook for the module kernel message:\n\n    [    5.532793] kobject: 'rust_minimal' (ffffffffa0002090): kobject_release, parent ffff888000928250 (delayed 2000)\n\nHere the address is `0xffffffffa0002090`.\n\nTODO: why +0x2090 to base address `0xffffffffa0000000`?\n\nUpdate gdb.config `add-symbol-file` section with the base address.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblaind%2Frust_linux_kernel_example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblaind%2Frust_linux_kernel_example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblaind%2Frust_linux_kernel_example/lists"}