Skip to content

Instantly share code, notes, and snippets.

@Kreijstal
Last active December 24, 2024 21:02
Show Gist options
  • Save Kreijstal/a749bb4442714515f93efdb7f6306e69 to your computer and use it in GitHub Desktop.
Save Kreijstal/a749bb4442714515f93efdb7f6306e69 to your computer and use it in GitHub Desktop.
Betribsystempraktikum von TU Berlin toolchain mit guix

Overview

BSPrak is a custom setup for using QEMU through the GNU Guix package manager. This repository contains scripts and configurations to manage a custom QEMU build along with other essential tools.

Enter the Development Environment: Use guix shell to enter a shell with all necessary dependencies:

guix shell -m manifest.scm

TODO: add gcc 14

;(use-modules (ice-9 readline))
;(activate-readline)
;(use-modules (oop goops)) ; for class-of
(use-modules (web client)
(web uri)
(web response)
(srfi srfi-1)
(rnrs bytevectors)
(rnrs io ports)
(guile)
(gcrypt hash))
(define (bytevector->hex-string bv)
(let ((len (bytevector-length bv)))
(list->string
(let loop ((i 0) (result '()))
(if (= i len)
result
(let ((byte (bytevector-u8-ref bv i)))
(loop (+ i 1)
(append result
(list (integer->char
(if (< (quotient byte 16) 10)
(+ (quotient byte 16) (char->integer #\0))
(+ (- (quotient byte 16) 10) (char->integer #\a))))
(integer->char
(if (< (modulo byte 16) 10)
(+ (modulo byte 16) (char->integer #\0))
(+ (- (modulo byte 16) 10) (char->integer #\a)
)
)
)
)
)
)
)
)
)
)
)
)
;(define thehash (bytevector->hex-string (sha256 (cadr (call-with-values (lambda () (http-get "https://download.qemu.org/qemu-9.2.0.tar.xz" #:decode-body? #f)) list)))))
(define-module (bsprak)
#:use-module (gnu packages)
#:use-module (gnu packages attr)
#:use-module (gnu packages admin)
#:use-module (gnu packages audio)
#:use-module (gnu packages base)
#:use-module (gnu packages bison)
#:use-module (gnu packages bootloaders)
#:use-module (gnu packages compression)
#:use-module (gnu packages flex)
#:use-module (gnu packages gcc)
#:use-module (gnu packages gettext)
#:use-module (gnu packages gl)
#:use-module (gnu packages glib)
#:use-module (gnu packages gtk)
#:use-module (gnu packages image)
#:use-module (gnu packages libusb)
#:use-module (gnu packages linux)
#:use-module (gnu packages ncurses)
#:use-module (gnu packages ninja)
#:use-module (gnu packages perl)
#:use-module (gnu packages pkg-config)
#:use-module (gnu packages pulseaudio)
#:use-module (gnu packages python)
#:use-module (gnu packages python-build)
#:use-module (gnu packages python-xyz)
#:use-module (gnu packages sdl)
#:use-module (gnu packages sphinx)
#:use-module (gnu packages spice)
#:use-module (gnu packages texinfo)
#:use-module (gnu packages virtualization)
#:use-module (gnu packages vim)
#:use-module (gnu packages wget)
#:use-module (gnu packages build-tools)
#:use-module (gnu packages xdisorg)
#:use-module (gnu packages networking)
#:use-module (gnu packages firmware)
#:use-module (gnu packages containers)
#:use-module (gnu packages dbm)
#:use-module (gnu packages base)
#:use-module ((guix licenses) #:prefix license:)
#:use-module (guix build-system gnu)
#:use-module (guix build-system meson)
#:use-module (guix build-system cmake)
#:use-module (guix download)
#:use-module (guix packages)
#:use-module (guix gexp)
#:use-module (guix git-download)
#:export (qemu-bsprak
cppcheck-bsprak)
)
(define-public qemu-bsprak
(package
(name "qemu")
(version "9.2.0")
(source (origin
(method url-fetch)
(uri (string-append "https://download.qemu.org/qemu-" version ".tar.xz"))
(sha256
(base32
"156qkc44vd1gcmy6i9i1r7r5mvpcrwmwks5v83837xg1cnyg0ngq"))
(patches
(list (local-file "./qemu-led.patch")
(local-file "./qemu-txff.patch")
(local-file "./qemu-unaligned.patch")))
))
(build-system gnu-build-system)
(arguments
`(#:tests? #f ; Add this line to skip tests
#:configure-flags
(list (string-append "--prefix=" (assoc-ref %outputs "out")))
#:phases
(modify-phases %standard-phases
(add-before 'configure 'set-cc
(lambda _
(setenv "CC" "gcc") ;; actually works
#t))
;; Override the default configure phase to avoid unwanted flags
(replace 'configure
(lambda* (#:key outputs #:allow-other-keys)
(let ((out (assoc-ref outputs "out")))
(invoke "./configure"
(string-append "--prefix=" out))))))))
(native-inputs
(list pkg-config
python-wrapper
python-sphinx
python-sphinx-rtd-theme
python-tomli
bison
flex
perl
meson
ninja
vim
wget
tar))
(inputs
(list alsa-lib
attr
dtc
gdbm
gettext-minimal
glib
gtk+
libaio
libcacard
libcap-ng
libdrm
libepoxy
libjpeg-turbo
libpng
libseccomp
libslirp
liburing
libusb
mesa
ncurses
openbios-qemu-ppc
opensbi-qemu
pixman
pulseaudio
sdl2
seabios-qemu
spice
texinfo
usbredir
util-linux
vde2
virglrenderer
zlib
zstd
ipxe-qemu))
(home-page "https://www.qemu.org")
(synopsis "Machine emulator and virtualizer")
(description "QEMU is a generic machine emulator and virtualizer.
When used as a machine emulator, QEMU can run operating systems and programs made
for one machine on a different machine. When used as a virtualizer, QEMU achieves
near native performances by executing the guest code directly on the host CPU.")
(license license:gpl2+)))
(define-public cppcheck-bsprak
(package
(name "cppcheck")
(version "2.14.2")
(source (origin
(method git-fetch)
(uri (git-reference
(url "https://github.com/danmar/cppcheck")
(commit version)))
(file-name (git-file-name name version))
(sha256
(base32 "1xfxcg00rxjrb9m2k78yd3jjlldkciv67fsbmjb6n3l43hgfxb9k"))
(patches (search-patches "cppcheck-disable-char-signedness-test.patch"))))
(build-system cmake-build-system)
(arguments
'(#:configure-flags '("-DBUILD_TESTS=ON")))
(home-page "https://cppcheck.sourceforge.io")
(synopsis "Static C/C++ code analyzer")
(description "Cppcheck is a static code analyzer for C and C++. Unlike
C/C++ compilers and many other analysis tools it does not detect syntax errors
in the code. Cppcheck primarily detects the types of bugs that the compilers
normally do not detect. The goal is to detect only real errors in the code
(i.e. have zero false positives).")
(license license:gpl3+)))
From 9c6e71079b46ad5433165feaa2001450f2017b56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Przemys=C5=82aw=20Buczkowski?= <[email protected]>
Date: Mon, 16 Aug 2021 13:16:21 +0100
Subject: GCC: Patch for Apple Silicon compatibility
This patch fixes a linker error occuring when compiling
the cross-compiler on macOS and ARM64 architecture.
Adapted from:
https://github.com/richfelker/musl-cross-make/issues/116#issuecomment-823612404
Change-Id: Ia3ee98a163bbb62689f42e2da83a5ef36beb0913
Reviewed-on: https://review.haiku-os.org/c/buildtools/+/4329
Reviewed-by: John Scipione <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
---
gcc/gcc/config/aarch64/aarch64.h | 2 +-
gcc/gcc/config/host-darwin.c | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/gcc/gcc/config/aarch64/aarch64.h b/gcc/gcc/config/aarch64/aarch64.h
index 976f9afae5..736b83ff0b 100644
--- a/gcc/gcc/config/aarch64/aarch64.h
+++ b/gcc/gcc/config/aarch64/aarch64.h
@@ -1186,6 +1186,6 @@ extern const char *aarch64_rewrite_mcpu (int argc, const char **argv);
/* Extra specs when building a native AArch64-hosted compiler.
Option rewriting rules based on host system. */
-#if defined(__aarch64__)
+#if defined(__aarch64__) && ! defined(__APPLE__)
extern const char *host_detect_local_cpu (int argc, const char **argv);
#define HAVE_LOCAL_CPU_DETECT
# define EXTRA_SPEC_FUNCTIONS \
diff --git a/gcc/gcc/config/host-darwin.c b/gcc/gcc/config/host-darwin.c
index 8f700eec85..b737abc006 100644
--- a/gcc/gcc/config/host-darwin.cc
+++ b/gcc/gcc/config/host-darwin.cc
@@ -24,6 +24,8 @@
#include "diagnostic-core.h"
#include "config/host-darwin.h"
#include <errno.h>
+#include "hosthooks.h"
+#include "hosthooks-def.h"
/* For Darwin (macOS only) platforms, without ASLR (PIE) enabled on the
binaries, the following VM addresses are expected to be available.
@@ -181,3 +181,5 @@ darwin_gt_pch_use_address (void *addr, size_t sz, int fd, size_t off)
return 1;
}
+
+const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;
--
cgit v1.2.3
(use-modules (guix packages)
(gnu packages)
(gnu packages embedded))
;; Load the local module from bsprak.scm
(add-to-load-path (dirname (current-filename)))
(let ((bsprak (resolve-module '(bsprak))))
(packages->manifest
(append (list (module-ref bsprak 'qemu-bsprak)
(specification->package "make")
(specification->package "gcc-toolchain")
(specification->package "vim")
(specification->package "bash")
(specification->package "clang")
(specification->package "gdb-multiarch")
;; Evaluate the variable to get the package object
;guix shell -e "((@ (gnu packages embedded) make-gcc-arm-none-eabi-12.3.rel1))"
((@ (gnu packages embedded) make-gcc-arm-none-eabi-12.3.rel1)))
%base-packages)))
--- a/hw/gpio/bcm2835_gpio.c 2020-10-27 12:29:10.140056483 +0100
+++ /tmp/bcm2835_gpio.c 2020-10-27 12:26:10.618863244 +0100
@@ -120,7 +120,28 @@
for (i = 0; i < count; i++) {
if ((changes & cur) && (gpfsel_is_out(s, start + i))) {
qemu_set_irq(s->out[start + i], 1);
- }
+ if (start == 0){
+ switch (i) {
+ case 4:
+ printf("erste grüne LED: an\n");
+ break;
+ case 5:
+ printf("erste gelbe LED: an\n");
+ break;
+ case 6:
+ printf("einzige rote LED: an\n");
+ break;
+ case 7:
+ printf("zweite gelbe LED: an\n");
+ break;
+ case 8:
+ printf("zweite grüne LED: an\n");
+ break;
+ default:
+ break;
+ }
+ }
+ }
cur <<= 1;
}
@@ -137,6 +158,27 @@
for (i = 0; i < count; i++) {
if ((changes & cur) && (gpfsel_is_out(s, start + i))) {
qemu_set_irq(s->out[start + i], 0);
+ if (start == 0){
+ switch (i) {
+ case 4:
+ printf("erste grüne LED: aus\n");
+ break;
+ case 5:
+ printf("erste gelbe LED: aus\n");
+ break;
+ case 6:
+ printf("einzige rote LED: aus\n");
+ break;
+ case 7:
+ printf("zweite gelbe LED: aus\n");
+ break;
+ case 8:
+ printf("zweite grüne LED: aus\n");
+ break;
+ default:
+ break;
+ }
+ }
}
cur <<= 1;
}
diff -ur qemu-9.0.2-vanilla/hw/char/pl011.c qemu-9.0.2-txff/hw/char/pl011.c
--- qemu-9.0.2-vanilla/hw/char/pl011.c 2024-09-10 22:23:52.336598355 +0200
+++ qemu-9.0.2-txff/hw/char/pl011.c 2024-09-10 22:28:17.719993502 +0200
@@ -32,6 +32,30 @@
#include "qemu/log.h"
#include "qemu/module.h"
#include "trace.h"
+#include <stdbool.h>
+
+#define US_PER_S (1000000UL)
+#define TRANSFERED_BITS_PER_BYTE_UART (10UL)
+
+static unsigned int pl011_get_baudrate(const PL011State *s);
+
+static bool transmission_complete(PL011State *s){
+ struct timeval curr;
+ gettimeofday(&curr, NULL);
+
+ /* assume that at no baud rate we take longer than 1 second,
+ to avoid overflow in delta_us */
+ if((curr.tv_sec - s->last_transmission_start.tv_sec) >= 2){
+ return true;
+ }
+
+ unsigned long delta_us = (curr.tv_sec - s->last_transmission_start.tv_sec) * US_PER_S
+ + (curr.tv_usec - s->last_transmission_start.tv_usec);
+
+ unsigned long time_per_byte = (US_PER_S * TRANSFERED_BITS_PER_BYTE_UART) / pl011_get_baudrate(s);
+ return delta_us > time_per_byte;
+}
+
DeviceState *pl011_create(hwaddr addr, qemu_irq irq, Chardev *chr)
{
@@ -183,6 +207,11 @@
r = s->rsr;
break;
case 6: /* UARTFR */
+
+ if(transmission_complete(s)){
+ s->flags &= (~PL011_FLAG_TXFF);
+ }
+
r = s->flags;
break;
case 8: /* UARTILPR */
@@ -248,7 +277,7 @@
uint64_t clk;
if (s->ibrd == 0) {
- return 0;
+ return 921600;
}
clk = clock_get_hz(s->clk);
@@ -406,6 +444,11 @@
break;
case 14: /* UARTIMSC */
s->int_enabled = value;
+
+ if((value & INT_TX) != 0){
+ printf("TX interrupts are not supported!\n");
+ }
+
pl011_update(s);
break;
case 17: /* UARTICR */
diff -ur qemu-9.0.2-vanilla/include/hw/char/pl011.h qemu-9.0.2-txff/include/hw/char/pl011.h
--- qemu-9.0.2-vanilla/include/hw/char/pl011.h 2024-09-10 22:23:52.512595958 +0200
+++ qemu-9.0.2-txff/include/hw/char/pl011.h 2024-09-10 22:24:24.939154286 +0200
@@ -19,6 +19,9 @@
#include "chardev/char-fe.h"
#include "qom/object.h"
+#include <sys/time.h>
+#include <bits/types/struct_timeval.h>
+
#define TYPE_PL011 "pl011"
OBJECT_DECLARE_SIMPLE_TYPE(PL011State, PL011)
@@ -53,6 +56,7 @@
Clock *clk;
bool migrate_clk;
const unsigned char *id;
+ struct timeval last_transmission_start;
};
DeviceState *pl011_create(hwaddr addr, qemu_irq irq, Chardev *chr);
--- orig/configs/targets/arm-softmmu.mak 2021-10-13 17:06:06.852972541 +0000
+++ configs/targets/arm-softmmu.mak 2021-10-13 17:30:31.402907095 +0000
@@ -2,3 +2,4 @@
TARGET_SUPPORTS_MTTCG=y
TARGET_XML_FILES= gdb-xml/arm-core.xml gdb-xml/arm-vfp.xml gdb-xml/arm-vfp3.xml gdb-xml/arm-vfp-sysregs.xml gdb-xml/arm-neon.xml gdb-xml/arm-m-profile.xml gdb-xml/arm-m-profile-mve.xml
TARGET_NEED_FDT=y
+TARGET_ALIGNED_ONLY=y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment