diff --git a/index.html b/index.html index f4cec6e..5850538 100644 --- a/index.html +++ b/index.html @@ -1254,493 +1254,494 @@ pre{ white-space:pre }
  • 19.17. gem5 ARM platforms
  • 19.18. gem5 upstream images
  • 19.19. gem5 bootloaders
  • -
  • 19.20. gem5 internals +
  • 19.20. gem5 CommMonitor
  • +
  • 19.21. gem5 internals
  • -
  • 19.21. Gensim
  • -
  • 20. Buildroot +
  • 20. Gensim
  • +
  • 21. Buildroot
  • -
  • 21. Userland content +
  • 22. Userland content
  • -
  • 22. Userland assembly +
  • 23. Userland assembly
  • -
  • 23. x86 userland assembly +
  • 24. x86 userland assembly @@ -2291,7 +2292,7 @@ pre{ white-space:pre }

    If you don’t know which one to go for, start with QEMU Buildroot setup getting started.

    -

    Design goals of this project are documented at: Section 33.20.1, “Design goals”.

    +

    Design goals of this project are documented at: Section 34.20.1, “Design goals”.

    1.1. Should you waste your life with systems programming?

    @@ -2388,7 +2389,7 @@ pre{ white-space:pre }

    1.2.1. QEMU Buildroot setup getting started

    -

    This setup has been mostly tested on Ubuntu. For other host operating systems see: Section 33.1, “Supported hosts”. For greater stability, consider using the latest release instead of master: https://github.com/cirosantilli/linux-kernel-module-cheat/releases

    +

    This setup has been mostly tested on Ubuntu. For other host operating systems see: Section 34.1, “Supported hosts”. For greater stability, consider using the latest release instead of master: https://github.com/cirosantilli/linux-kernel-module-cheat/releases

    Reserve 12Gb of disk and run:

    @@ -2405,7 +2406,7 @@ cd linux-kernel-module-cheat

    You don’t need to clone recursively even though we have .git submodules: download-dependencies fetches just the submodules that you need for this build to save time.

    The initial build will take a while (30 minutes to 2 hours) to clone and build, see Benchmark builds for more details.

    @@ -2488,7 +2489,7 @@ hello2 cleanup
    -

    To avoid typing --arch aarch64 many times, you can set the default arch as explained at: Section 33.4, “Default command line arguments”

    +

    To avoid typing --arch aarch64 many times, you can set the default arch as explained at: Section 34.4, “Default command line arguments”

    I now urge you to read the following sections which contain widely applicable information:

    @@ -3329,7 +3330,7 @@ j = 0

    This repository has been tested inside clean Docker containers.

    -

    This is a good option if you are on a Linux host, but the native setup failed due to your weird host distribution, and you have better things to do with your life than to debug it. See also: Section 33.1, “Supported hosts”.

    +

    This is a good option if you are on a Linux host, but the native setup failed due to your weird host distribution, and you have better things to do with your life than to debug it. See also: Section 34.1, “Supported hosts”.

    For example, to do a QEMU Buildroot setup inside Docker, run:

    @@ -3517,7 +3518,7 @@ j = 0
    -

    TODO: the carriage returns are a bit different than in QEMU, see: Section 27.6, “gem5 baremetal carriage return”.

    +

    TODO: the carriage returns are a bit different than in QEMU, see: Section 28.6, “gem5 baremetal carriage return”.

    Note that ./build-baremetal requires the --emulator gem5 option, and generates separate executable images for both, as can be seen from:

    @@ -4339,10 +4340,10 @@ echo "$(./getvar --arch aarch64 --baremetal userland/c/hello.c --emulator gem5 -

    But just stick to newer and better VExpress_GEM5_V1 unless you have a good reason to use RealViewPBX.

    -

    When doing baremetal programming, it is likely that you will want to learn userland assembly first, see: Section 22, “Userland assembly”.

    +

    When doing baremetal programming, it is likely that you will want to learn userland assembly first, see: Section 23, “Userland assembly”.

    -

    For more information on baremetal, see the section: Section 27, “Baremetal”.

    +

    For more information on baremetal, see the section: Section 28, “Baremetal”.

    The following subjects are particularly important:

    @@ -4407,7 +4408,7 @@ xdg-open README.html
    -

    More information about our documentation internals can be found at: Section 33.5, “Documentation”

    +

    More information about our documentation internals can be found at: Section 34.5, “Documentation”

    @@ -5643,7 +5644,7 @@ Breakpoint 3 at 0xffffffff811615e3: fdget_pos. (9 locations)

    2.9. GDB step debug multicore userland

    -

    For a more minimal baremetal multicore setup, see: Section 27.10.3, “ARM baremetal multicore”.

    +

    For a more minimal baremetal multicore setup, see: Section 28.10.3, “ARM baremetal multicore”.

    We can set and get which cores the Linux kernel allows a program to run on with sched_getaffinity and sched_setaffinity:

    @@ -8023,7 +8024,7 @@ qw er
    -

    To stop at the very first instruction of a freestanding program, just use --no-continue. A good example of this is shown at: Section 22.5.1, “Freestanding programs”.

    +

    To stop at the very first instruction of a freestanding program, just use --no-continue. A good example of this is shown at: Section 23.5.1, “Freestanding programs”.

    @@ -8076,7 +8077,7 @@ qw er

    The gem5 tests require building statically with build id static, see also: Section 10.7, “gem5 syscall emulation mode”. TODO automate this better.

    -

    See: Section 33.16, “Test this repo” for more useful testing tips.

    +

    See: Section 34.16, “Test this repo” for more useful testing tips.

    @@ -8491,7 +8492,7 @@ qemu: uncaught target signal 6 (Aborted) - core dumped

    Support for dynamic linking was added in November 2019: https://stackoverflow.com/questions/50542222/how-to-run-a-dynamically-linked-executable-syscall-emulation-mode-se-py-in-gem5/50696098#50696098

    -

    Note that as shown at Section 29.2.2, “Benchmark emulators on userland executables”, the dynamic version runs 200x more instructions, which might have an impact on smaller simulations in detailed CPUs.

    +

    Note that as shown at Section 30.2.2, “Benchmark emulators on userland executables”, the dynamic version runs 200x more instructions, which might have an impact on smaller simulations in detailed CPUs.

    @@ -8928,7 +8929,7 @@ Program aborted at tick 0
    @@ -9835,7 +9836,7 @@ xeyes

    14.1. Enable networking

    -

    We disable networking by default because it starts an userland process, and we want to keep the number of userland processes to a minimum to make the system more understandable as explained at: Section 33.20.3, “Resource tradeoff guidelines”

    +

    We disable networking by default because it starts an userland process, and we want to keep the number of userland processes to a minimum to make the system more understandable as explained at: Section 34.20.3, “Resource tradeoff guidelines”

    To enable networking on Buildroot, simply run:

    @@ -10684,15 +10685,15 @@ git log | grep -E ' Linux [0-9]+\.' | head

    This also makes this repo the perfect setup to develop the Linux kernel.

    -

    In case something breaks while updating the Linux kernel, you can try to bisect it to understand the root cause, see: Section 33.17, “Bisection”.

    +

    In case something breaks while updating the Linux kernel, you can try to bisect it to understand the root cause, see: Section 34.17, “Bisection”.

    15.2.2.1. Update the Linux kernel LKMC procedure
    -

    First, use use the branching procedure described at: Section 33.18, “Update a forked submodule”

    +

    First, use use the branching procedure described at: Section 34.18, “Update a forked submodule”

    -

    Because the kernel is so central to this repository, almost all tests must be re-run, so basically just follow the full testing procedure described at: Section 33.16, “Test this repo”. The only tests that can be skipped are essentially the Baremetal tests.

    +

    Because the kernel is so central to this repository, almost all tests must be re-run, so basically just follow the full testing procedure described at: Section 34.16, “Test this repo”. The only tests that can be skipped are essentially the Baremetal tests.

    Before comitting, don’t forget to update:

    @@ -15240,7 +15241,7 @@ detected buffer overflow in strlen
    -

    SELinux requires glibc as mentioned at: Section 20.10, “libc choice”.

    +

    SELinux requires glibc as mentioned at: Section 21.10, “libc choice”.

    @@ -16371,7 +16372,7 @@ wget \
    -

    STRESS_NG is likely the best, but it requires glibc, see: Section 20.10, “libc choice”.

    +

    STRESS_NG is likely the best, but it requires glibc, see: Section 21.10, “libc choice”.

    Websites:

    @@ -17809,10 +17810,10 @@ run

    The build outputs are automatically stored in a different directories for optimized and debug builds, which prevents debug files from overwriting opt ones. Therefore, --gem5-build-id is not required.

    -

    The price to pay for debuggability is high however: a Linux kernel boot was about 3x slower in QEMU and 14 times slower in gem5 debug compared to opt, see benchmarks at: Section 29.2.1, “Benchmark Linux kernel boot”.

    +

    The price to pay for debuggability is high however: a Linux kernel boot was about 3x slower in QEMU and 14 times slower in gem5 debug compared to opt, see benchmarks at: Section 30.2.1, “Benchmark Linux kernel boot”.

    -

    Similar slowdowns can be observed at: Section 29.2.2, “Benchmark emulators on userland executables”.

    +

    Similar slowdowns can be observed at: Section 30.2.2, “Benchmark emulators on userland executables”.

    When in QEMU text mode, using --debug-vm makes Ctrl-C not get passed to the QEMU guest anymore: it is instead captured by GDB itself, so allow breaking. So e.g. you won’t be able to easily quit from a guest program like:

    @@ -18546,7 +18547,7 @@ extern SimpleFlag ExecEnable;

    25007500: time count in some unit. Note how the microops execute at further timestamps.

  • -

    system.cpu: distinguishes between CPUs when there are more than one. For example, running Section 27.10.3, “ARM baremetal multicore” with two cores produces system.cpu0 and system.cpu1

    +

    system.cpu: distinguishes between CPUs when there are more than one. For example, running Section 28.10.3, “ARM baremetal multicore” with two cores produces system.cpu0 and system.cpu1

  • T0: thread number. TODO: hyperthread? How to play with it?

    @@ -18839,7 +18840,7 @@ root

    runs are deterministic by default, unlike QEMU which has a special QEMU record and replay mode, that requires first playing the content once and then replaying

  • -

    gem5 ARM at least appears to implement more low level CPU functionality than QEMU, e.g. QEMU only added EL2 in 2018: https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up See also: Section 27.10.1, “ARM exception levels”

    +

    gem5 ARM at least appears to implement more low level CPU functionality than QEMU, e.g. QEMU only added EL2 in 2018: https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up See also: Section 28.10.1, “ARM exception levels”

  • gem5 offers more advanced logging, even for non micro architectural things which QEMU models in some way, e.g. QEMU trace memory accesses, because QEMU’s binary translation optimizations reduce visibility

    @@ -18852,7 +18853,7 @@ root
    -

    One simple example of its operation can be seen at: Section 19.20.4.2, “gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis”

    +

    One simple example of its operation can be seen at: Section 19.21.4.2, “gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis”

    But arguably interesting effects can only be observed when we have more than 1 CPUs as in gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs.

    @@ -22762,7 +22763,50 @@ cd ..
    -

    19.20. gem5 internals

    +

    19.20. gem5 CommMonitor

    +
    +

    You can place this SimObject in between two ports to get extra statistics about the packets that are going through.

    +
    +
    +

    It only works on timing CPUs, and does not seem to dump any memory values, only add extra statistics.

    +
    +
    +

    For example, the patch patches/manual/gem5-commmonitor-se.patch hack a CommMonitor between the CPU and the L1 cache on top of gem5 1c3662c9557c85f0d25490dc4fbde3f8ab0cb350:

    +
    +
    +
    +
    patch -d "$(./getvar gem5_source_dir)" -p 1 < patches/manual/gem5-commmonitor-se.patch
    +
    +
    +
    +

    which you can run with:

    +
    +
    +
    +
    ./run \
    +  --arch aarch64 \
    +  --emulator gem5 \
    +  --userland userland/arch/aarch64/freestanding/linux/hello.S \
    +  -- \
    +  --caches \
    +  --cpu-type TimingSimpleCPU \
    +;
    +
    +
    +
    +

    and now we have some new extra histogram statistics such as:

    +
    +
    +
    +
    system.cpu.dcache_mon.readBurstLengthHist::samples            1
    +
    +
    +
    +

    One neat thing about this is that it is agnostic to the memory object type, so you don’t have to recode those statistics for every new type of object that operates on memory packets.

    +
    +
    +
    +

    19.21. gem5 internals

    Internals under other sections:

    @@ -22780,7 +22824,7 @@ cd ..
    -

    19.20.1. gem5 Eclipse configuration

    +

    19.21.1. gem5 Eclipse configuration

    @@ -22842,7 +22886,7 @@ cd ..
    -

    19.20.2. gem5 Python C++ interaction

    +

    19.21.2. gem5 Python C++ interaction

    The interaction uses the Python C extension interface https://docs.python.org/2/extending/extending.html interface through the pybind11 helper library: https://github.com/pybind/pybind11

    @@ -23017,6 +23061,9 @@ static EmbeddedPyBind embed_obj("BadDevice", module_init, "BasicPioDevice");

    https://stackoverflow.com/questions/61910993/viewing-the-parameters-of-the-branch-predictor-in-gem5/61914449#61914449

  • +
  • +

    https://stackoverflow.com/questions/62969566/attributes-of-system-object-in-gem5/62970092#62970092

    +
  • @@ -23024,7 +23071,7 @@ static EmbeddedPyBind embed_obj("BadDevice", module_init, "BasicPioDevice");
    -

    19.20.3. gem5 entry point

    +

    19.21.3. gem5 entry point

    The main is at: src/sim/main.cc. It calls:

    @@ -23112,7 +23159,7 @@ exec filecode in scope

    Tested at gem5 b4879ae5b0b6644e6836b0881e4da05c64a6550d.

    -
    19.20.3.1. gem5 m5.objects module
    +
    19.21.3.1. gem5 m5.objects module

    All SimObjects seem to be automatically added to the m5.objects namespace, and this is done in a very convoluted way, let’s try to understand a bit:

    @@ -23277,7 +23324,7 @@ for source in PySource.all:
    -

    19.20.4. gem5 event queue

    +

    19.21.4. gem5 event queue

    gem5 is an event based simulator, and as such the event queue is of of the crucial elements in the system.

    @@ -23383,7 +23430,7 @@ b EventFunctionWrapper::process

    Then, once we had that, the most perfect thing ever would be to make the full event graph containing which events schedule which events!

    -
    19.20.4.1. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis
    +
    19.21.4.1. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis

    Let’s now analyze every single event on a minimal gem5 syscall emulation mode in the simplest CPU that we have:

    @@ -23519,7 +23566,7 @@ AtomicSimpleCPU::tick() at atomic.cc:757 0x55555907834c

    Tested in gem5 12c917de54145d2d50260035ba7fa614e25317a3.

    -
    19.20.4.1.1. AtomicSimpleCPU initial events
    +
    19.21.4.1.1. AtomicSimpleCPU initial events

    Let’s have a closer look at the initial magically scheduled events of the simulation.

    @@ -23738,7 +23785,7 @@ simulate() at simulate.cc:104 0x555559476d6f
    -
    19.20.4.1.2. AtomicSimpleCPU tick reschedule timing
    +
    19.21.4.1.2. AtomicSimpleCPU tick reschedule timing

    Inside AtomicSimpleCPU::tick() we saw previously that the reschedule happens at:

    @@ -23778,7 +23825,7 @@ clock=500
    -
    19.20.4.1.3. AtomicSimpleCPU memory access
    +
    19.21.4.1.3. AtomicSimpleCPU memory access

    It will be interesting to see how AtomicSimpleCPU makes memory access on GDB and to compare that with TimingSimpleCPU.

    @@ -23832,7 +23879,7 @@ clock=500
    -
    19.20.4.1.4. gem5 se.py page translation
    +
    19.21.4.1.4. gem5 se.py page translation

    Happens on EmulationPageTable, and seems to happen atomically without making any extra memory requests.

    @@ -23903,7 +23950,7 @@ Exiting @ tick 3500 because exiting with last active thread context
    -
    19.20.4.2. gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis
    +
    19.21.4.2. gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis

    Now, let’s move on to TimingSimpleCPU, which is just like AtomicSimpleCPU internally, but now the memory requests don’t actually finish immediately: gem5 CPU types!

    @@ -24184,7 +24231,7 @@ info: Entering event queue @ 0. Starting simulation...
    -
    19.20.4.2.1. TimingSimpleCPU analysis #0
    +
    19.21.4.2.1. TimingSimpleCPU analysis #0

    Schedules TimingSimpleCPU::fetch through:

    @@ -24229,7 +24276,7 @@ ArmLinuxProcess64::initState
    -
    19.20.4.2.2. TimingSimpleCPU analysis #1
    +
    19.21.4.2.2. TimingSimpleCPU analysis #1

    Backtrace:

    @@ -24360,7 +24407,7 @@ DRAMCtrl::Rank::startup(Tick ref_tick)
    -
    19.20.4.2.3. TimingSimpleCPU analysis #2
    +
    19.21.4.2.3. TimingSimpleCPU analysis #2

    This is just the startup of the second rank, see: TimingSimpleCPU analysis #1.

    @@ -24393,13 +24440,13 @@ DRAMCtrl::Rank::startup(Tick ref_tick)
    -
    19.20.4.2.4. TimingSimpleCPU analysis #3 and #4
    +
    19.21.4.2.4. TimingSimpleCPU analysis #3 and #4

    From the timing we know what that one is: the end of time exit event, like for AtomicSimpleCPU.

    -
    19.20.4.2.5. TimingSimpleCPU analysis #5
    +
    19.21.4.2.5. TimingSimpleCPU analysis #5

    Executes TimingSimpleCPU::fetch().

    @@ -24507,7 +24554,7 @@ DRAMCtrl::Rank::startup(Tick ref_tick)
    -
    19.20.4.2.6. TimingSimpleCPU analysis #6
    +
    19.21.4.2.6. TimingSimpleCPU analysis #6

    Schedules DRAMCtrl::processNextReqEvent through:

    @@ -24644,7 +24691,7 @@ TimingSimpleCPU::fetch
    -
    19.20.4.2.7. TimingSimpleCPU analysis #7
    +
    19.21.4.2.7. TimingSimpleCPU analysis #7

    Schedules BaseXBar::Layer::releaseLayer through:

    @@ -24670,13 +24717,13 @@ TimingSimpleCPU::fetch
    -
    19.20.4.2.8. TimingSimpleCPU analysis #8
    +
    19.21.4.2.8. TimingSimpleCPU analysis #8

    Executes DRAMCtrl::processNextReqEvent.

    -
    19.20.4.2.9. TimingSimpleCPU analysis #9
    +
    19.21.4.2.9. TimingSimpleCPU analysis #9

    Schedules DRAMCtrl::Rank::processActivateEvent through:

    @@ -24690,7 +24737,7 @@ DRAMCtrl::processNextReqEvent
    -
    19.20.4.2.10. TimingSimpleCPU analysis #10
    +
    19.21.4.2.10. TimingSimpleCPU analysis #10

    Schedules DRAMCtrl::processRespondEvent through:

    @@ -24702,7 +24749,7 @@ DRAMCtrl::processNextReqEvent
    -
    19.20.4.2.11. TimingSimpleCPU analysis #11
    +
    19.21.4.2.11. TimingSimpleCPU analysis #11

    Schedules DRAMCtrl::processNextReqEvent through:

    @@ -24714,7 +24761,7 @@ DRAMCtrl::processNextReqEvent
    -
    19.20.4.2.12. TimingSimpleCPU analysis #12
    +
    19.21.4.2.12. TimingSimpleCPU analysis #12

    Executes DRAMCtrl::Rank::processActivateEvent.

    @@ -24723,7 +24770,7 @@ DRAMCtrl::processNextReqEvent
    -
    19.20.4.2.13. TimingSimpleCPU analysis #13
    +
    19.21.4.2.13. TimingSimpleCPU analysis #13

    Schedules DRAMCtrl::Rank::processPowerEvent through:

    @@ -24736,7 +24783,7 @@ DRAMCtrl::Rank::processActivateEvent
    -
    19.20.4.2.14. TimingSimpleCPU analysis #14
    +
    19.21.4.2.14. TimingSimpleCPU analysis #14

    Executes DRAMCtrl::Rank::processPowerEvent.

    @@ -24745,25 +24792,25 @@ DRAMCtrl::Rank::processActivateEvent
    -
    19.20.4.2.15. TimingSimpleCPU analysis #15
    +
    19.21.4.2.15. TimingSimpleCPU analysis #15

    Executes BaseXBar::Layer<SrcType, DstType>::releaseLayer.

    -
    19.20.4.2.16. TimingSimpleCPU analysis #16
    +
    19.21.4.2.16. TimingSimpleCPU analysis #16

    Executes DRAMCtrl::processNextReqEvent().

    -
    19.20.4.2.17. TimingSimpleCPU analysis #17
    +
    19.21.4.2.17. TimingSimpleCPU analysis #17

    Executes DRAMCtrl::processRespondEvent().

    -
    19.20.4.2.18. TimingSimpleCPU analysis #18
    +
    19.21.4.2.18. TimingSimpleCPU analysis #18

    Schedules PacketQueue::processSendEvent() through:

    @@ -24778,13 +24825,13 @@ DRAMCtrl::processRespondEvent
    -
    19.20.4.2.19. TimingSimpleCPU analysis #19
    +
    19.21.4.2.19. TimingSimpleCPU analysis #19

    Executes PacketQueue::processSendEvent().

    -
    19.20.4.2.20. TimingSimpleCPU analysis #20
    +
    19.21.4.2.20. TimingSimpleCPU analysis #20

    Schedules PacketQueue::processSendEvent through:

    @@ -24808,7 +24855,7 @@ PacketQueue::processSendEvent
    -
    19.20.4.2.21. TimingSimpleCPU analysis #21
    +
    19.21.4.2.21. TimingSimpleCPU analysis #21

    Schedules BaseXBar::Layer<SrcType, DstType>::releaseLayer through:

    @@ -24828,19 +24875,19 @@ PacketQueue::processSendEvent
    -
    19.20.4.2.22. TimingSimpleCPU analysis #22
    +
    19.21.4.2.22. TimingSimpleCPU analysis #22

    Executes BaseXBar::Layer<SrcType, DstType>::releaseLayer.

    -
    19.20.4.2.23. TimingSimpleCPU analysis #23
    +
    19.21.4.2.23. TimingSimpleCPU analysis #23

    Executes PacketQueue::processSendEvent.

    -
    19.20.4.2.24. TimingSimpleCPU analysis #24
    +
    19.21.4.2.24. TimingSimpleCPU analysis #24

    Schedules TimingSimpleCPU::IcachePort::ITickEvent::process() through:

    @@ -24858,7 +24905,7 @@ PacketQueue::processSendEvent
    -
    19.20.4.2.25. TimingSimpleCPU analysis #25
    +
    19.21.4.2.25. TimingSimpleCPU analysis #25

    Executes TimingSimpleCPU::IcachePort::ITickEvent::process().

    @@ -24878,7 +24925,7 @@ PacketQueue::processSendEvent
    -
    19.20.4.2.26. TimingSimpleCPU analysis #26
    +
    19.21.4.2.26. TimingSimpleCPU analysis #26

    Schedules DRAMCtrl::processNextReqEvent through:

    @@ -24907,7 +24954,7 @@ TimingSimpleCPU::IcachePort::ITickEvent::process
    -
    19.20.4.2.27. TimingSimpleCPU analysis #27
    +
    19.21.4.2.27. TimingSimpleCPU analysis #27

    Schedules BaseXBar::Layer<SrcType, DstType>::releaseLayer through:

    @@ -24933,19 +24980,19 @@ TimingSimpleCPU::IcachePort::ITickEvent::process
    -
    19.20.4.2.28. TimingSimpleCPU analysis #28
    +
    19.21.4.2.28. TimingSimpleCPU analysis #28

    Execute DRAMCtrl::processNextReqEvent.

    -
    19.20.4.2.29. TimingSimpleCPU analysis #29
    +
    19.21.4.2.29. TimingSimpleCPU analysis #29

    Schedule DRAMCtrl::processRespondEvent().

    -
    19.20.4.2.30. TimingSimpleCPU analysis: LDR stall
    +
    19.21.4.2.30. TimingSimpleCPU analysis: LDR stall

    One important thing we want to check now, is how the memory reads are going to make the processor stall in the middle of an instruction.

    @@ -25063,7 +25110,7 @@ TimingSimpleCPU::IcachePort::ITickEvent::process
    -
    19.20.4.3. gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis with caches
    +
    19.21.4.3. gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis with caches

    Let’s just add --caches to gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis to see if things go any faster, and add Cache to --trace as in:

    @@ -25358,7 +25405,7 @@ type=SetAssociative
    -
    19.20.4.3.1. What is the coherency protocol implemented by the classic cache system in gem5?
    +
    19.21.4.3.1. What is the coherency protocol implemented by the classic cache system in gem5?

    MOESI cache coherence protocol: https://github.com/gem5/gem5/blob/9fc9c67b4242c03f165951775be5cd0812f2a705/src/mem/cache/cache_blk.hh#L352

    @@ -25366,12 +25413,12 @@ type=SetAssociative

    The actual representation is done via separate state bits: https://github.com/gem5/gem5/blob/9fc9c67b4242c03f165951775be5cd0812f2a705/src/mem/cache/cache_blk.hh#L66 and MOESI appears explicitly only on the pretty printing.

    -

    This pretty printing appears for example in the --trace Cache lines as shown at gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis with caches and with a few more transitions visible at Section 19.20.4.4, “gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs”.

    +

    This pretty printing appears for example in the --trace Cache lines as shown at gem5 event queue TimingSimpleCPU syscall emulation freestanding example analysis with caches and with a few more transitions visible at Section 19.21.4.4, “gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs”.

    -
    19.20.4.4. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs
    +
    19.21.4.4. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs

    It would be amazing to analyze a simple example with interconnect packets possibly invalidating caches of other CPUs.

    @@ -25581,7 +25628,7 @@ type=SetAssociative

    and so on, they just keep fighting over that address and changing one another’s state.

    -
    19.20.4.4.1. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs and Ruby
    +
    19.21.4.4.1. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs and Ruby

    Now let’s do the exact same we did for gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs, but with Ruby rather than the classic system.

    @@ -25624,7 +25671,7 @@ non-atomic 19
    -
    19.20.4.5. gem5 event queue MinorCPU syscall emulation freestanding example analysis
    +
    19.21.4.5. gem5 event queue MinorCPU syscall emulation freestanding example analysis

    The events for the Atomic CPU were pretty simple: basically just ticks.

    @@ -25757,11 +25804,11 @@ non-atomic 19

    so now we are ready to run the third and fourth instructions of the program:

    -
    -

    ,…​ - ldr x2, =len - mov x8, 64 -,…​

    +
    +
    +
        ldr x2, =len
    +    mov x8, 64
    +

    The LDR goes all the way down to FU 6 which is the memory one:

    @@ -25794,14 +25841,14 @@ non-atomic 19
    -
    19.20.4.5.1. gem5 event queue MinorCPU syscall emulation freestanding example analysis: hazard
    +
    19.21.4.5.1. gem5 event queue MinorCPU syscall emulation freestanding example analysis: hazard
    -
    19.20.4.6. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis
    +
    19.21.4.6. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis

    Like gem5 event queue MinorCPU syscall emulation freestanding example analysis but even more complex since for the gem5 DerivO3CPU!

    @@ -25829,7 +25876,7 @@ non-atomic 19

    This section and children are tested at LKMC 144a552cf926ea630ef9eadbb22b79fe2468c456.

    -
    19.20.4.6.1. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazardless
    +
    19.21.4.6.1. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazardless

    Let’s have a look at the arguably simplest example userland/arch/aarch64/freestanding/linux/hazardless.S.

    @@ -26068,7 +26115,7 @@ non-atomic 19
    -
    19.20.4.6.2. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazard
    +
    19.21.4.6.2. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazard

    Now let’s do the same as in gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazardless but with a hazard: userland/arch/aarch64/freestanding/linux/hazard.S.

    @@ -26112,7 +26159,7 @@ non-atomic 19
    -
    19.20.4.6.3. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazard4
    +
    19.21.4.6.3. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazard4

    Like gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazard but a hazard of depth 4: userland/arch/aarch64/freestanding/linux/hazard.S.

    @@ -26153,7 +26200,7 @@ non-atomic 19
    -
    19.20.4.6.4. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall
    +
    19.21.4.6.4. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall

    Like gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: hazard but now with an LDR stall: userland/arch/aarch64/freestanding/linux/stall.S.

    @@ -26204,7 +26251,7 @@ non-atomic 19
    -
    19.20.4.6.5. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall-gain
    +
    19.21.4.6.5. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall-gain

    Like gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall but now with an LDR stall: userland/arch/aarch64/freestanding/linux/stall-gain.S.

    @@ -26291,7 +26338,7 @@ non-atomic 19
    -
    19.20.4.6.6. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall-hazard4
    +
    19.21.4.6.6. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall-hazard4

    Like gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: stall-gain but now with some dependencies after the LDR: userland/arch/aarch64/freestanding/linux/stall-hazard4.S.

    @@ -26358,7 +26405,7 @@ non-atomic 19
    -
    19.20.4.6.7. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: speculative
    +
    19.21.4.6.7. gem5 event queue DerivO3CPU syscall emulation freestanding example analysis: speculative

    Now let’s try to see some Speculative execution in action with userland/arch/aarch64/freestanding/linux/speculative.S.

    @@ -26547,7 +26594,7 @@ wbActual:0
    -

    19.20.5. gem5 instruction definitions

    +

    19.21.5. gem5 instruction definitions

    This is one of the parts of gem5 that rely on semi-useless code generation inside the .isa sublanguage.

    @@ -26590,7 +26637,7 @@ wbActual:0
    -

    We also notice that the key argument passed to those instructions is of type ExecContext, which is discussed further at: Section 19.20.7.3, “gem5 ExecContext.

    +

    We also notice that the key argument passed to those instructions is of type ExecContext, which is discussed further at: Section 19.21.7.3, “gem5 ExecContext.

    The file is an include so that compilation can be split up into chunks by the autogenerated includers

    @@ -26795,7 +26842,7 @@ namespace ArmISAInst {

    Tested in gem5 b1623cb2087873f64197e503ab8894b5e4d4c7b4.

    -
    19.20.5.1. gem5 execute vs initiateAcc vs completeAcc
    +
    19.21.5.1. gem5 execute vs initiateAcc vs completeAcc

    These are the key methods defined in instruction definitions, so lets see when each one gets called and what they do more or less.

    @@ -26849,7 +26896,7 @@ namespace ArmISAInst {

    This can be seen concretely in GDB from the analysis done at: TimingSimpleCPU analysis: LDR stall and for more memory details see gem5 functional vs atomic vs timing memory requests.

    -
    19.20.5.1.1. gem5 completeAcc
    +
    19.21.5.1.1. gem5 completeAcc

    completeAcc is boring on most simple store memory instructions, e.g. a simple STR:

    @@ -26902,7 +26949,7 @@ namespace ArmISAInst {
    -
    19.20.5.2. gem5 microops
    +
    19.21.5.2. gem5 microops

    Some gem5 instructions break down into multiple microops.

    @@ -26963,7 +27010,7 @@ namespace ArmISAInst {
    -

    19.20.6. gem5 port system

    +

    19.21.6. gem5 port system

    The gem5 memory system is connected in a very flexible way through the port system.

    @@ -26971,7 +27018,7 @@ namespace ArmISAInst {

    This system exists to allow seamlessly connecting any combination of CPU, caches, interconnects, DRAM and peripherals.

    -
    19.20.6.1. gem5 functional vs atomic vs timing memory requests
    +
    19.21.6.1. gem5 functional vs atomic vs timing memory requests

    gem5 memory requests can be classified in the following broad categories:

    @@ -27181,7 +27228,7 @@ TimingSimpleCPU::finishTranslation(WholeTranslationState *state)

    Tested in gem5 b1623cb2087873f64197e503ab8894b5e4d4c7b4.

    -
    19.20.6.1.1. gem5 functional requests
    +
    19.21.6.1.1. gem5 functional requests

    As seen at gem5 functional vs atomic vs timing memory requests, functional requests are not used in common simulation, since the core must always go through caches.

    @@ -27228,7 +27275,7 @@ TimingSimpleCPU::finishTranslation(WholeTranslationState *state)
    -

    19.20.7. gem5 ThreadContext vs ThreadState vs ExecContext vs Process

    +

    19.21.7. gem5 ThreadContext vs ThreadState vs ExecContext vs Process

    These classes get used everywhere, and they have a somewhat convoluted relation with one another, so let’s figure it out this mess.

    @@ -27239,7 +27286,7 @@ TimingSimpleCPU::finishTranslation(WholeTranslationState *state)

    This section and all children tested at gem5 b1623cb2087873f64197e503ab8894b5e4d4c7b4.

    -
    19.20.7.1. gem5 ThreadContext
    +
    19.21.7.1. gem5 ThreadContext

    As we delve into more details below, we will reach the following conclusion: a ThreadContext represents on thread of a CPU with multiple Hardware threads.

    @@ -27289,7 +27336,7 @@ typedef SimpleThread MinorThread;

    Essentially all methods of the base ThreadContext are pure virtual.

    -
    19.20.7.1.1. gem5 SimpleThread
    +
    19.21.7.1.1. gem5 SimpleThread

    SimpleThread storage defined on BaseSimpleCPU for simple CPUs like AtomicSimpleCPU:

    @@ -27384,7 +27431,7 @@ typedef SimpleThread MinorThread;
    -
    19.20.7.1.2. gem5 O3ThreadContext
    +
    19.21.7.1.2. gem5 O3ThreadContext

    Instantiation happens in the FullO3CPU constructor:

    @@ -27485,7 +27532,7 @@ FullO3CPU<Impl>::readArchIntReg(int reg_idx, ThreadID tid)
    -
    19.20.7.2. gem5 ThreadState
    +
    19.21.7.2. gem5 ThreadState

    Owned one per ThreadContext.

    @@ -27531,7 +27578,7 @@ class O3ThreadContext : public ThreadContext
    -
    19.20.7.3. gem5 ExecContext
    +
    19.21.7.3. gem5 ExecContext

    ExecContext gets used in gem5 instruction definitions, e.g.:

    @@ -27691,7 +27738,7 @@ class O3ThreadContext : public ThreadContext

    This makes sense, since each ThreadContext represents one CPU register set, and therefore needs a separate ExecContext which allows instruction implementations to access those registers.

    -
    19.20.7.3.1. gem5 ExecContext::readIntRegOperand register resolution
    +
    19.21.7.3.1. gem5 ExecContext::readIntRegOperand register resolution

    Let’s have a look at how ExecContext::readIntRegOperand actually matches registers to decoded registers IDs, since it is not obvious.

    @@ -27730,7 +27777,7 @@ class O3ThreadContext : public ThreadContext

    First, we guess that they must be related to the reading of x1 and x2, which are the inputs of the addition.

    -

    Next, we also guess that the 0 read must correspond to x2, since it later gets potentially shifted as mentioned at Section 24.4.4.1, “ARM shift suffixes”.

    +

    Next, we also guess that the 0 read must correspond to x2, since it later gets potentially shifted as mentioned at Section 25.4.4.1, “ARM shift suffixes”.

    Let’s also have a look at the decoder code that builds the instruction instance in build/ARM/arch/arm/generated/decoder-ns.cc.inc:

    @@ -27964,7 +28011,7 @@ flattenIntIndex(int reg) const
    -
    19.20.7.4. gem5 Process
    +
    19.21.7.4. gem5 Process

    The Process class is used only for gem5 syscall emulation mode, and it represents a process like a Linux userland process, in addition to any further gem5 specific data needed to represent the process.

    @@ -28052,12 +28099,12 @@ readFunc(SyscallDesc *desc, ThreadContext *tc,
    -

    19.20.8. gem5 functional units

    +

    19.21.8. gem5 functional units

    Each instruction is marked with a class, and each class can execute in a given functional unit.

    -
    19.20.8.1. gem5 MinorCPU default functional units
    +
    19.21.8.1. gem5 MinorCPU default functional units

    Which units are available is visible for example on the gem5 config.ini of a gem5 MinorCPU run. Functional units are not present in simple CPUs like gem5 TimingSimpleCPU.

    @@ -28216,7 +28263,7 @@ opClass=IntAlu
    -
    19.20.8.2. gem5 DerivO3CPU default functional units
    +
    19.21.8.2. gem5 DerivO3CPU default functional units

    On gem5 3ca404da175a66e0b958165ad75eb5f54cb5e772, after running:

    @@ -28314,7 +28361,7 @@ pipelined=false
    -

    19.20.9. gem5 code generation

    +

    19.21.9. gem5 code generation

    gem5 uses a ton of code generation, which makes the project horrendous:

    @@ -28359,7 +28406,7 @@ pipelined=false

    But it has been widely overused to insanity. It likely also exists partly because when the project started in 2003 C++ compilers weren’t that good, so you couldn’t rely on features like templates that much.

    -
    19.20.9.1. gem5 THE_ISA
    +
    19.21.9.1. gem5 THE_ISA

    Generated code at: build/<ISA>/config/the_isa.hh which e.g. for ARM contains:

    @@ -28405,9 +28452,9 @@ enum class Arch {
    -

    19.20.10. gem5 build system

    +

    19.21.10. gem5 build system

    -
    19.20.10.1. M5_OVERRIDE_PY_SOURCE
    +
    19.21.10.1. M5_OVERRIDE_PY_SOURCE
    @@ -28422,7 +28469,7 @@ enum class Arch {
    -
    19.20.10.2. gem5 build broken on recent compiler version
    +
    19.21.10.2. gem5 build broken on recent compiler version

    gem5 moves a bit slowly, and if your host compiler is very new, the gem5 build might be broken for it, e.g. this was the case for Ubuntu 19.10 with GCC 9 and gem5 62d75e7105fe172eb906d4f80f360ff8591d4178 from Dec 2019.

    @@ -28447,7 +28494,7 @@ enum class Arch {
    -
    19.20.10.3. gem5 polymorphic ISA includes
    +
    19.21.10.3. gem5 polymorphic ISA includes

    E.g. src/cpu/decode_cache.hh includes:

    @@ -28526,7 +28573,7 @@ build/ARM/config/the_isa.hh
    -
    19.20.10.4. Why are all C++ symlinked into the gem5 build dir?
    +
    19.21.10.4. Why are all C++ symlinked into the gem5 build dir?

    Upstream request: https://gem5.atlassian.net/browse/GEM5-469

    @@ -28564,8 +28611,11 @@ build/ARM/config/the_isa.hh
    -
    -

    19.21. Gensim

    +
    + +
    +

    20. Gensim

    +
    @@ -28667,12 +28717,11 @@ gensim/models/armv8/isa.ac
    -
    -

    20. Buildroot

    +

    21. Buildroot

    -

    20.1. Introduction to Buildroot

    +

    21.1. Introduction to Buildroot

    Buildroot is a set of Make scripts that download and compile from source compatible versions of:

    @@ -28685,7 +28734,7 @@ gensim/models/armv8/isa.ac

    Linux kernel

  • -

    C standard library: Buildroot supports several implementations, see: Section 20.10, “libc choice”

    +

    C standard library: Buildroot supports several implementations, see: Section 21.10, “libc choice”

  • BusyBox: provides the shell and basic command line utilities

    @@ -28696,7 +28745,7 @@ gensim/models/armv8/isa.ac

    It therefore produces a pristine, blob-less, debuggable setup, where all moving parts are configured to work perfectly together.

  • -

    Perhaps the awesomeness of Buildroot only sinks in once you notice that all it takes is 4 commands as explained at Section 20.11, “Buildroot hello world”.

    +

    Perhaps the awesomeness of Buildroot only sinks in once you notice that all it takes is 4 commands as explained at Section 21.11, “Buildroot hello world”.

    The downsides of Buildroot are:

    @@ -28741,7 +28790,7 @@ gensim/models/armv8/isa.ac
    -

    20.2. Custom Buildroot configs

    +

    21.2. Custom Buildroot configs

    We provide the following mechanisms:

    @@ -28776,10 +28825,10 @@ gensim/models/armv8/isa.ac

    The clean is necessary because the source files didn’t change, so make would just check the timestamps and not build anything.

    -

    You will then likely want to make those more permanent as explained at: Section 33.4, “Default command line arguments”.

    +

    You will then likely want to make those more permanent as explained at: Section 34.4, “Default command line arguments”.

    -

    20.2.1. Enable Buildroot compiler optimizations

    +

    21.2.1. Enable Buildroot compiler optimizations

    If you are benchmarking compiled programs instead of hand written assembly, remember that we configure Buildroot to disable optimizations by default with:

    @@ -28811,7 +28860,7 @@ gensim/models/armv8/isa.ac
    -

    20.4. Change user

    +

    21.4. Change user

    At startup, we login automatically as the root user.

    @@ -28910,7 +28959,7 @@ make menuconfig
    -

    20.4.1. Login as a non-root user without password

    +

    21.4.1. Login as a non-root user without password

    Replace on inittab:

    @@ -28933,7 +28982,7 @@ make menuconfig
    -

    20.5. Add new files to the Buildroot image

    +

    21.5. Add new files to the Buildroot image

    There are basically two choices:

    @@ -28986,7 +29035,7 @@ make menuconfig
    -

    20.5.1. Add new Buildroot packages

    +

    21.5.1. Add new Buildroot packages

    First, see if you can’t get away without actually adding a new package, for example:

    @@ -28996,7 +29045,7 @@ make menuconfig

    if you have a standalone C file with no dependencies besides the C standard library to be compiled with GCC, just add a new file under buildroot_packages/sample_package and you are done

  • -

    if you have a dependency on a library, first check if Buildroot doesn’t have a package for it already with ls buildroot/package. If yes, just enable that package as explained at: Section 20.2, “Custom Buildroot configs”

    +

    if you have a dependency on a library, first check if Buildroot doesn’t have a package for it already with ls buildroot/package. If yes, just enable that package as explained at: Section 21.2, “Custom Buildroot configs”

  • @@ -29004,7 +29053,7 @@ make menuconfig

    If none of those methods are flexible enough for you, you can just fork or hack up buildroot_packages/sample_package the sample package to do what you want.

    -

    For how to use that package, see: Section 33.15.2, “buildroot_packages directory”.

    +

    For how to use that package, see: Section 34.15.2, “buildroot_packages directory”.

    Then iterate trying to do what you want and reading the manual until it works: https://buildroot.org/downloads/manual/manual.html

    @@ -29012,7 +29061,7 @@ make menuconfig
    -

    20.6. Remove Buildroot packages

    +

    21.6. Remove Buildroot packages

    Once you’ve built a package in to the image, there is no easy way to remove it.

    @@ -29023,11 +29072,11 @@ make menuconfig

    Also mentioned at: https://stackoverflow.com/questions/47320800/how-to-clean-only-target-in-buildroot

    -

    See this for a sample manual workaround: Section 21.8.1.4, “PARSEC uninstall”.

    +

    See this for a sample manual workaround: Section 22.8.1.4, “PARSEC uninstall”.

    -

    20.7. BR2_TARGET_ROOTFS_EXT2_SIZE

    +

    21.7. BR2_TARGET_ROOTFS_EXT2_SIZE

    When adding new large package to the Buildroot root filesystem, it may fail with the message:

    @@ -29079,7 +29128,7 @@ TODO benchmark: would gem5 suffer a considerable disk read performance hit due t

    Bibliography: https://stackoverflow.com/questions/49211241/is-there-a-way-to-automatically-detect-the-minimum-required-br2-target-rootfs-ex

    -

    20.7.1. SquashFS

    +

    21.7.1. SquashFS

    SquashFS creation with mksquashfs does not take fixed sizes, and I have successfully booted from it, but it is readonly, which is unacceptable.

    @@ -29092,7 +29141,7 @@ TODO benchmark: would gem5 suffer a considerable disk read performance hit due t
    -

    20.8. Buildroot rebuild is slow when the root filesystem is large

    +

    21.8. Buildroot rebuild is slow when the root filesystem is large

    Buildroot is not designed for large root filesystem images, and the rebuild becomes very slow when we add a large package to it.

    @@ -29130,7 +29179,7 @@ TODO benchmark: would gem5 suffer a considerable disk read performance hit due t
    -

    20.9. Report upstream bugs

    +

    21.9. Report upstream bugs

    When asking for help on upstream repositories outside of this repository, you will need to provide the commands that you are running in detail without referencing our scripts.

    @@ -29190,7 +29239,7 @@ git -C "$(./getvar qemu_source_dir)" checkout -

    Then, you will also want to do a Bisection to pinpoint the exact commit to blame, and CC that developer.

    -

    Finally, give the images you used save upstream developers' time as shown at: Section 33.19.2, “release-zip”.

    +

    Finally, give the images you used save upstream developers' time as shown at: Section 34.19.2, “release-zip”.

    For Buildroot problems, you should wither provide the config you have:

    @@ -29205,7 +29254,7 @@ git -C "$(./getvar qemu_source_dir)" checkout -
    -

    20.10. libc choice

    +

    21.10. libc choice

    Buildroot supports several libc implementations, including:

    @@ -29253,7 +29302,7 @@ git -C "$(./getvar qemu_source_dir)" checkout -
    -

    20.11. Buildroot hello world

    +

    21.11. Buildroot hello world

    This repo doesn’t do much more other than setting a bunch of Buildroot configurations and building it.

    @@ -29298,7 +29347,7 @@ git -C "$(./getvar qemu_source_dir)" checkout -
    -

    20.12. Update the Buildroot toolchain

    +

    21.12. Update the Buildroot toolchain

    Users of this repo will often want to update the compilation toolchain to the latest version to get fresh new features like new ISA instructions.

    @@ -29312,7 +29361,7 @@ git -C "$(./getvar qemu_source_dir)" checkout -

    In this section we cover the most common cases.

    -

    20.12.1. Update GCC: GCC supported by Buildroot

    +

    21.12.1. Update GCC: GCC supported by Buildroot

    This is of course the simplest case.

    @@ -29430,9 +29479,9 @@ cd ../..
    -

    20.12.2. Update GCC: GCC not supported by Buildroot

    +

    21.12.2. Update GCC: GCC not supported by Buildroot

    -

    Now it gets fun, but well, guess what, we will try to do the same as Section 20.12.1, “Update GCC: GCC supported by Buildroot” but:

    +

    Now it gets fun, but well, guess what, we will try to do the same as Section 21.12.1, “Update GCC: GCC supported by Buildroot” but:

    -

    20.13. Buildroot vanilla kernel

    +

    21.13. Buildroot vanilla kernel

    By default, our build system uses build-linux, and the Buildroot kernel build is disabled: https://stackoverflow.com/questions/52231793/can-buildroot-build-the-root-filesystem-without-building-the-linux-kernel

    @@ -29522,7 +29571,7 @@ cd ../..
    -

    21. Userland content

    +

    22. Userland content

    This section documents our test and educational userland content, such as C, C++ and POSIX examples, present mostly under userland/.

    @@ -29531,7 +29580,7 @@ cd ../..

    Getting started at: Section 1.8, “Userland setup”

    -

    Userland assembly content is located at: Section 22, “Userland assembly”. It was split from this section basically because we were hitting the HTML h6 limit, stupid web :-)

    +

    Userland assembly content is located at: Section 23, “Userland assembly”. It was split from this section basically because we were hitting the HTML h6 limit, stupid web :-)

    This content makes up the bulk of the userland/ directory.

    @@ -29543,7 +29592,7 @@ cd ../..

    This section was originally moved in here from: https://github.com/cirosantilli/cpp-cheat

    -

    21.1. C

    +

    22.1. C

    Programs under userland/c/ are examples of ANSI C programming:

    @@ -29682,7 +29731,7 @@ cd ../..
    -
    21.1.1.1. malloc implementation
    +
    22.1.1.1. malloc implementation

    TODO: the exact answer is going to be hard.

    @@ -29741,7 +29790,7 @@ printf '%x\n' 4198400
    -
    21.1.1.2. malloc maximum size
    +
    22.1.1.2. malloc maximum size
    @@ -29807,7 +29856,7 @@ echo 1 > /proc/sys/vm/overcommit_memory

    If we start using the pages, the OOM killer would sooner or later step in and kill our process: Linux out-of-memory killer.

    -
    21.1.1.2.1. Linux out-of-memory killer
    +
    22.1.1.2.1. Linux out-of-memory killer

    We can observe the OOM in LKMC 1e969e832f66cb5a72d12d57c53fb09e9721d589 which defaults to 256MiB of memory with:

    @@ -29833,7 +29882,7 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -

    21.1.2. C multithreading

    +

    22.1.2. C multithreading

    Added in C11!

    @@ -29851,7 +29900,7 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -
    21.1.2.1. atomic.c
    +
    22.1.2.1. atomic.c

    userland/c/atomic.c

    @@ -29927,9 +29976,9 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -

    21.1.3. GCC C extensions

    +

    22.1.3. GCC C extensions

    -
    21.1.3.1. C empty struct
    +
    22.1.3.1. C empty struct
    @@ -29941,7 +29990,7 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -
    21.1.3.2. OpenMP
    +
    22.1.3.2. OpenMP

    GCC implements the OpenMP threading implementation: https://stackoverflow.com/questions/3949901/pthreads-vs-openmp

    @@ -29964,7 +30013,7 @@ echo 1 > /proc/sys/vm/overcommit_memory

    strace shows that OpenMP makes clone() syscalls in Linux. TODO: does it actually call pthread_ functions, or does it make syscalls directly? Or in other words, can it work on Freestanding programs? A quick grep shows many references to pthreads.

    -
    21.1.3.2.1. OpenMP validation
    +
    22.1.3.2.1. OpenMP validation

    https://github.com/uhhpctools/omp-validation

    @@ -30062,7 +30111,7 @@ mkdir -p bin/c
    -

    21.2. C++

    +

    22.2. C++

    Programs under userland/cpp/ are examples of ISO C programming.

    @@ -30186,7 +30235,7 @@ mkdir -p bin/c
    -

    21.2.1. C++ initialization types

    +

    22.2.1. C++ initialization types

    OMG this is hell, understand when primitive variables are initialized or not:

    @@ -30234,7 +30283,7 @@ mkdir -p bin/c
    -

    21.2.2. C++ multithreading

    +

    22.2.2. C++ multithreading

    -
    21.2.2.1. atomic.cpp
    +
    22.2.2.1. atomic.cpp
    @@ -30465,7 +30514,7 @@ time ./mutex.out 4 100000000
    -
    21.2.2.1.1. Detailed gem5 analysis of how data races happen
    +
    22.2.2.1.1. Detailed gem5 analysis of how data races happen

    The smallest data race we managed to come up as of LKMC 7c01b29f1ee7da878c7cc9cb4565f3f3cf516a92 and gem5 872cb227fdc0b4d60acc7840889d567a6936b6e1 was with userland/c/atomic.c (see also C multithreading):

    @@ -30570,7 +30619,7 @@ non-atomic 19
    -
    21.2.2.2. C++ std::memory_order
    +
    22.2.2.2. C++ std::memory_order

    https://stackoverflow.com/questions/12346487/what-do-each-memory-order-mean

    @@ -30582,7 +30631,7 @@ non-atomic 19
    -
    21.2.2.3. C++ parallel algorithms
    +
    22.2.2.3. C++ parallel algorithms

    https://stackoverflow.com/questions/51031060/are-c17-parallel-algorithms-implemented-already/55989883#55989883

    @@ -30592,7 +30641,7 @@ non-atomic 19
    -

    21.2.3. C++ standards

    +

    22.2.3. C++ standards

    Like for C, you have to pay for the standards…​ insane. So we just use the closest free drafts instead.

    @@ -30600,14 +30649,14 @@ non-atomic 19

    https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents

    -
    21.2.3.1. C++17 N4659 standards draft
    +
    22.2.3.1. C++17 N4659 standards draft

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf

    -

    21.2.4. C++ type casting

    +

    22.2.4. C++ type casting

    userland/cpp/static_dynamic_reinterpret_cast.cpp

    @@ -30617,7 +30666,7 @@ non-atomic 19
    -

    21.3. POSIX

    +

    22.3. POSIX

    Programs under userland/posix/ are examples of POSIX C programming.

    @@ -30635,13 +30684,13 @@ non-atomic 19
    -

    21.3.1. Environment variables

    +

    22.3.1. Environment variables

    POSIX C example that prints all environment variables: userland/posix/environ.c

    -

    21.3.2. unistd.h

    +

    22.3.2. unistd.h

    -

    21.3.3. fork

    +

    22.3.3. fork

    POSIX' multiprocess API. Contrast with pthreads which are for threads.

    @@ -30679,7 +30728,7 @@ fork() return = 13039

    Read the source comments and understand everything that is going on!

    -
    21.3.3.1. getpid
    +
    22.3.3.1. getpid

    The minimal interesting example is to use fork and observe different PIDs.

    @@ -30691,7 +30740,7 @@ fork() return = 13039
    -
    21.3.3.2. Fork bomb
    +
    22.3.3.2. Fork bomb

    https://en.wikipedia.org/wiki/Fork_bomb

    @@ -30726,7 +30775,7 @@ fork() return = 13039
    -

    21.3.4. pthreads

    +

    22.3.4. pthreads

    POSIX' multithreading API. Contrast with fork which is for processes.

    @@ -30750,7 +30799,7 @@ fork() return = 13039
    -
    21.3.4.1. pthread_mutex
    +
    22.3.4.1. pthread_mutex

    userland/posix/pthread_count.c exemplifies the functions:

    @@ -30787,7 +30836,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.3.5. sysconf

    +

    22.3.5. sysconf

    https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html

    @@ -30833,7 +30882,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.3.6. mmap

    +

    22.3.6. mmap

    The mmap system call allows advanced memory operations.

    @@ -30844,7 +30893,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -
    21.3.6.1. mmap MAP_ANONYMOUS
    +
    22.3.6.1. mmap MAP_ANONYMOUS

    Basic mmap example, do the same as userland/c/malloc.c, but with mmap.

    @@ -30862,7 +30911,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -
    21.3.6.2. mmap file
    +
    22.3.6.2. mmap file

    Memory mapped file example: userland/posix/mmap_file.c

    @@ -30874,7 +30923,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -
    21.3.6.3. brk
    +
    22.3.6.3. brk

    Previously POSIX, but was deprecated in favor of malloc

    @@ -30890,7 +30939,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.3.7. socket

    +

    22.3.7. socket

    A bit like read and write, but from / to the Internet!

    @@ -30904,7 +30953,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.4. Userland multithreading

    +

    22.4. Userland multithreading

    The following sections are related to multithreading in userland:

    @@ -30966,12 +31015,12 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.5. C debugging

    +

    22.5. C debugging

    Let’s group the hard-to-debug undefined-behaviour-like stuff found in C / C+ here and how to tackle those problems.

    -

    21.5.1. Stack smashing

    +

    22.5.1. Stack smashing

    @@ -30991,7 +31040,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.5.2. Memory leaks

    +

    22.5.2. Memory leaks

    @@ -31000,7 +31049,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.5.3. Profiling userland programs

    +

    22.5.3. Profiling userland programs

    @@ -31020,12 +31069,12 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.6. Interpreted languages

    +

    22.6. Interpreted languages

    Maybe some day someone will use this setup to study the performance of interpreters.

    -

    21.6.1. Python

    +

    22.6.1. Python

    Examples:

    @@ -31050,7 +31099,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -
    21.6.1.1. Build and install the interpreter
    +
    22.6.1.1. Build and install the interpreter

    Buildroot has a Python package that can be added to the guest image:

    @@ -31109,7 +31158,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -
    21.6.1.2. Python gem5 user mode simulation
    +
    22.6.1.2. Python gem5 user mode simulation

    At LKMC 50ac89b779363774325c81157ec8b9a6bdb50a2f gem5 390a74f59934b85d91489f8a563450d8321b602da:

    @@ -31159,7 +31208,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -
    21.6.1.3. Embedding Python in another application
    +
    22.6.1.3. Embedding Python in another application

    Here we will add some better examples and explanations for: https://docs.python.org/3/extending/embedding.html#very-high-level-embedding

    @@ -31210,7 +31259,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -
    21.6.1.4. pybind11
    +
    22.6.1.4. pybind11
    @@ -31233,7 +31282,7 @@ There are no non-locking atomic types or atomic primitives in POSIX: -

    21.6.2. Node.js

    +

    22.6.2. Node.js

    @@ -31330,7 +31379,7 @@ my type is MyClassToString and a is 1 and b is 2
    -
    21.6.2.1. NPM
    +
    22.6.2.1. NPM
    @@ -31349,7 +31398,7 @@ my type is MyClassToString and a is 1 and b is 2
    -
    21.6.2.1.1. NPM data-files
    +
    22.6.2.1.1. NPM data-files

    Illustrates how to add extra non-code data files to an NPM package, and then use those files at runtime.

    @@ -31360,7 +31409,7 @@ my type is MyClassToString and a is 1 and b is 2
    -

    21.6.3. Java

    +

    22.6.3. Java

    No OpenJDK package as of 2018.08: https://stackoverflow.com/questions/28874150/buildroot-with-jamvm-2-0-for-java-8/59290927#59290927 partly because their build system is shit like the rest of the project’s setup.

    @@ -31376,7 +31425,7 @@ my type is MyClassToString and a is 1 and b is 2
    -

    21.7. Algorithms

    +

    22.7. Algorithms

    userland/algorithm

    @@ -31536,7 +31585,7 @@ cmp tmp.o tmp.e

    These are good targets for performance analysis with gem5, and there is some overlap between this section and Benchmarks.

    -

    21.7.1. BST vs heap vs hashmap

    +

    22.7.1. BST vs heap vs hashmap

    TODO: move benchmark graph from userland/cpp/bst_vs_heap_vs_hashmap.cpp to userland/algorithm/set.

    @@ -31654,7 +31703,7 @@ xdg-open bst_vs_heap_vs_hashmap_gem5.tmp.png
    -

    21.7.2. BLAS

    +

    22.7.2. BLAS

    Buildroot supports it, which makes everything just trivial:

    @@ -31706,7 +31755,7 @@ cblas_dgemm( CblasColMajor, CblasNoTrans, CblasTrans,3,3,2 ,1, A,3, B,
    -

    21.7.3. Eigen

    +

    22.7.3. Eigen

    Header only linear algebra library with a mainline Buildroot package:

    @@ -31745,7 +31794,7 @@ cblas_dgemm( CblasColMajor, CblasNoTrans, CblasTrans,3,3,2 ,1, A,3, B,
    -

    21.8. Benchmarks

    +

    22.8. Benchmarks

    These are good targets for performance analysis with gem5.

    @@ -31763,7 +31812,7 @@ cblas_dgemm( CblasColMajor, CblasNoTrans, CblasTrans,3,3,2 ,1, A,3, B,
    -

    21.8.1. PARSEC benchmark

    +

    22.8.1. PARSEC benchmark

    We have ported parts of the PARSEC benchmark for cross compilation at: https://github.com/cirosantilli/parsec-benchmark See the documentation on that repo to find out which benchmarks have been ported. Some of the benchmarks were are segfaulting, they are documented in that repo.

    @@ -31781,7 +31830,7 @@ cblas_dgemm( CblasColMajor, CblasNoTrans, CblasTrans,3,3,2 ,1, A,3, B,
    -
    21.8.1.1. PARSEC benchmark without parsecmgmt
    +
    22.8.1.1. PARSEC benchmark without parsecmgmt
    ./build --arch arm --download-dependencies gem5-buildroot parsec-benchmark
    @@ -31815,7 +31864,7 @@ cblas_dgemm(      CblasColMajor, CblasNoTrans, CblasTrans,3,3,2  ,1,    A,3,  B,
     
    -
    21.8.1.2. PARSEC change the input size
    +
    22.8.1.2. PARSEC change the input size

    Running a benchmark of a size different than test, e.g. simsmall, requires a rebuild with:

    @@ -31879,7 +31928,7 @@ cblas_dgemm( CblasColMajor, CblasNoTrans, CblasTrans,3,3,2 ,1, A,3, B,
    -
    21.8.1.3. PARSEC benchmark with parsecmgmt
    +
    22.8.1.3. PARSEC benchmark with parsecmgmt

    Most users won’t want to use this method because:

    @@ -31942,9 +31991,9 @@ parsecmgmt -a run -p splash2x.fmm -i test
    -
    21.8.1.4. PARSEC uninstall
    +
    22.8.1.4. PARSEC uninstall
    -

    If you want to remove PARSEC later, Buildroot doesn’t provide an automated package removal mechanism as mentioned at: Section 20.6, “Remove Buildroot packages”, but the following procedure should be satisfactory:

    +

    If you want to remove PARSEC later, Buildroot doesn’t provide an automated package removal mechanism as mentioned at: Section 21.6, “Remove Buildroot packages”, but the following procedure should be satisfactory:

    @@ -31960,7 +32009,7 @@ parsecmgmt -a run -p splash2x.fmm -i test
    -
    21.8.1.5. PARSEC benchmark hacking
    +
    22.8.1.5. PARSEC benchmark hacking

    If you end up going inside submodules/parsec-benchmark to hack up the benchmark (you will!), these tips will be helpful.

    @@ -32012,7 +32061,7 @@ git clean -xdf .
    -
    21.8.1.6. Coremark
    +
    22.8.1.6. Coremark

    https://en.wikipedia.org/wiki/Coremark

    @@ -32225,7 +32274,7 @@ RUN_FLAGS =
    -

    21.8.2. Microbenchmarks

    +

    22.8.2. Microbenchmarks

    It eventually has to come to that, hasn’t it?

    @@ -32262,7 +32311,7 @@ RUN_FLAGS =
    -
    21.8.2.1. Dhrystone
    +
    22.8.2.1. Dhrystone

    https://en.wikipedia.org/wiki/Dhrystone

    @@ -32379,7 +32428,7 @@ Dhrystones per Second: 16152479.0
    -
    21.8.2.2. LMbench
    +
    22.8.2.2. LMbench

    http://www.bitmover.com/lmbench/

    @@ -32497,7 +32546,7 @@ make
    -
    21.8.2.3. STREAM benchmark
    +
    22.8.2.3. STREAM benchmark

    http://www.cs.virginia.edu/stream/ref.html

    @@ -32623,7 +32672,7 @@ Solution Validates: avg error less than 1.000000e-13 on all three arrays
    -

    21.9. userland/libs directory

    +

    22.9. userland/libs directory

    Tests under userland/libs require certain optional libraries to be installed on the target, and are not built or tested by default, you must enable them with either:

    @@ -32637,7 +32686,7 @@ Solution Validates: avg error less than 1.000000e-13 on all three arrays

    See for example BLAS.

    -

    21.9.1. Boost

    +

    22.9.1. Boost

    https://en.wikipedia.org/wiki/Boost_(C%2B%2B_libraries)

    @@ -32653,7 +32702,7 @@ Solution Validates: avg error less than 1.000000e-13 on all three arrays
    -

    21.9.2. HDF5

    +

    22.9.2. HDF5

    https://en.wikipedia.org/wiki/Hierarchical_Data_Format

    @@ -32676,7 +32725,7 @@ Solution Validates: avg error less than 1.000000e-13 on all three arrays
    -

    21.10. Userland content filename conventions

    +

    22.10. Userland content filename conventions

    The following basenames should always refer to programs that do the same thing, but in different languages:

    @@ -32705,7 +32754,7 @@ Solution Validates: avg error less than 1.000000e-13 on all three arrays
    -

    21.11. Userland content bibliography

    +

    22.11. Userland content bibliography

    -

    22. Userland assembly

    +

    23. Userland assembly

    Programs under userland/arch/<arch>/ are examples of userland assembly programming.

    @@ -32818,7 +32867,7 @@ Solution Validates: avg error less than 1.000000e-13 on all three arrays
  • -

    registers, see: Section 22.1, “Assembly registers”

    +

    registers, see: Section 23.1, “Assembly registers”

  • jumping:

    @@ -32961,14 +33010,14 @@ error: asm_main returned 1 at line 8
  • -

    22.1. Assembly registers

    +

    23.1. Assembly registers

    After seeing an ADD hello world, you need to learn the general registers:

    -

    22.1.1. ARMv8 aarch64 x31 register

    +

    23.1.1. ARMv8 aarch64 x31 register

    @@ -33084,7 +33133,7 @@ When instructions do not interpret this operand encoding as the zero register, u
    -

    22.2. Floating point assembly

    +

    23.2. Floating point assembly

    Keep in mind that many ISAs started floating point as an optional thing, and it later got better integrated into the main CPU, side by side with SIMD.

    @@ -33126,7 +33175,7 @@ When instructions do not interpret this operand encoding as the zero register, u
    -

    22.3. SIMD assembly

    +

    23.3. SIMD assembly

    Much like ADD for non-SIMD, start learning SIMD instructions by looking at the integer and floating point SIMD ADD instructions of each ISA:

    @@ -33216,14 +33265,14 @@ When instructions do not interpret this operand encoding as the zero register, u

    Bibliography: https://stackoverflow.com/questions/1389712/getting-started-with-intel-x86-sse-simd-instructions/56409539#56409539

    -

    22.3.1. FMA instruction

    +

    23.3.1. FMA instruction

    Fused multiply add:

    @@ -33265,7 +33314,7 @@ When instructions do not interpret this operand encoding as the zero register, u
    -

    22.4. User vs system assembly

    +

    23.4. User vs system assembly

    By "userland assembly", we mean "the parts of the ISA which can be freely used from userland".

    @@ -33276,7 +33325,7 @@ When instructions do not interpret this operand encoding as the zero register, u

    One big difference between both is that we can run userland assembly on Userland setup, which is easier to get running and debug.

    -

    In particular, most userland assembly examples link to the C standard library, see: Section 22.5, “Userland assembly C standard library”.

    +

    In particular, most userland assembly examples link to the C standard library, see: Section 23.5, “Userland assembly C standard library”.

    Userland assembly is generally simpler, and a pre-requisite for Baremetal setup.

    @@ -33286,7 +33335,7 @@ When instructions do not interpret this operand encoding as the zero register, u
    -

    22.5. Userland assembly C standard library

    +

    23.5. Userland assembly C standard library

    All examples except the Freestanding programs link to the C standard library.

    @@ -33319,7 +33368,7 @@ When instructions do not interpret this operand encoding as the zero register, u
    -

    22.5.1. Freestanding programs

    +

    23.5.1. Freestanding programs

    Unlike most our other assembly examples, which use the C standard library for portability, examples under freestanding/ directories don’t link to the C standard library:

    @@ -33374,7 +33423,7 @@ When instructions do not interpret this operand encoding as the zero register, u

    This is analogous to step debugging baremetal examples.

    -
    22.5.1.1. nostartfiles programs
    +
    23.5.1.1. nostartfiles programs

    Assembly examples under nostartfiles directories can use the standard library, but they don’t use the pre-main boilerplate and start directly at our explicitly given _start:

    @@ -33457,7 +33506,7 @@ Is it any easy to determine which functions I can use or not, in case there are
    -

    22.6. GCC inline assembly

    +

    23.6. GCC inline assembly

    Examples under arch/<arch>/c/ directories show to how use inline assembly from higher level languages such as C:

    @@ -33520,7 +33569,7 @@ Is it any easy to determine which functions I can use or not, in case there are
    -

    22.6.1. GCC inline assembly register variables

    +

    23.6.1. GCC inline assembly register variables

    Used notably in some of the Linux system calls setups:

    @@ -33544,14 +33593,14 @@ Is it any easy to determine which functions I can use or not, in case there are

    In arm, it is the only way to achieve this effect: https://stackoverflow.com/questions/10831792/how-to-use-specific-register-in-arm-inline-assembler

    -

    This feature notably useful for making system calls from C, see: Section 22.7, “Linux system calls”.

    +

    This feature notably useful for making system calls from C, see: Section 23.7, “Linux system calls”.

    Documentation: https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Explicit-Reg-Vars.html

    -

    22.6.2. GCC inline assembly scratch registers

    +

    23.6.2. GCC inline assembly scratch registers

    How to use temporary registers in inline assembly:

    @@ -33577,7 +33626,7 @@ Is it any easy to determine which functions I can use or not, in case there are
    -

    22.6.3. GCC inline assembly early-clobbers

    +

    23.6.3. GCC inline assembly early-clobbers

    An example of using the & early-clobber modifier: link:userland/arch/aarch64/earlyclobber.c

    @@ -33589,7 +33638,7 @@ Is it any easy to determine which functions I can use or not, in case there are
    -

    22.6.4. GCC inline assembly floating point ARM

    +

    23.6.4. GCC inline assembly floating point ARM

    Not documented as of GCC 8.2, but possible: https://stackoverflow.com/questions/53960240/armv8-floating-point-output-inline-assembly

    @@ -33605,7 +33654,7 @@ Is it any easy to determine which functions I can use or not, in case there are
    -

    22.6.5. GCC intrinsics

    +

    23.6.5. GCC intrinsics

    Pre-existing C wrappers using inline assembly, this is what production programs should use instead of inline assembly for SIMD:

    @@ -33627,7 +33676,7 @@ Is it any easy to determine which functions I can use or not, in case there are
    -
    22.6.5.1. GCC x86 intrinsics
    +
    23.6.5.1. GCC x86 intrinsics

    Good official cheatsheet with all intrinsics and what they expand to: https://software.intel.com/sites/landingpage/IntrinsicsGuide

    @@ -33755,7 +33804,7 @@ zmmintrin.h AVX512
    -

    22.7. Linux system calls

    +

    23.7. Linux system calls

    The following Userland setup programs illustrate how to make system calls:

    @@ -33854,7 +33903,7 @@ zmmintrin.h AVX512
    -

    22.7.1. futex system call

    +

    23.7.1. futex system call

    This is how threads either:

    @@ -33916,7 +33965,7 @@ child after parent sleep
    -
    22.7.1.1. Userland mutex implementation
    +
    23.7.1.1. Userland mutex implementation

    The best article to understand spinlocks is: https://eli.thegreenplace.net/2018/basics-of-futexes/

    @@ -33926,7 +33975,7 @@ child after parent sleep
    -

    22.7.2. getcpu system call and the sched_getaffinity glibc wrapper

    +

    23.7.2. getcpu system call and the sched_getaffinity glibc wrapper

    Examples:

    @@ -34001,7 +34050,7 @@ child after parent sleep
    -

    22.8. Linux calling conventions

    +

    23.8. Linux calling conventions

    A summary of results is shown at: Table 3, “Summary of Linux calling conventions for several architectures”.

    @@ -34043,7 +34092,7 @@ child after parent sleep
    -

    22.8.1. x86_64 calling convention

    +

    23.8.1. x86_64 calling convention

    Examples:

    @@ -34072,7 +34121,7 @@ child after parent sleep
    -

    22.8.2. ARM calling convention

    +

    23.8.2. ARM calling convention

    Call C standard library functions from assembly and vice versa.

    @@ -34134,7 +34183,7 @@ child after parent sleep
    -

    22.9. GNU GAS assembler

    +

    23.9. GNU GAS assembler

    GNU GAS is the default assembler used by GDB, and therefore it completely dominates in Linux.

    @@ -34142,7 +34191,7 @@ child after parent sleep

    The Linux kernel in particular uses GNU GAS assembly extensively for the arch specific parts under arch/.

    -

    22.9.1. GNU GAS assembler comments

    +

    23.9.1. GNU GAS assembler comments

    In this tutorial, we use exclusively C Preprocessor /**/ comments because:

    @@ -34177,7 +34226,7 @@ child after parent sleep
    -

    22.9.2. GNU GAS assembler immediates

    +

    23.9.2. GNU GAS assembler immediates

    Summary:

    @@ -34209,7 +34258,7 @@ child after parent sleep
    -

    22.9.3. GNU GAS assembler data sizes

    +

    23.9.3. GNU GAS assembler data sizes

    Let’s see how many bytes go into each data type:

    @@ -34301,9 +34350,9 @@ child after parent sleep
    -
    22.9.3.1. GNU GAS assembler ARM specifics
    +
    23.9.3.1. GNU GAS assembler ARM specifics
    -
    22.9.3.1.1. GNU GAS assembler ARM unified syntax
    +
    23.9.3.1.1. GNU GAS assembler ARM unified syntax

    There are two types of ARMv7 assemblies:

    @@ -34348,14 +34397,14 @@ child after parent sleep
  • -

    cannot have implicit destination with shift, see: Section 24.4.4.1, “ARM shift suffixes”

    +

    cannot have implicit destination with shift, see: Section 25.4.4.1, “ARM shift suffixes”

  • -
    22.9.3.2. GNU GAS assembler ARM .n and .w suffixes
    +
    23.9.3.2. GNU GAS assembler ARM .n and .w suffixes

    When reading disassembly, many instructions have either a .n or .w suffix.

    @@ -34368,7 +34417,7 @@ child after parent sleep
    -

    22.9.4. GNU GAS assembler char literals

    +

    23.9.4. GNU GAS assembler char literals

    userland/arch/x86_64/char_literals.S

    @@ -34389,14 +34438,14 @@ child after parent sleep
    -

    22.10. NOP instructions

    +

    23.10. NOP instructions

    @@ -34413,13 +34462,13 @@ child after parent sleep
    -

    23. x86 userland assembly

    +

    24. x86 userland assembly

    -

    Arch agnostic infrastructure getting started at: Section 22, “Userland assembly”.

    +

    Arch agnostic infrastructure getting started at: Section 23, “Userland assembly”.

    -

    23.1. x86 registers

    +

    24.1. x86 registers

    link:userland/arch/x86_64/registers.S

    @@ -34470,7 +34519,7 @@ child after parent sleep
    -

    23.2. x86 addressing modes

    +

    24.2. x86 addressing modes

    @@ -34553,7 +34602,7 @@ child after parent sleep
    -

    23.3. x86 data transfer instructions

    +

    24.3. x86 data transfer instructions

    5.1.1 "Data Transfer Instructions"

    @@ -34584,7 +34633,7 @@ child after parent sleep
    -

    23.3.1. x86 exchange instructions

    +

    24.3.1. x86 exchange instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 7.3.1.2 "Exchange Instructions":

    @@ -34602,7 +34651,7 @@ child after parent sleep

    TODO: concrete multi-thread GCC inline assembly examples of how all those instructions are normally used as synchronization primitives.

    -
    23.3.1.1. x86 CMPXCHG instruction
    +
    24.3.1.1. x86 CMPXCHG instruction

    userland/arch/x86_64/cmpxchg.S

    @@ -34626,7 +34675,7 @@ child after parent sleep
    -

    23.3.2. x86 PUSH and POP instructions

    +

    24.3.2. x86 PUSH and POP instructions

    userland/arch/x86_64/push.S

    @@ -34653,7 +34702,7 @@ add $8, %rsp
    -

    23.3.3. x86 CQTO and CLTQ instructions

    +

    24.3.3. x86 CQTO and CLTQ instructions

    Examples:

    @@ -34754,7 +34803,7 @@ add $8, %rsp
    -

    23.3.4. x86 CMOVcc instructions

    +

    24.3.4. x86 CMOVcc instructions

    -

    It is interesting to compare this with ARMv7 conditional execution: which is available for all instructions, as shown at: Section 24.2.5, “ARM conditional execution”.

    +

    It is interesting to compare this with ARMv7 conditional execution: which is available for all instructions, as shown at: Section 25.2.5, “ARM conditional execution”.

    -

    23.4. x86 binary arithmetic instructions

    +

    24.4. x86 binary arithmetic instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.2 "Binary Arithmetic Instructions":

    @@ -34880,7 +34929,7 @@ add $8, %rsp
    -

    23.5. x86 logical instructions

    +

    24.5. x86 logical instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.4 "Logical Instructions"

    @@ -34902,7 +34951,7 @@ add $8, %rsp
    -

    23.6. x86 shift and rotate instructions

    +

    24.6. x86 shift and rotate instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.5 "Shift and Rotate Instructions"

    @@ -34954,7 +35003,7 @@ add $8, %rsp
    -

    23.7. x86 bit and byte instructions

    +

    24.7. x86 bit and byte instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.6 "Bit and Byte Instructions"

    @@ -35013,7 +35062,7 @@ add $8, %rsp
    -

    23.8. x86 control transfer instructions

    +

    24.8. x86 control transfer instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.7 "Control Transfer Instructions"

    @@ -35032,7 +35081,7 @@ add $8, %rsp
    -

    23.8.1. x86 Jcc instructions

    +

    24.8.1. x86 Jcc instructions

    userland/arch/x86_64/jcc.S

    @@ -35106,7 +35155,7 @@ add $8, %rsp
    -

    23.8.2. x86 LOOP instruction

    +

    24.8.2. x86 LOOP instruction

    userland/arch/x86_64/loop.S

    @@ -35115,7 +35164,7 @@ add $8, %rsp
    -

    23.8.3. x86 string instructions

    +

    24.8.3. x86 string instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.8 "String Instructions"

    @@ -35168,7 +35217,7 @@ add $8, %rsp

    However, as computer architecture evolved, those instructions might not offer considerable speedups anymore, and modern glibc such as 2.29 just uses x86 SIMD operations instead:, see also: https://stackoverflow.com/questions/33480999/how-can-the-rep-stosb-instruction-execute-faster-than-the-equivalent-loop

    -
    23.8.3.1. x86 REP prefix
    +
    24.8.3.1. x86 REP prefix

    Example: userland/arch/x86_64/rep.S

    @@ -35207,7 +35256,7 @@ add $8, %rsp
    -

    23.8.4. x86 ENTER and LEAVE instructions

    +

    24.8.4. x86 ENTER and LEAVE instructions

    userland/arch/x86_64/enter.S

    @@ -35258,16 +35307,16 @@ pop %rbp
    -

    23.9. x86 miscellaneous instructions

    +

    24.9. x86 miscellaneous instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.13 "Miscellaneous Instructions"

    -

    NOP: Section 22.10, “NOP instructions”

    +

    NOP: Section 23.10, “NOP instructions”

    -

    23.10. x86 random number generator instructions

    +

    24.10. x86 random number generator instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.15 Random Number Generator Instructions

    @@ -35290,7 +35339,7 @@ pop %rbp

    RDRAND sets the carry flag when data is ready so we must loop if the carry flag isn’t set.

    -

    23.10.1. x86 CPUID instruction

    +

    24.10.1. x86 CPUID instruction

    Example: userland/arch/x86_64/cpuid.S

    @@ -35361,7 +35410,7 @@ pop %rbp
    -

    23.11. x86 x87 FPU instructions

    +

    24.11. x86 x87 FPU instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.2 "X87 FPU INSTRUCTIONS"

    @@ -35454,7 +35503,7 @@ pop %rbp
    -

    23.11.1. x86 x87 FPU vs SIMD

    +

    24.11.1. x86 x87 FPU vs SIMD

    https://stackoverflow.com/questions/1844669/benefits-of-x87-over-sse

    @@ -35493,9 +35542,9 @@ pop %rbp
    -

    23.12. x86 SIMD

    +

    24.12. x86 SIMD

    -

    Parent section: Section 22.3, “SIMD assembly”

    +

    Parent section: Section 23.3, “SIMD assembly”

    History:

    @@ -35529,12 +35578,12 @@ pop %rbp
    -

    23.12.1. x86 SSE instructions

    +

    24.12.1. x86 SSE instructions

    -
    23.12.1.2. x86 SSE packed arithmetic instructions
    +
    24.12.1.2. x86 SSE packed arithmetic instructions
    @@ -35566,14 +35615,14 @@ pop %rbp
    -
    23.12.1.3. x86 SSE conversion instructions
    +
    24.12.1.3. x86 SSE conversion instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.5.1.6 "SSE Conversion Instructions"

    -

    23.12.2. x86 SSE2 instructions

    +

    24.12.2. x86 SSE2 instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.6 "SSE2 INSTRUCTIONS"

    @@ -35585,7 +35634,7 @@ pop %rbp
    -
    23.12.2.1. x86 PADDQ instruction
    +
    24.12.2.1. x86 PADDQ instruction

    userland/arch/x86_64/paddq.S: PADDQ, PADDL, PADDW, PADDB

    @@ -35595,7 +35644,7 @@ pop %rbp
    -

    23.12.3. x86 fused multiply add (FMA)

    +

    24.12.3. x86 fused multiply add (FMA)

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.15 "FUSED-MULTIPLY-ADD (FMA)"

    @@ -35615,12 +35664,12 @@ pop %rbp
    -

    23.13. x86 system instructions

    +

    24.13. x86 system instructions

    Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.20 "SYSTEM INSTRUCTIONS"

    -

    23.13.1. x86 RDTSC instruction

    +

    24.13.1. x86 RDTSC instruction

    Sources:

    @@ -35694,7 +35743,7 @@ pop %rbp
    -
    23.13.1.1. x86 RDTSCP instruction
    +
    24.13.1.1. x86 RDTSCP instruction

    RDTSCP is like RDTSP, but it also stores the CPU ID into ECX: this is convenient because the value of RDTSC depends on which core we are currently on, so you often also want the core ID when you want the RDTSC.

    @@ -35737,7 +35786,7 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1
    -
    23.13.1.2. ARM PMCCNTR register
    +
    24.13.1.2. ARM PMCCNTR register

    TODO We didn’t manage to find a working ARM analogue to x86 RDTSC instruction: kernel_modules/pmccntr.c is oopsing, and even it if weren’t, it likely won’t give the cycle count since boot since it needs to be activate before it starts counting anything:

    @@ -35758,9 +35807,9 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1
    -

    23.14. x86 thread synchronization primitives

    +

    24.14. x86 thread synchronization primitives

    -

    23.14.1. x86 LOCK prefix

    +

    24.14.1. x86 LOCK prefix

    Inline assembly example at: userland/cpp/atomic/x86_64_lock_inc.cpp, see also: atomic.cpp.

    @@ -35786,11 +35835,11 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1
    -

    23.15. x86 assembly bibliography

    +

    24.15. x86 assembly bibliography

    -

    23.15.1. x86 official bibliography

    +

    24.15.1. x86 official bibliography

    -
    23.15.1.1. Intel 64 and IA-32 Architectures Software Developer’s Manuals
    +
    24.15.1.1. Intel 64 and IA-32 Architectures Software Developer’s Manuals

    We are using the May 2019 version unless otherwise noted.

    @@ -35807,25 +35856,25 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1

    Also I can’t find older versions on the website easily, so I just web archive everything.

    -

    24. ARM userland assembly

    +

    25. ARM userland assembly

    -

    Arch general getting started at: Section 22, “Userland assembly”.

    +

    Arch general getting started at: Section 23, “Userland assembly”.

    Instructions here loosely grouped based on that of the ARMv7 architecture reference manual Chapter A4 "The Instruction Sets".

    @@ -35848,7 +35897,7 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1

    We cover here mostly ARMv7, and then treat aarch64 differentially, since much of the ARMv7 userland is the same in aarch32.

    -

    24.1. Introduction to the ARM architecture

    +

    25.1. Introduction to the ARM architecture

    The ARM architecture is has been used on the vast majority of mobile phones in the 2010’s, and on a large fraction of micro controllers.

    @@ -35865,7 +35914,7 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1

    ARM Holdings was bought by the Japanese giant SoftBank in 2016.

    -

    24.1.1. ARMv8 vs ARMv7 vs AArch64 vs AArch32

    +

    25.1.1. ARMv8 vs ARMv7 vs AArch64 vs AArch32

    ARMv7 is the older architecture described at: ARMv7 architecture reference manual.

    @@ -35921,7 +35970,7 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1

    They are described at: ARMv8 architecture reference manual A1.7 "ARMv8 architecture extensions".

    -
    24.1.1.1. AArch32
    +
    25.1.1.1. AArch32

    32-bit mode of operation of ARMv8.

    @@ -35953,7 +36002,7 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1
    -
    24.1.1.2. AArch32 vs AArch64
    +
    25.1.1.2. AArch32 vs AArch64

    A great summary of differences can be found at: https://en.wikipedia.org/wiki/ARM_architecture#AArch64_features

    @@ -35963,17 +36012,17 @@ taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1
    -

    24.1.2. Free ARM implementations

    +

    25.1.2. Free ARM implementations

    The ARM instruction set is itself protected by patents / copyright / whatever, and you have to pay ARM Holdings a licence to implement it, even if you are creating your own custom Verilog code.

    @@ -36012,7 +36061,7 @@ Bibliography: -

    24.1.3. ARM instruction encodings

    +

    25.1.3. ARM instruction encodings

    Understanding the basics of instruction encodings is fundamental to help you to remember what instructions do and why some things are possible or not, notably the ARM LDR pseudo-instruction and the ADRP instruction.

    @@ -36124,7 +36173,7 @@ Bibliography: -
    24.1.3.1. ARM Thumb encoding
    +
    25.1.3.1. ARM Thumb encoding

    Thumb examples are available at:

    @@ -36183,7 +36232,7 @@ Bibliography: -
    24.1.3.2. ARM big endian mode
    +
    25.1.3.2. ARM big endian mode

    ARM can switch between big and little endian mode on the fly!

    @@ -36279,9 +36328,9 @@ Bibliography: -

    24.2. ARM branch instructions

    +

    25.2. ARM branch instructions

    -

    24.2.1. ARM B instruction

    +

    25.2.1. ARM B instruction

    Unconditional branch.

    @@ -36299,7 +36348,7 @@ Bibliography: -

    24.2.2. ARM BEQ instruction

    +

    25.2.2. ARM BEQ instruction

    Branch if equal based on the status registers.

    @@ -36343,7 +36392,7 @@ Bibliography: -

    24.2.3. ARM BL instruction

    +

    25.2.3. ARM BL instruction

    Branch with link, i.e. branch and store the return address on the RL register.

    @@ -36357,13 +36406,13 @@ Bibliography: -
    24.2.3.1. ARM BX instruction
    +
    25.2.3.1. ARM BX instruction
    -
    24.2.3.2. ARMv8 aarch64 ret instruction
    +
    25.2.3.2. ARMv8 aarch64 ret instruction
    @@ -36396,7 +36445,7 @@ Bibliography: -

    24.2.4. ARM CBZ instruction

    +

    25.2.4. ARM CBZ instruction

    Compare and branch if zero.

    @@ -36411,7 +36460,7 @@ Bibliography: -

    24.2.5. ARM conditional execution

    +

    25.2.5. ARM conditional execution

    Weirdly, ARM B instruction and family are not the only instructions that can execute conditionally on the flags: the same also applies to most instructions, e.g. ADD.

    @@ -36427,7 +36476,7 @@ Bibliography: -

    24.3. ARM load and store instructions

    +

    25.3. ARM load and store instructions

    In ARM, there are only two instruction families that do memory access:

    @@ -36451,9 +36500,9 @@ Bibliography: Load/store architecture.

    -

    24.3.1. ARM LDR instruction

    +

    25.3.1. ARM LDR instruction

    -
    24.3.1.1. ARM LDR pseudo-instruction
    +
    25.3.1.1. ARM LDR pseudo-instruction

    LDR can be either a regular instruction that loads stuff into memory, or also a pseudo-instruction (assembler magic): http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0041c/Babbfdih.html

    @@ -36487,7 +36536,7 @@ Bibliography: -
    24.3.1.2. ARM addressing modes
    +
    25.3.1.2. ARM addressing modes
    @@ -36558,7 +36607,7 @@ Bibliography: ARMv8 architecture reference manual: C1.3.3 "Load/Store addressing modes"

    -
    24.3.1.2.1. ARM loop over array
    +
    25.3.1.2.1. ARM loop over array

    As an application of the post-indexed addressing mode, let’s increment an array.

    @@ -36568,7 +36617,7 @@ Bibliography: -
    24.3.1.3. ARM LDRH and LDRB instructions
    +
    25.3.1.3. ARM LDRH and LDRB instructions

    There are LDR variants that load less than full 4 bytes:

    @@ -36595,7 +36644,7 @@ Bibliography: -

    24.3.2. ARM STR instruction

    +

    25.3.2. ARM STR instruction

    Store from memory into registers.

    @@ -36606,7 +36655,7 @@ Bibliography: ARM LDR instruction also applies here so we won’t go into much detail.

    -
    24.3.2.1. ARMv8 aarch64 STR instruction
    +
    25.3.2.1. ARMv8 aarch64 STR instruction

    PC-relative STR is not possible in aarch64.

    @@ -36624,7 +36673,7 @@ Bibliography: -
    24.3.2.2. ARMv8 aarch64 LDP and STP instructions
    +
    25.3.2.2. ARMv8 aarch64 LDP and STP instructions

    Push a pair of registers to the stack.

    @@ -36632,7 +36681,7 @@ Bibliography: lkmc/aarch64.h since it is the main way to restore register state.

    -
    24.3.2.2.1. ARMV8 aarch64 stack alignment
    +
    25.3.2.2.1. ARMV8 aarch64 stack alignment

    In ARMv8, the stack can be enforced to 16-byte alignment.

    @@ -36679,7 +36728,7 @@ Bibliography: -

    24.3.3. ARM LDMIA instruction

    +

    25.3.3. ARM LDMIA instruction

    Pop values form stack into the register and optionally update the address register.

    @@ -36729,7 +36778,7 @@ ldmia sp!, reglist
    -

    24.4. ARM data processing instructions

    +

    25.4. ARM data processing instructions

    Arithmetic:

    @@ -36753,7 +36802,7 @@ ldmia sp!, reglist
    -

    24.4.1. ARM CSET instruction

    +

    25.4.1. ARM CSET instruction

    @@ -36765,7 +36814,7 @@ ldmia sp!, reglist
    -

    24.4.2. ARM bitwise instructions

    +

    25.4.2. ARM bitwise instructions

    • @@ -36783,7 +36832,7 @@ ldmia sp!, reglist
    -
    24.4.2.1. ARM BIC instruction
    +
    25.4.2.1. ARM BIC instruction

    Bitwise Bit Clear: clear some bits.

    @@ -36797,7 +36846,7 @@ ldmia sp!, reglist
    -
    24.4.2.2. ARM UBFM instruction
    +
    25.4.2.2. ARM UBFM instruction

    Unsigned Bitfield Move.

    @@ -36815,7 +36864,7 @@ ldmia sp!, reglist

    TODO: explain full behaviour. Very complicated. Has several simpler to understand aliases.

    -
    24.4.2.2.1. ARM UBFX instruction
    +
    25.4.2.2.1. ARM UBFX instruction

    Alias for:

    @@ -36849,12 +36898,12 @@ ldmia sp!, reglist
    -
    24.4.2.3. ARM BFM instruction
    +
    25.4.2.3. ARM BFM instruction

    TODO: explain. Similar to UBFM but leave untouched bits unmodified.

    -
    24.4.2.3.1. ARM BFI instruction
    +
    25.4.2.3.1. ARM BFI instruction

    Examples:

    @@ -36885,12 +36934,12 @@ ldmia sp!, reglist
    -

    24.4.3. ARM MOV instruction

    +

    25.4.3. ARM MOV instruction

    Move an immediate to a register, or a register to another register.

    -

    Cannot load from or to memory, since only the LDR and STR instruction families can do that in ARM as mentioned at: Section 24.3, “ARM load and store instructions”.

    +

    Cannot load from or to memory, since only the LDR and STR instruction families can do that in ARM as mentioned at: Section 25.3, “ARM load and store instructions”.

    Example: userland/arch/arm/mov.S

    @@ -36951,7 +37000,7 @@ ldmia sp!, reglist

    Assemblers however support magic memory allocations which may hide what is truly going on: https://stackoverflow.com/questions/14046686/why-use-ldr-over-mov-or-vice-versa-in-arm-assembly Always ask your friendly disassembly for a good confirmation.

    -
    24.4.3.1. ARM movw and movt instructions
    +
    25.4.3.1. ARM movw and movt instructions

    Set the higher or lower 16 bits of a register to an immediate in one go.

    @@ -36963,7 +37012,7 @@ ldmia sp!, reglist
    -
    24.4.3.2. ARMv8 aarch64 movk instruction
    +
    25.4.3.2. ARMv8 aarch64 movk instruction

    Fill a 64 bit register with 4 16-bit instructions one at a time.

    @@ -36978,7 +37027,7 @@ ldmia sp!, reglist
    -
    24.4.3.3. ARMv8 aarch64 movn instruction
    +
    25.4.3.3. ARMv8 aarch64 movn instruction

    Set 16-bits negated and the rest to 1.

    @@ -36988,9 +37037,9 @@ ldmia sp!, reglist
    -

    24.4.4. ARM data processing instruction suffixes

    +

    25.4.4. ARM data processing instruction suffixes

    -
    24.4.4.1. ARM shift suffixes
    +
    25.4.4.1. ARM shift suffixes

    Most data processing instructions can also optionally shift the second register operand.

    @@ -37018,7 +37067,7 @@ ldmia sp!, reglist
    -
    24.4.4.2. ARM S suffix
    +
    25.4.4.2. ARM S suffix

    Example: userland/arch/arm/s_suffix.S

    @@ -37034,7 +37083,7 @@ ldmia sp!, reglist
    -

    24.4.5. ARM ADR instruction

    +

    25.4.5. ARM ADR instruction

    Similar rationale to the ARM LDR pseudo-instruction, allowing to easily store a PC-relative reachable address into a register in one go, to overcome the 4-byte fixed instruction size.

    @@ -37058,19 +37107,19 @@ ldmia sp!, reglist

    More details: https://stackoverflow.com/questions/41906688/what-are-the-semantics-of-adrp-and-adrl-instructions-in-arm-assembly/54042899#54042899

    -
    24.4.5.1. ARM ADRL instruction
    +
    25.4.5.1. ARM ADRL instruction
    -

    See: Section 24.4.5, “ARM ADR instruction”.

    +

    See: Section 25.4.5, “ARM ADR instruction”.

    -

    24.5. ARM miscellaneous instructions

    +

    25.5. ARM miscellaneous instructions

    -

    24.5.1. ARM NOP instruction

    +

    25.5.1. ARM NOP instruction

    There are a few different ways to encode NOP, notably MOV a register into itself, and a dedicated miscellaneous instruction.

    @@ -37091,7 +37140,7 @@ ldmia sp!, reglist
    -

    24.5.2. ARM UDF instruction

    +

    25.5.2. ARM UDF instruction

    Guaranteed undefined! Therefore raise illegal instruction signal. Used by GCC __builtin_trap apparently: https://stackoverflow.com/questions/16081618/programmatically-cause-undefined-instruction-exception

    @@ -37110,7 +37159,7 @@ ldmia sp!, reglist
    -

    24.5.3. ARM system register instructions

    +

    25.5.3. ARM system register instructions

    Examples of using them can be found at: dump_regs

    @@ -37217,7 +37266,7 @@ dc isw
    -
    24.5.3.1. ARM system register encodings
    +
    25.5.3.1. ARM system register encodings

    Each aarch64 system register is specified in the encoding of ARM system register instructions by 5 integer numbers:

    @@ -37263,12 +37312,12 @@ LKMC_DUMP_SYSTEM_REGS_PRINTF("ID_ISAR6_EL1 0x%" PRIX32 "\n", id_isar6_el1);
    -

    24.6. ARM SIMD

    +

    25.6. ARM SIMD

    -

    Parent section: Section 22.3, “SIMD assembly”

    +

    Parent section: Section 23.3, “SIMD assembly”

    -

    24.6.1. ARM VFP

    +

    25.6.1. ARM VFP

    The name for the ARMv7 and AArch32 floating point and SIMD instructions / registers.

    @@ -37314,7 +37363,7 @@ LKMC_DUMP_SYSTEM_REGS_PRINTF("ID_ISAR6_EL1 0x%" PRIX32 "\n", id_isar6_el1);
    -
    24.6.1.1. ARM VFP registers
    +
    25.6.1.1. ARM VFP registers

    TODO example

    @@ -37350,20 +37399,20 @@ LKMC_DUMP_SYSTEM_REGS_PRINTF("ID_ISAR6_EL1 0x%" PRIX32 "\n", id_isar6_el1);
    -
    24.6.1.2. ARM VADD instruction
    +
    25.6.1.2. ARM VADD instruction
    -
    24.6.1.3. ARM VCVT instruction
    +
    25.6.1.3. ARM VCVT instruction
    @@ -37392,7 +37441,7 @@ LKMC_DUMP_SYSTEM_REGS_PRINTF("ID_ISAR6_EL1 0x%" PRIX32 "\n", id_isar6_el1);
    -
    24.6.1.3.1. ARM VCVTR instruction
    +
    25.6.1.3.1. ARM VCVTR instruction
    @@ -37410,7 +37459,7 @@ LKMC_DUMP_SYSTEM_REGS_PRINTF("ID_ISAR6_EL1 0x%" PRIX32 "\n", id_isar6_el1);
    -
    24.6.1.3.2. ARMv8 AArch32 VCVTA instruction
    +
    25.6.1.3.2. ARMv8 AArch32 VCVTA instruction
    @@ -37430,7 +37479,7 @@ LKMC_DUMP_SYSTEM_REGS_PRINTF("ID_ISAR6_EL1 0x%" PRIX32 "\n", id_isar6_el1);
    -

    24.6.2. ARMv8 Advanced SIMD and floating-point support

    +

    25.6.2. ARMv8 Advanced SIMD and floating-point support

    The ARMv8 architecture reference manual specifies floating point and SIMD support in the main architecture at A1.5 "Advanced SIMD and floating-point support".

    @@ -37438,13 +37487,13 @@ LKMC_DUMP_SYSTEM_REGS_PRINTF("ID_ISAR6_EL1 0x%" PRIX32 "\n", id_isar6_el1);The feature is often refered to simply as "SIMD&FP" throughout the manual.

    -

    The Linux kernel shows /proc/cpuinfo compatibility as neon, which is yet another intermediate name that came up at some point, see: Section 24.6.2.2, “ARM NEON”.

    +

    The Linux kernel shows /proc/cpuinfo compatibility as neon, which is yet another intermediate name that came up at some point, see: Section 25.6.2.2, “ARM NEON”.

    Vs ARM VFP: https://stackoverflow.com/questions/4097034/arm-cortex-a8-whats-the-difference-between-vfp-and-neon

    -
    24.6.2.1. ARMv8 floating point availability
    +
    25.6.2.1. ARMv8 floating point availability

    Support is semi-mandatory. ARMv8 architecture reference manual A1.5 "Advanced SIMD and floating-point support":

    @@ -37481,7 +37530,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.6.2.2. ARM NEON
    +
    25.6.2.2. ARM NEON

    Just an informal name for the "Advanced SIMD instructions"? Very confusing.

    @@ -37508,7 +37557,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    24.6.3. ARMv8 AArch64 floating point registers

    +

    25.6.3. ARMv8 AArch64 floating point registers

    TODO example.

    @@ -37563,7 +37612,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.6.3.1. ARMv8 aarch64 add vector instruction
    +
    25.6.3.1. ARMv8 aarch64 add vector instruction

    userland/arch/aarch64/add_vector.S

    @@ -37572,21 +37621,21 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.6.3.2. ARMv8 aarch64 FADD instruction
    +
    25.6.3.2. ARMv8 aarch64 FADD instruction
    -
    24.6.3.2.1. ARM FADD vs VADD
    +
    25.6.3.2.1. ARM FADD vs VADD
    -

    It is very confusing, but FADDS and FADDD in Aarch32 are pre-UAL for vadd.f32 and vadd.f64 which we use in this tutorial, see: Section 24.6.1.2, “ARM VADD instruction”

    +

    It is very confusing, but FADDS and FADDD in Aarch32 are pre-UAL for vadd.f32 and vadd.f64 which we use in this tutorial, see: Section 25.6.1.2, “ARM VADD instruction”

    The same goes for most ARMv7 mnemonics: f* is old, and v* is the newer better syntax.

    @@ -37598,12 +37647,12 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    Also keep in mind that fused multiply add is FMADD.

    -
    24.6.3.3. ARMv8 aarch64 LD2 instruction
    +
    25.6.3.3. ARMv8 aarch64 LD2 instruction

    Example: userland/arch/aarch64/ld2.S

    @@ -37619,7 +37668,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    24.6.4. ARM SIMD bibliography

    +

    25.6.4. ARM SIMD bibliography

    -

    24.6.5. ARM SVE

    +

    25.6.5. ARM SVE

    Scalable Vector Extension.

    @@ -37697,7 +37746,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    Using SVE normally requires setting the CPACR_EL1.FPEN and ZEN bits, which as as of lkmc 29fd625f3fda79f5e0ee6cac43517ba74340d513 + 1 we also enable in our Baremetal bootloaders, see also: aarch64 baremetal NEON setup.

    -
    24.6.5.1. ARM SVE VADDL instruction
    +
    25.6.5.1. ARM SVE VADDL instruction

    Get the SVE vector length. The following programs do that and print it to stdout:

    @@ -37713,7 +37762,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.6.5.2. Change ARM SVE vector length in emulators
    +
    25.6.5.2. Change ARM SVE vector length in emulators

    gem5 covered at: https://stackoverflow.com/questions/57692765/how-to-change-the-gem5-arm-sve-vector-length

    @@ -37750,7 +37799,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.6.5.3. SVE bibliography
    +
    25.6.5.3. SVE bibliography
    -
    24.6.5.3.1. SVE spec
    +
    25.6.5.3.1. SVE spec

    ARMv8 architecture reference manual A1.7 "ARMv8 architecture extensions" says:

    @@ -37790,12 +37839,12 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    24.7. ARM thread synchronization primitives

    +

    25.7. ARM thread synchronization primitives

    Parent section: Userland multithreading.

    -

    24.7.1. ARM LDXR and STXR instructions

    +

    25.7.1. ARM LDXR and STXR instructions

    Parent section: atomic.cpp

    @@ -37845,7 +37894,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    24.7.2. ARM Large System Extensions (LSE)

    +

    25.7.2. ARM Large System Extensions (LSE)

    Set of atomic and synchronization primitives added in ARMv8.1 architecture extension.

    @@ -37872,9 +37921,9 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    24.8. ARMv8 architecture extensions

    +

    25.8. ARMv8 architecture extensions

    -

    24.8.1. ARMv8.1 architecture extension

    +

    25.8.1. ARMv8.1 architecture extension

    ARMv8 architecture reference manual db A1.7.3 "The ARMv8.1 architecture extension"

    @@ -37888,9 +37937,9 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    24.9. ARM assembly bibliography

    +

    25.9. ARM assembly bibliography

    -

    24.9.1. ARM non-official bibliography

    +

    25.9.1. ARM non-official bibliography

    Good getting started tutorials:

    @@ -37912,7 +37961,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    24.9.2. ARM official bibliography

    +

    25.9.2. ARM official bibliography

    The official manuals were stored in http://infocenter.arm.com but as of 2017 they started to slowly move to https://developer.arm.com.

    @@ -37926,7 +37975,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    Bibliography: https://www.quora.com/Where-can-I-find-the-official-documentation-of-ARM-instruction-set-architectures-ISAs

    -
    24.9.2.1. ARMv7 architecture reference manual
    +
    25.9.2.1. ARMv7 architecture reference manual

    https://developer.arm.com/products/architecture/a-profile/docs/ddi0406/latest/arm-architecture-reference-manual-armv7-a-and-armv7-r-edition

    @@ -37938,7 +37987,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.9.2.2. ARMv8 architecture reference manual
    +
    25.9.2.2. ARMv8 architecture reference manual

    https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf

    @@ -37994,19 +38043,19 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.9.2.3. ARMv8 architecture reference manual db
    +
    25.9.2.3. ARMv8 architecture reference manual db

    https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf

    -
    24.9.2.4. ARMv8 architecture reference manual db
    +
    25.9.2.4. ARMv8 architecture reference manual db

    https://static.docs.arm.com/ddi0487/fa/DDI0487F_a_armv8_arm.pdf

    -
    24.9.2.5. Programmer’s Guide for ARMv8-A
    +
    25.9.2.5. Programmer’s Guide for ARMv8-A

    https://static.docs.arm.com/den0024/a/DEN0024A_v8_architecture_PG.pdf

    @@ -38021,7 +38070,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.9.2.6. Arm A64 Instruction Set Architecture: Future Architecture Technologies in the A architecture profile Documentation
    +
    25.9.2.6. Arm A64 Instruction Set Architecture: Future Architecture Technologies in the A architecture profile Documentation

    https://developer.arm.com/docs/ddi0602/b

    @@ -38030,7 +38079,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.9.2.7. ARM processor documentation
    +
    25.9.2.7. ARM processor documentation

    ARM also releases documentation specific to each given processor.

    @@ -38054,7 +38103,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.9.2.7.1. ARM Cortex-A15 MPCore Processor Technical Reference Manual r4p0
    +
    25.9.2.7.1. ARM Cortex-A15 MPCore Processor Technical Reference Manual r4p0

    http://infocenter.arm.com/help/topic/com.arm.doc.ddi0438i/DDI0438I_cortex_a15_r4p0_trm.pdf

    @@ -38064,13 +38113,13 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -
    24.9.2.8. Arm Cortex‑A77 Technical Reference Manual r1p1
    +
    25.9.2.8. Arm Cortex‑A77 Technical Reference Manual r1p1

    https://static.docs.arm.com/101111/0101/arm_cortex_a77_trm_101111_0101_04_en.pdf

    -
    24.9.2.9. Arm Cortex‑A77 Software Optimization Guide r1p1
    +
    25.9.2.9. Arm Cortex‑A77 Software Optimization Guide r1p1

    https://static.docs.arm.com/swog011050/c/Arm_Cortex-A77_Software_Optimization_Guide.pdf

    @@ -38080,7 +38129,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    25. ELF

    +

    26. ELF

    https://en.wikipedia.org/wiki/Executable_and_Linkable_Format

    @@ -38094,7 +38143,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    26. IEEE 754

    +

    27. IEEE 754

    https://en.wikipedia.org/wiki/IEEE_754

    @@ -38124,13 +38173,13 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    27. Baremetal

    +

    28. Baremetal

    -

    27.1. Baremetal GDB step debug

    +

    28.1. Baremetal GDB step debug

    GDB step debug works on baremetal exactly as it does on the Linux kernel, which is described at: Section 2, “GDB step debug”.

    @@ -38201,7 +38250,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    -

    27.2. Baremetal bootloaders

    +

    28.2. Baremetal bootloaders

    As can be seen from Baremetal GDB step debug, all examples under baremetal/, with the exception of baremetal/arch/<arch>/no_bootloader, start from our tiny bootloaders:

    @@ -38237,7 +38286,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

    the stack pointer

  • -

    NEON: Section 27.11.2, “aarch64 baremetal NEON setup”

    +

    NEON: Section 28.11.2, “aarch64 baremetal NEON setup”

  • TODO: we don’t do this currently but maybe we should setup BSS

    @@ -38265,7 +38314,7 @@ AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.

  • -

    27.3. Baremetal linker script

    +

    28.3. Baremetal linker script

    For things to work in baremetal, we often have to layout memory in specific ways.

    @@ -38294,7 +38343,7 @@ lkmc_heap_top = .;
    -

    27.4. Baremetal command line arguments

    +

    28.4. Baremetal command line arguments

    QEMU and gem5 currently supports baremetal CLI arguments!

    @@ -38343,7 +38392,7 @@ cc

    It is worth noting that e.g. ARM has a Semihosting mechanism for loading CLI arguments through SYS_GET_CMDLINE, but our mechanism works in principle for any ISA.

    -

    27.4.1. gem5 baremetal arm CLI args

    +

    28.4.1. gem5 baremetal arm CLI args

    Currently not supported, so we just hardcode argc 0 on the arm baremetal bootloader.

    @@ -38353,7 +38402,7 @@ cc
    -

    27.5. Semihosting

    +

    28.5. Semihosting

    Semihosting is a publicly documented interface specified by ARM Holdings that allows us to do some magic operations very useful in development, such as writting to the terminal or reading and writing host files.

    @@ -38471,9 +38520,9 @@ svc 0x00123456
    -

    27.5.1. gem5 semihosting

    +

    28.5.1. gem5 semihosting

    -

    For gem5, you need:

    +

    For gem5, you need patches/manual/gem5-semihost.patch:

    @@ -38486,7 +38535,7 @@ svc 0x00123456
    -

    27.6. gem5 baremetal carriage return

    +

    28.6. gem5 baremetal carriage return

    TODO: our example is printing newlines without automatic carriage return \r as in:

    @@ -38509,7 +38558,7 @@ svc 0x00123456
    -

    27.7. Baremetal host packaged toolchain

    +

    28.7. Baremetal host packaged toolchain

    For arm, some baremetal examples compile fine with:

    @@ -38545,13 +38594,13 @@ collect2: error: ld returned 1 exit status
    -

    27.8. Baremetal C++

    +

    28.8. Baremetal C++

    Didn’t get it working, traking at: https://github.com/cirosantilli/linux-kernel-module-cheat/issues/119

    -

    27.9. GDB builtin CPU simulator

    +

    28.9. GDB builtin CPU simulator

    It is incredible, but GDB also has a CPU simulator inside of it as documented at: https://sourceware.org/gdb/onlinedocs/gdb/Target-Commands.html

    @@ -38611,7 +38660,7 @@ starti
    -

    27.9.1. GDB builtin CPU simulator userland

    +

    28.9.1. GDB builtin CPU simulator userland

    Since I had this compiled, I also decided to try it out on userland.

    @@ -38646,7 +38695,7 @@ starti
    -

    27.10. ARM baremetal

    +

    28.10. ARM baremetal

    In this section we will focus on learning ARM architecture concepts that can only learnt on baremetal setups.

    @@ -38654,7 +38703,7 @@ starti

    Userland information can be found at: https://github.com/cirosantilli/arm-assembly-cheat

    -

    27.10.1. ARM exception levels

    +

    28.10.1. ARM exception levels

    ARM exception levels are analogous to x86 rings.

    @@ -38783,13 +38832,13 @@ CurrentEL.EL 0x3

    According to ARMv7 architecture reference manual, access to that register is controlled by other registers NSACR.{CP11, CP10} and HCPTR so those must be turned off, but I’m lazy to investigate now, even just trying to dump those registers in userland/arch/arm/dump_regs.c also leads to exceptions…​

    -
    27.10.1.1. ARM change exception level
    +
    28.10.1.1. ARM change exception level

    TODO. Create a minimal runnable example of going into EL0 and jumping to EL1.

    -
    27.10.1.2. ARM SP0 vs SPx
    +
    28.10.1.2. ARM SP0 vs SPx

    See ARMv8 architecture reference manual db D1.6.2 "The stack pointer registers".

    @@ -38808,7 +38857,7 @@ CurrentEL.EL 0x3
    -

    27.10.2. ARM SVC instruction

    +

    28.10.2. ARM SVC instruction

    This is the most basic example of exception handling we have.

    @@ -39157,7 +39206,7 @@ IN: main
    -
    27.10.2.1. ARMv8 exception vector table format
    +
    28.10.2.1. ARMv8 exception vector table format

    The vector table format is described on ARMv8 architecture reference manual Table D1-7 "Vector offsets from vector table base address".

    @@ -39297,29 +39346,29 @@ IN: main
    -
    27.10.2.2. ARM ESR register
    +
    28.10.2.2. ARM ESR register

    Exception Syndrome Register.

    -

    See example at: Section 27.10.2, “ARM SVC instruction”

    +

    See example at: Section 28.10.2, “ARM SVC instruction”

    Documentation: ARMv8 architecture reference manual db D12.2.36 "ESR_EL1, Exception Syndrome Register (EL1)".

    -
    27.10.2.3. ARM ELR register
    +
    28.10.2.3. ARM ELR register

    Exception Link Register.

    -

    See the example at: Section 27.10.2, “ARM SVC instruction”

    +

    See the example at: Section 28.10.2, “ARM SVC instruction”

    -

    27.10.3. ARM baremetal multicore

    +

    28.10.3. ARM baremetal multicore

    Examples:

    @@ -39398,7 +39447,7 @@ IN: main

    Bibliography: https://stackoverflow.com/questions/980999/what-does-multicore-assembly-language-look-like/33651438#33651438

    -
    27.10.3.1. ARM WFE and SEV instructions
    +
    28.10.3.1. ARM WFE and SEV instructions

    The WFE and SEV instructions are just hints: a compliant implementation can treat them as NOPs.

    @@ -39551,7 +39600,7 @@ IN: main

    For how userland spinlocks and mutexes are implemented see Userland mutex implementation.

    -
    27.10.3.1.1. ARM WFE global monitor events
    +
    28.10.3.1.1. ARM WFE global monitor events

    Examples:

    @@ -39591,7 +39640,7 @@ IN: main
    -
    27.10.3.1.2. WFE from userland
    +
    28.10.3.1.2. WFE from userland

    WFE and SEV are usable from userland, and are part of an efficient spinlock implementation (which userland should arguably stay away from and rather use the futex system call which allow for non busy sleep instead), which maybe is not something that userland should ever tho and just stick to mutexes?

    @@ -39698,7 +39747,7 @@ IN: main
    -
    27.10.3.1.3. ARMv8 spinlock pattern
    +
    28.10.3.1.3. ARMv8 spinlock pattern

    http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka16277.html

    @@ -39717,7 +39766,7 @@ IN: main
    -
    27.10.3.1.4. gem5 ARM WFE
    +
    28.10.3.1.4. gem5 ARM WFE

    gem5 390a74f59934b85d91489f8a563450d8321b602d does not sleep on the first WFE on either syscall emulation or full system, because the code does:

    @@ -39759,14 +39808,14 @@ IN: main
    -
    27.10.3.1.5. ARM YIELD instruction
    +
    28.10.3.1.5. ARM YIELD instruction

    https://stackoverflow.com/questions/59311066/how-does-the-arm-yield-instruction-inform-other-threads-that-they-could-start-a

    -
    27.10.3.2. ARM LDAXR and STLXR instructions
    +
    28.10.3.2. ARM LDAXR and STLXR instructions

    Can be used to implement atomic variables, see also:

    @@ -39785,7 +39834,7 @@ IN: main
    -
    27.10.3.3. ARM PSCI
    +
    28.10.3.3. ARM PSCI

    In QEMU, CPU 1 starts in a halted state. This can be observed from GDB, where:

    @@ -39835,14 +39884,14 @@ IN: main
    -
    27.10.3.4. ARM DMB instruction
    +
    28.10.3.4. ARM DMB instruction

    TODO: create and study a minimal examples in gem5 where the DMB instruction leads to less cycles: https://stackoverflow.com/questions/15491751/real-life-use-cases-of-barriers-dsb-dmb-isb-in-arm

    -

    27.10.4. ARM timer

    +

    28.10.4. ARM timer

    The ARM timer is the simplest way to generate hardware interrupts periodically, and therefore serves as the simples example of ARM GIC usage.

    @@ -39995,7 +40044,7 @@ cntvct_el0 0x3CF516F
    -

    27.10.5. ARM GIC

    +

    28.10.5. ARM GIC

    Generic Interrupt Controller.

    @@ -40037,7 +40086,7 @@ cntvct_el0 0x3CF516F
    -

    27.10.6. ARM paging

    +

    28.10.6. ARM paging

    TODO create a minimal working aarch64 example analogous to the x86 one at: https://github.com/cirosantilli/x86-bare-metal-examples/blob/6dc9a73830fc05358d8d66128f740ef9906f7677/paging.S

    @@ -40067,9 +40116,9 @@ cntvct_el0 0x3CF516F
    -

    27.10.7. ARM baremetal bibliography

    +

    28.10.7. ARM baremetal bibliography

    -

    First, also consider the userland bibliography: Section 24.9, “ARM assembly bibliography”.

    +

    First, also consider the userland bibliography: Section 25.9, “ARM assembly bibliography”.

    The most useful ARM baremetal example sets we’ve seen so far are:

    @@ -40094,7 +40143,7 @@ cntvct_el0 0x3CF516F
    -
    27.10.7.1. NienfengYao/armv8-bare-metal
    +
    28.10.7.1. NienfengYao/armv8-bare-metal
    @@ -40153,7 +40202,7 @@ cntvct_el0 0x3CF516F
    -
    27.10.7.2. tukl-msd/gem5.bare-metal
    +
    28.10.7.2. tukl-msd/gem5.bare-metal

    https://github.com/tukl-msd/gem5.bare-metal

    @@ -40195,7 +40244,7 @@ make CROSS_COMPILE_DIR=/usr/bin
    -

    27.11. How we got some baremetal stuff to work

    +

    28.11. How we got some baremetal stuff to work

    It is nice when thing just work.

    @@ -40203,7 +40252,7 @@ make CROSS_COMPILE_DIR=/usr/bin

    But you can also learn a thing or two from how I actually made them work in the first place.

    -

    27.11.1. Find the UART address

    +

    28.11.1. Find the UART address

    Enter the QEMU console:

    @@ -40239,7 +40288,7 @@ make CROSS_COMPILE_DIR=/usr/bin
    -

    27.11.2. aarch64 baremetal NEON setup

    +

    28.11.2. aarch64 baremetal NEON setup

    Inside baremetal/lib/aarch64.S there is a chunk of code that enables floating point operations:

    @@ -40363,7 +40412,7 @@ ISB
    -

    27.12. Baremetal tests

    +

    28.12. Baremetal tests

    Baremetal tests work exactly like User mode tests, except that you have to add the --mode baremetal option, for example:

    @@ -40376,13 +40425,13 @@ ISB

    In baremetal, we detect if tests failed by parsing logs for the Magic failure string.

    -

    See: Section 33.16, “Test this repo” for more useful testing tips.

    +

    See: Section 34.16, “Test this repo” for more useful testing tips.

    -

    28. Android

    +

    29. Android

    Remember: Android AOSP is a huge undocumented piece of bloatware. It’s integration into this repo will likely never be super good. See also: https://cirosantilli.com#android

    @@ -40430,7 +40479,7 @@ ISB

    Tested on: 8.1.0_r60.

    -

    28.1.1. Android images read-only

    +

    29.1.1. Android images read-only

    From mount, we can see that some of the mounted images are ro.

    @@ -40587,7 +40636,7 @@ date >/system/a
    -

    28.1.2. Android /data partition

    +

    29.1.2. Android /data partition

    When I install an app like F-Droid, it goes under /data according to:

    @@ -40648,7 +40697,7 @@ date >/system/a
    -

    28.2. Install Android apps

    +

    29.2. Install Android apps

    I don’t know how to download files from the web on Vanilla android, the default browser does not download anything, and there is no wget:

    @@ -40698,7 +40747,7 @@ date >/system/a
    -

    28.3. Android init

    +

    29.3. Android init

    For Linux in general, see: Section 6, “init”.

    @@ -40747,7 +40796,7 @@ import /init.${ro.zygote}.rc
    -

    29. Benchmark this repo

    +

    30. Benchmark this repo

    TODO: didn’t fully port during refactor after 3b0a343647bed577586989fb702b760bd280844a. Reimplementing should not be hard.

    @@ -40776,7 +40825,7 @@ cd -
    -

    29.1. Continuous integration

    +

    30.1. Continuous integration

    We have explored a few Continuous integration solutions.

    @@ -40784,13 +40833,13 @@ cd -

    We haven’t setup any of them yet.

    -

    29.1.1. Travis

    +

    30.1.1. Travis

    We tried to automate it on Travis with .travis.yml but it hits the current 50 minute job timeout: https://travis-ci.org/cirosantilli/linux-kernel-module-cheat/builds/296454523 And I bet it would likely hit a disk maxout either way if it went on.

    -

    29.1.2. CircleCI

    +

    30.1.2. CircleCI

    This setup successfully built gem5 on every commit: .circleci/config.yml

    @@ -40819,9 +40868,9 @@ cd -
    -

    29.2. Benchmark this repo benchmarks

    +

    30.2. Benchmark this repo benchmarks

    -

    29.2.1. Benchmark Linux kernel boot

    +

    30.2.1. Benchmark Linux kernel boot

    Run all kernel boot benchmarks for one arch:

    @@ -40930,7 +40979,7 @@ instructions 124346081
    -
    29.2.1.1. gem5 arm HPI boot takes much longer than aarch64
    +
    30.2.1.1. gem5 arm HPI boot takes much longer than aarch64

    TODO 62f6870e4e0b384c4bd2d514116247e81b241251 takes 33 minutes to finish at 62f6870e4e0b384c4bd2d514116247e81b241251:

    @@ -40956,7 +41005,7 @@ instructions 124346081
    -
    29.2.1.2. gem5 x86_64 DerivO3CPU boot panics
    +
    30.2.1.2. gem5 x86_64 DerivO3CPU boot panics

    https://github.com/cirosantilli-work/gem5-issues/issues/2

    @@ -40968,7 +41017,7 @@ instructions 124346081
    -

    29.2.2. Benchmark emulators on userland executables

    +

    30.2.2. Benchmark emulators on userland executables

    Let’s see how fast our simulators are running some well known or easy to understand userland benchmarks!

    @@ -41287,7 +41336,7 @@ instructions 124346081

    so ~ 110 million instructions / 100 seconds makes ~ 1 MIPS (million instructions per second).

    -

    This experiment also suggests that each loop is about 11 instructions long (110M instructions / 10M loops), which we confirm at Section 31.2, “C busy loop”, bingo!

    +

    This experiment also suggests that each loop is about 11 instructions long (110M instructions / 10M loops), which we confirm at Section 32.2, “C busy loop”, bingo!

    Then for QEMU, we experimentally turn the number of loops up to 10^10 loops (100000 100000), which contains an expected 11 * 10^10 instructions, and the runtime is 00:01:08, so we have 1.1 * 10^11 instruction / 68 seconds ~ 2 * 10^9 = 2000 MIPS!

    @@ -41296,7 +41345,7 @@ instructions 124346081

    We can then repeat the experiment for other gem5 CPUs to see how they compare.

    -
    29.2.2.1. User mode vs full system benchmark
    +
    30.2.2.1. User mode vs full system benchmark

    Let’s see if user mode runs considerably faster than full system or not, ignoring the kernel boot.

    @@ -41304,7 +41353,7 @@ instructions 124346081

    First we build Dhrystone manually statically since dynamic linking is broken in gem5 as explained at: Section 10.7, “gem5 syscall emulation mode”.

    -

    TODO: move this section to our new custom dhrystone setup: Section 21.8.2.1, “Dhrystone”.

    +

    TODO: move this section to our new custom dhrystone setup: Section 22.8.2.1, “Dhrystone”.

    gem5 user mode:

    @@ -41383,7 +41432,7 @@ time \
    -

    29.2.3. Benchmark builds

    +

    30.2.3. Benchmark builds

    The build times are calculated after doing ./configure and make source, which downloads the sources, and basically benchmarks the Internet.

    @@ -41408,7 +41457,7 @@ cat ../linux-kernel-module-cheat-regression/*/build-time.log
    -
    29.2.3.1. Find which Buildroot packages are making the build slow and big
    +
    30.2.3.1. Find which Buildroot packages are making the build slow and big
    ./build-buildroot -- graph-build graph-size graph-depends
    @@ -41419,14 +41468,14 @@ xdg-open graph-size.pdf
    -
    29.2.3.1.1. Buildroot use prebuilt host toolchain
    +
    30.2.3.1.1. Buildroot use prebuilt host toolchain

    The biggest build time hog is always GCC, and it does not look like we can use a precompiled one: https://stackoverflow.com/questions/10833672/buildroot-environment-with-host-toolchain

    -
    29.2.3.2. Benchmark Buildroot build baseline
    +
    30.2.3.2. Benchmark Buildroot build baseline

    This is the minimal build we could expect to get away with.

    @@ -41494,7 +41543,7 @@ xdg-open graph-size.pdf
    -
    29.2.3.3. Benchmark gem5 build
    +
    30.2.3.3. Benchmark gem5 build

    How long it takes to build gem5 itself.

    @@ -41526,7 +41575,7 @@ tail -n+1 ../linux-kernel-module-cheat-regression/*/gem5-bench-build-*.txt

    A profiling of the build has been done at: https://gem5.atlassian.net/browse/GEM5-277 Analysis there showed that d7d9bc240615625141cd6feddbadd392457e49eb (2018-06-17) is also composed of 50% pybind11 and with no obvious time sinks.

    -
    29.2.3.3.1. pybind11 accounts for 50% of gem5 build time
    +
    30.2.3.3.1. pybind11 accounts for 50% of gem5 build time

    https://gem5.atlassian.net/browse/GEM5-366

    @@ -41538,7 +41587,7 @@ tail -n+1 ../linux-kernel-module-cheat-regression/*/gem5-bench-build-*.txt
    -
    29.2.3.3.2. Benchmark gem5 single file change rebuild time
    +
    30.2.3.3.2. Benchmark gem5 single file change rebuild time

    This is the critical development parameter, and is dominated by the link time of huge binaries.

    @@ -41615,9 +41664,9 @@ tail -n+1 ../linux-kernel-module-cheat-regression/*/gem5-bench-build-*.txt
    -

    29.3. Benchmark machines

    +

    30.3. Benchmark machines

    -

    29.3.1. 2017 Lenovo ThinkPad P51

    +

    30.3.1. 2017 Lenovo ThinkPad P51

    Serial number: TYPE 20HH-CTO1WW S/N PF-0V5V5N 17/11

    @@ -41723,7 +41772,7 @@ tail -n+1 ../linux-kernel-module-cheat-regression/*/gem5-bench-build-*.txt
    -
    29.3.1.1. P51 benchmarks
    +
    30.3.1.1. P51 benchmarks

    Dhrystone on Ubuntu 20.04 results at Dhrystone.

    @@ -41731,7 +41780,7 @@ tail -n+1 ../linux-kernel-module-cheat-regression/*/gem5-bench-build-*.txt

    STREAM benchmark on Ubuntu 20.04 results at STREAM benchmark.

    -
    29.3.1.1.1. P51 CoreMark-Pro
    +
    30.3.1.1.1. P51 CoreMark-Pro

    CoreMark-Pro d5b4f2ba7ba31e37a5aa93423831e7d5eb933868 on Ubuntu 20.04 with XCMD="-c$(nproc)":

    @@ -41760,7 +41809,7 @@ CoreMark-PRO 25016.00 6079.70 4.11
    -
    29.3.1.2. P51 maintenance history
    +
    30.3.1.2. P51 maintenance history

    Bought: 2017 for approximately 2400 pounds.

    @@ -41818,7 +41867,7 @@ CoreMark-PRO 25016.00 6079.70 4.11
    -
    29.3.1.3. Intel Core i7-7820HQ CPU
    +
    30.3.1.3. Intel Core i7-7820HQ CPU

    https://ark.intel.com/products/97496/Intel-Core-i7-7820HQ-Processor-8M-Cache-up-to-3-90-GHz- (archive).

    @@ -41900,7 +41949,7 @@ LEVEL4_CACHE_LINESIZE 0
    -
    29.3.1.4. Samsung M471A2K43BB1-CRC 16GB DRAM
    +
    30.3.1.4. Samsung M471A2K43BB1-CRC 16GB DRAM

    Nominal speed: 2400 Mbps

    @@ -41915,7 +41964,7 @@ LEVEL4_CACHE_LINESIZE 0
    -
    29.3.1.5. Samsung MZVLB512HAJQ-000L7 512GB SSD
    +
    30.3.1.5. Samsung MZVLB512HAJQ-000L7 512GB SSD

    PCIe TLC OPAL2.

    @@ -41940,7 +41989,7 @@ LEVEL4_CACHE_LINESIZE 0
    -
    29.3.1.6. Seagate ST1000LM035-1RK1 1TB hard disk
    +
    30.3.1.6. Seagate ST1000LM035-1RK1 1TB hard disk

    1TB.

    @@ -41964,15 +42013,15 @@ LEVEL4_CACHE_LINESIZE 0
    -
    29.3.1.7. NVIDIA Quadro M1200 4GB GDDR5 GPU
    +
    30.3.1.7. NVIDIA Quadro M1200 4GB GDDR5 GPU
    -

    29.4. Benchmark Internets

    +

    30.4. Benchmark Internets

    -

    29.4.1. 38Mbps internet

    +

    30.4.1. 38Mbps internet

    2c12b21b304178a81c9912817b782ead0286d282:

    @@ -41992,7 +42041,7 @@ LEVEL4_CACHE_LINESIZE 0
    -

    29.5. Benchmark this repo bibliography

    +

    30.5. Benchmark this repo bibliography

    gem5:

    @@ -42020,10 +42069,10 @@ LEVEL4_CACHE_LINESIZE 0
    -

    30. RTOS

    +

    31. RTOS

    -

    30.1. Zephyr

    +

    31.1. Zephyr

    @@ -42066,7 +42115,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    30.2. ARM Mbed

    +

    31.2. ARM Mbed

    @@ -42077,13 +42126,13 @@ west build -b qemu_aarch64 samples/hello_world
    -

    31. Compilers

    +

    32. Compilers

    Argh, compilers are boring, let’s learn a bit about them.

    -

    31.1. Prevent statement reordering

    +

    32.1. Prevent statement reordering

    @@ -42095,7 +42144,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    31.2. C busy loop

    +

    32.2. C busy loop

    @@ -42179,10 +42228,10 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32. Computer architecture

    +

    33. Computer architecture

    -

    32.1. Instruction pipelining

    +

    33.1. Instruction pipelining

    In gem5, can be seen on:

    @@ -42197,7 +42246,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.1.1. Classic RISC pipeline

    +

    33.1.1. Classic RISC pipeline

    @@ -42207,7 +42256,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.2. Superscalar processor

    +

    33.2. Superscalar processor

    @@ -42234,7 +42283,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.2.1. Execution unit

    +

    33.2.1. Execution unit

    @@ -42247,7 +42296,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.3. Out-of-order execution

    +

    33.3. Out-of-order execution

    https://en.wikipedia.org/wiki/Out-of-order_execution

    @@ -42264,7 +42313,7 @@ west build -b qemu_aarch64 samples/hello_world

    As mentioned at: https://stackoverflow.com/questions/10074831/what-is-general-difference-between-superscalar-and-ooo-execution it is in theory possible for an out-of-order CPU to not a Superscalar processor, but the combination is so natural (since you can look ahead, you might as well run it!) that it is not super common.

    -

    32.3.1. Speculative execution

    +

    33.3.1. Speculative execution

    https://en.wikipedia.org/wiki/Speculative_execution

    @@ -42282,7 +42331,7 @@ west build -b qemu_aarch64 samples/hello_world
    -
    32.3.1.1. Branch predictor
    +
    33.3.1.1. Branch predictor

    https://en.wikipedia.org/wiki/Branch_predictor

    @@ -42295,20 +42344,20 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.3.2. Re-order buffer

    +

    33.3.2. Re-order buffer

    https://en.wikipedia.org/wiki/Re-order_buffer

    -

    32.3.3. Register renaming

    +

    33.3.3. Register renaming

    https://en.wikipedia.org/wiki/Register_renaming

    -

    32.4. Instruction level parallelism

    +

    33.4. Instruction level parallelism

    https://en.wikipedia.org/wiki/Instruction-level_parallelism

    @@ -42327,7 +42376,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.5. Hardware threads

    +

    33.5. Hardware threads

    Intel name: "Hyperthreading"

    @@ -42377,7 +42426,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.6. Cache coherence

    +

    33.6. Cache coherence

    https://en.wikipedia.org/wiki/Cache_coherence

    @@ -42419,7 +42468,7 @@ west build -b qemu_aarch64 samples/hello_world

    Even if caches are coherent, this is still not enough to avoid data race conditions, because this does not enforce atomicity of read modify write sequences. This is for example shown at: Detailed gem5 analysis of how data races happen.

    -

    32.6.1. Memory consistency

    +

    33.6.1. Memory consistency

    According to http://www.inf.ed.ac.uk/teaching/courses/pa/Notes/lecture07-sc.pdf "memory consistency" is about ordering requirements of different memory addresses.

    @@ -42427,14 +42476,14 @@ west build -b qemu_aarch64 samples/hello_world

    This is represented explicitly in C++ for example C++ std::memory_order.

    -
    32.6.1.1. Sequential Consistency
    +
    33.6.1.1. Sequential Consistency

    According to http://www.inf.ed.ac.uk/teaching/courses/pa/Notes/lecture07-sc.pdf, the strongest possible consistency, everything nicely ordered as you’d expect.

    -

    32.6.2. Can caches snoop data from other caches?

    +

    33.6.2. Can caches snoop data from other caches?

    Either they can snoop only control, or both control and data can be snooped.

    @@ -42449,7 +42498,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.6.3. VI cache coherence protocol

    +

    33.6.3. VI cache coherence protocol

    Mentioned at:

    @@ -42696,7 +42745,7 @@ west build -b qemu_aarch64 samples/hello_world
    -

    32.6.4. MSI cache coherence protocol

    +

    33.6.4. MSI cache coherence protocol

    https://en.wikipedia.org/wiki/MSI_protocol

    @@ -43008,7 +43057,7 @@ CACHE2 S nyy

    TODO gem5 concrete example.

    -
    32.6.4.1. MSI cache coherence protocol with transient states
    +
    33.6.4.1. MSI cache coherence protocol with transient states

    TODO understand well why those are needed.

    @@ -43028,7 +43077,7 @@ CACHE2 S nyy
    -

    32.6.5. MESI cache coherence protocol

    +

    33.6.5. MESI cache coherence protocol

    https://en.wikipedia.org/wiki/MESI_protocol

    @@ -43088,7 +43137,7 @@ CACHE2 S nyy
    -

    32.6.6. MOSI cache coherence protocol

    +

    33.6.6. MOSI cache coherence protocol

    https://en.wikipedia.org/wiki/MOSI_protocol The critical MSI vs MOSI section was a bit bogus though: https://en.wikipedia.org/w/index.php?title=MOSI_protocol&oldid=895443023 but I edited it :-)

    @@ -43148,7 +43197,7 @@ CACHE2 S nyy
    -

    32.6.7. MOESI cache coherence protocol

    +

    33.6.7. MOESI cache coherence protocol

    https://en.wikipedia.org/wiki/MOESI_protocol

    @@ -43156,10 +43205,10 @@ CACHE2 S nyy

    MESI cache coherence protocol + MOSI cache coherence protocol, not much else to it!

    -

    In gem5 9fc9c67b4242c03f165951775be5cd0812f2a705, MOESI is the default cache coherency protocol of the classic memory system as shown at Section 19.20.4.3.1, “What is the coherency protocol implemented by the classic cache system in gem5?”.

    +

    In gem5 9fc9c67b4242c03f165951775be5cd0812f2a705, MOESI is the default cache coherency protocol of the classic memory system as shown at Section 19.21.4.3.1, “What is the coherency protocol implemented by the classic cache system in gem5?”.

    -

    A good an simple example showing several MOESI transitions in the classic memory model can be seen at: Section 19.20.4.4, “gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs”.

    +

    A good an simple example showing several MOESI transitions in the classic memory model can be seen at: Section 19.21.4.4, “gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis with caches and multiple CPUs”.

    gem5 12c917de54145d2d50260035ba7fa614e25317a3 has several Ruby MOESI models implemented: MOESI_AMD_Base, MOESI_CMP_directory, MOESI_CMP_token and MOESI_hammer.

    @@ -43169,10 +43218,10 @@ CACHE2 S nyy
    -

    33. About this repo

    +

    34. About this repo

    -

    33.1. Supported hosts

    +

    34.1. Supported hosts

    The host requirements depend a lot on which examples you want to run.

    @@ -43221,9 +43270,9 @@ CACHE2 S nyy
    -

    33.2. Common build issues

    +

    34.2. Common build issues

    -

    33.2.1. You must put some 'source' URIs in your sources.list

    +

    34.2.1. You must put some 'source' URIs in your sources.list

    If ./build --download-dependencies fails with:

    @@ -43237,7 +43286,7 @@ CACHE2 S nyy
    -

    33.2.2. Build from downloaded source zip files

    +

    34.2.2. Build from downloaded source zip files

    It does not work if you just download the .zip with the sources for this repository from GitHub because we use Git submodules, you must clone this repo.

    @@ -43247,7 +43296,7 @@ CACHE2 S nyy
    -

    33.3. Run command after boot

    +

    34.3. Run command after boot

    If you just want to run a command after boot ends without thinking much about it, just use the --eval-after option, e.g.:

    @@ -43264,7 +43313,7 @@ CACHE2 S nyy
    -

    33.4. Default command line arguments

    +

    34.4. Default command line arguments

    It gets annoying to retype --arch aarch64 for every single command, or to remember --config setups.

    @@ -43309,12 +43358,12 @@ CACHE2 S nyy
    -

    33.5. Documentation

    +

    34.5. Documentation

    To learn how to build the documentation see: Section 1.10, “Build the documentation”.

    -

    33.5.1. Documentation verification

    +

    34.5.1. Documentation verification

    When running build-doc, we do the following checks:

    @@ -43335,7 +43384,7 @@ CACHE2 S nyy

    The scripts prints what you have to fix and exits with an error status if there are any errors.

    - + @@ -43358,7 +43407,7 @@ CACHE2 S nyy
    -
    33.5.1.2. asciidoctor/extract-header-ids
    +
    34.5.1.2. asciidoctor/extract-header-ids

    Documentation for asciidoctor/extract-header-ids

    @@ -43403,7 +43452,7 @@ explicitly-given
    - +

    The Asciidoctor extension scripts:

    @@ -43431,7 +43480,7 @@ explicitly-given
    -

    33.6.1. GitHub pages

    +

    34.6.1. GitHub pages

    As mentioned before the TOC, we have to push this README to GitHub pages due to: https://github.com/isaacs/github/issues/1610

    @@ -43481,7 +43530,7 @@ explicitly-given
    -

    33.7. Clean the build

    +

    34.7. Clean the build

    You did something crazy, and nothing seems to work anymore?

    @@ -43545,7 +43594,7 @@ ls "$(./getvar buildroot_build_dir)"
    -

    33.8. Custom build directory

    +

    34.8. Custom build directory

    For now there is no way to change the build directory from out/ (resp. out.docker for <<docker>.) to something else.

    @@ -43560,7 +43609,7 @@ ln -s out /mnt/hd/linux-kernel-module-cheat-out
    -

    33.9. ccache

    +

    34.9. ccache

    ccache might save you a lot of re-build when you decide to Clean the build or create a new build variant.

    @@ -43640,7 +43689,7 @@ export CCACHE_MAXSIZE="20G"
    -

    33.10. getvar

    +

    34.10. getvar

    The getvar helper script can print the values of internal LKMC variables.

    @@ -43678,7 +43727,7 @@ export CCACHE_MAXSIZE="20G"

    For this reason, we use it in particular often in this README to reduce the need for refactoring.

    -

    33.10.1. run-toolchain

    +

    34.10.1. run-toolchain

    While you could just manually find/learn the path to toolchain tools, e.g. in LKMC b15a0e455d691afa49f3b813ad9b09394dfb02b7 they are:

    @@ -43725,7 +43774,7 @@ export CCACHE_MAXSIZE="20G"
    -
    33.10.1.1. disas
    +
    34.10.1.1. disas

    Since disassembly of a single function of a LKMC executable with GDB is such a common use case for run-toolchain via https://stackoverflow.com/questions/22769246/how-to-disassemble-one-single-function-using-objdump, we have this shortcut for it.

    @@ -43757,7 +43806,7 @@ export CCACHE_MAXSIZE="20G"
    -

    33.11. Rebuild Buildroot while running

    +

    34.11. Rebuild Buildroot while running

    It is not possible to rebuild the root filesystem while running QEMU because QEMU holds the file qcow2 file:

    @@ -43768,7 +43817,7 @@ export CCACHE_MAXSIZE="20G"
    -

    33.12. Simultaneous runs

    +

    34.12. Simultaneous runs

    When doing long simulations sweeping across multiple system parameters, it becomes fundamental to do multiple simulations in parallel.

    @@ -43864,7 +43913,7 @@ less "$(./getvar --arch aarch64 --emulator gem5 --run-id 1 termout_file)"
    -

    To run multiple gem5 checkouts, see: Section 33.13.3.1, “gem5 worktree”.

    +

    To run multiple gem5 checkouts, see: Section 34.13.3.1, “gem5 worktree”.

    Implementation note: we create multiple namespaces for two things:

    @@ -43903,7 +43952,7 @@ less "$(./getvar --arch aarch64 --emulator gem5 --run-id 1 termout_file)"
    -

    33.13. Build variants

    +

    34.13. Build variants

    It often happens that you are comparing two versions of the build, a good and a bad one, and trying to figure out why the bad one is bad.

    @@ -43911,7 +43960,7 @@ less "$(./getvar --arch aarch64 --emulator gem5 --run-id 1 termout_file)"

    Our build variants system allows you to keep multiple built versions of all major components, so that you can easily switching between running one or the other.

    -

    33.13.1. Linux kernel build variants

    +

    34.13.1. Linux kernel build variants

    If you want to keep two builds around, one for the latest Linux version, and the other for Linux v4.16:

    @@ -43947,11 +43996,11 @@ git -C "$(./getvar linux_source_dir)" checkout -
    -

    To run both kernels simultaneously, one on each QEMU instance, see: Section 33.12, “Simultaneous runs”.

    +

    To run both kernels simultaneously, one on each QEMU instance, see: Section 34.12, “Simultaneous runs”.

    -

    33.13.2. QEMU build variants

    +

    34.13.2. QEMU build variants

    Analogous to the Linux kernel build variants but with the --qemu-build-id option instead:

    @@ -43967,7 +44016,7 @@ git -C "$(./getvar qemu_source_dir)" checkout -
    -

    33.13.3. gem5 build variants

    +

    34.13.3. gem5 build variants

    Analogous to the Linux kernel build variants but with the --gem5-build-id option instead:

    @@ -43998,7 +44047,7 @@ git -C "$(./getvar gem5_source_dir)" checkout some-branch

    Therefore, you can’t forget to checkout to the sources to that of the corresponding build before running, unless you explicitly tell gem5 to use a non-default source tree with gem5 worktree. This becomes inevitable when you want to launch multiple simultaneous runs at different checkouts.

    -
    33.13.3.1. gem5 worktree
    +
    34.13.3.1. gem5 worktree

    --gem5-build-id goes a long way, but if you want to seamlessly switch between two gem5 tress without checking out multiple times, then --gem5-worktree is for you.

    @@ -44051,7 +44100,7 @@ cd -
    -
    33.13.3.2. gem5 private source trees
    +
    34.13.3.2. gem5 private source trees

    Suppose that you are working on a private fork of gem5, but you want to use this repository to develop it as well.

    @@ -44095,7 +44144,7 @@ gem5_internal="$(pwd)/gem5-internal"
    -

    33.13.4. Buildroot build variants

    +

    34.13.4. Buildroot build variants

    Allows you to have multiple versions of the GCC toolchain or root filesystem.

    @@ -44115,7 +44164,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.14. Optimization level of a build

    +

    34.14. Optimization level of a build

    The --optimization-level option is available on all build scripts and sets the given GCC `-`O optimization level where it has been implemented for guest binaries.

    @@ -44142,9 +44191,9 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.15. Directory structure

    +

    34.15. Directory structure

    -

    33.15.1. lkmc directory

    +

    34.15.1. lkmc directory

    lkmc/ contains sources and headers that are shared across kernel modules, userland and baremetal examples.

    @@ -44155,7 +44204,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -

    Another option would have been to name it as includes/lkmc, but that would make paths longer, and we might want to store source code in that directory as well in the future.

    -
    33.15.1.1. Userland objects vs header-only
    +
    34.15.1.1. Userland objects vs header-only

    When factoring out functionality across userland examples, there are two main options:

    @@ -44214,7 +44263,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.15.2. buildroot_packages directory

    +

    34.15.2. buildroot_packages directory

    Source: buildroot_packages/.

    @@ -44263,7 +44312,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -

    A custom build script can give you more flexibility: e.g. the package can be made work with other root filesystems more easily, have better 9P support, and rebuild faster as it evades some Buildroot boilerplate.

    -
    33.15.2.1. kernel_modules buildroot package
    +
    34.15.2.1. kernel_modules buildroot package

    Source: buildroot_packages/kernel_modules/

    @@ -44310,9 +44359,9 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.15.3. patches directory

    +

    34.15.3. patches directory

    -
    33.15.3.1. patches/global directory
    +
    34.15.3.1. patches/global directory

    Has the following structure:

    @@ -44329,7 +44378,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -
    33.15.3.2. patches/manual directory
    +
    34.15.3.2. patches/manual directory

    Patches in this directory are never applied automatically: it is up to users to manually apply them before usage following the instructions in this documentation.

    @@ -44339,7 +44388,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.15.4. rootfs_overlay

    +

    34.15.4. rootfs_overlay

    Source: rootfs_overlay.

    @@ -44386,7 +44435,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -

    This way you can just hack away the scripts and try them out immediately without any further operations.

    -
    33.15.4.1. out_rootfs_overlay_dir
    +
    34.15.4.1. out_rootfs_overlay_dir

    This path can be found with:

    @@ -44420,7 +44469,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.15.5. lkmc.c

    +

    34.15.5. lkmc.c

    The files:

    @@ -44450,7 +44499,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.15.6. lkmc_home

    +

    34.15.6. lkmc_home

    lkmc_home refers to the target base directory in which we put all our custom built stuff, such as userland executables and kernel modules.

    @@ -44483,7 +44532,7 @@ git -C "$(./getvar buildroot_source_dir)" checkout -
    -

    33.15.7. path_properties.py

    +

    34.15.7. path_properties.py

    In order to build and run each userland and baremetal example properly, we need per-file metadata such as compiler flags and required number of cores.

    @@ -44546,7 +44595,7 @@ baremetal=True
    -

    33.15.8. rand_check.out

    +

    34.15.8. rand_check.out

    Print out several parameters that normally change randomly from boot to boot:

    @@ -44574,9 +44623,9 @@ baremetal=True
    -

    33.16. Test this repo

    +

    34.16. Test this repo

    -

    33.16.1. Automated tests

    +

    34.16.1. Automated tests

    Run almost all tests:

    @@ -44632,7 +44681,7 @@ echo $?

    test does not all possible tests, because there are too many possible variations and that would take forever. The rationale is the same as for ./build all and is explained in ./build --help.

    -
    33.16.1.1. Test arch and emulator selection
    +
    34.16.1.1. Test arch and emulator selection

    You can select multiple archs and emulators of interest, as for an other command, with:

    @@ -44665,7 +44714,7 @@ echo $?
    -
    33.16.1.2. Quit on fail
    +
    34.16.1.2. Quit on fail

    By default, continue running even after the first failure happens, and they show a summary at the end.

    @@ -44679,7 +44728,7 @@ echo $?
    -
    33.16.1.3. Test userland in full system
    +
    34.16.1.3. Test userland in full system

    TODO: we really need a mechanism to automatically generate the test list automatically e.g. based on path_properties.py, currently there are many tests missing, and we have to add everything manually which is very annoying.

    @@ -44708,7 +44757,7 @@ echo $?
    -
    33.16.1.4. GDB tests
    +
    34.16.1.4. GDB tests

    We have some pexpect automated tests for GDB for both userland and baremetal programs!

    @@ -44781,7 +44830,7 @@ echo $?
    -
    33.16.1.5. Magic failure string
    +
    34.16.1.5. Magic failure string

    We do not know of any way to set the emulator exit status in QEMU arm full system.

    @@ -44884,9 +44933,9 @@ echo $?
    -

    33.16.2. Non-automated tests

    +

    34.16.2. Non-automated tests

    -
    33.16.2.1. Test GDB Linux kernel
    +
    34.16.2.1. Test GDB Linux kernel

    For the Linux kernel, do the following manual tests for now.

    @@ -44924,7 +44973,7 @@ echo $?
    -
    33.16.2.2. Test the Internet
    +
    34.16.2.2. Test the Internet

    You should also test that the Internet works:

    @@ -44935,7 +44984,7 @@ echo $?
    -
    33.16.2.3. CLI script tests
    +
    34.16.2.3. CLI script tests

    build-userland and test-executables have a wide variety of target selection modes, and it was hard to keep them all working without some tests:

    @@ -44953,7 +45002,7 @@ echo $?
    -

    33.17. Bisection

    +

    34.17. Bisection

    When updating the Linux kernel, QEMU and gem5, things sometimes break.

    @@ -45009,7 +45058,7 @@ git submodule update
    -

    33.18. Update a forked submodule

    +

    34.18. Update a forked submodule

    This is a template update procedure for submodules for which we have some patches on on top of mainline.

    @@ -45038,9 +45087,9 @@ git commit -m "linux: update to ${next_mainline_revision}"
    -

    33.19. Release

    +

    34.19. Release

    -

    33.19.1. Release procedure

    +

    34.19.1. Release procedure

    Ensure that the Automated tests are passing on a clean build:

    @@ -45051,7 +45100,7 @@ git commit -m "linux: update to ${next_mainline_revision}"
    -

    The ./build-test command builds a superset of what will be downloaded which also tests other things we would like to be working on the release. For the minimal build to generate the files to be uploaded, see: Section 33.19.2, “release-zip”

    +

    The ./build-test command builds a superset of what will be downloaded which also tests other things we would like to be working on the release. For the minimal build to generate the files to be uploaded, see: Section 34.19.2, “release-zip”

    The clean build is necessary as it generates clean images since it is not possible to remove Buildroot packages

    @@ -45121,7 +45170,7 @@ git push --follow-tags
    -

    33.19.2. release-zip

    +

    34.19.2. release-zip

    Create a zip containing all files required for Prebuilt setup:

    @@ -45146,7 +45195,7 @@ git push --follow-tags
    -

    33.19.3. release-upload

    +

    34.19.3. release-upload

    After:

    @@ -45194,9 +45243,9 @@ git push --follow-tags
    -

    33.20. Design rationale

    +

    34.20. Design rationale

    -

    33.20.1. Design goals

    +

    34.20.1. Design goals

    This project was created to help me understand, modify and test low level system components by using system simulators.

    @@ -45272,7 +45321,7 @@ git push --follow-tags
    -

    33.20.2. Setup trade-offs

    +

    34.20.2. Setup trade-offs

    The trade-offs between the different setups are basically a balance between:

    @@ -45297,13 +45346,13 @@ git push --follow-tags

    compatibility: how likely is is that all the components will work well together: emulator, compiler, kernel, standard library, …​

  • -

    guest software availability: how wide is your choice of easily installed guest software packages? See also: Section 33.20.4, “Linux distro choice”

    +

    guest software availability: how wide is your choice of easily installed guest software packages? See also: Section 34.20.4, “Linux distro choice”

  • -

    33.20.3. Resource tradeoff guidelines

    +

    34.20.3. Resource tradeoff guidelines

    Choosing which features go into our default builds means making tradeoffs, here are our guidelines:

    @@ -45344,11 +45393,11 @@ git push --follow-tags
    -

    In order to learn how to measure some of those aspects, see: Section 29, “Benchmark this repo”.

    +

    In order to learn how to measure some of those aspects, see: Section 30, “Benchmark this repo”.

    -

    33.20.4. Linux distro choice

    +

    34.20.4. Linux distro choice

    We haven’t found the ultimate distro yet, here is a summary table of trade-offs that we care about: Table 8, “Comparison of Linux distros for usage in this repository”.

    @@ -45451,9 +45500,9 @@ git push --follow-tags
    -

    33.21. Soft topics

    +

    34.21. Soft topics

    -

    33.21.1. Fairy tale

    +

    34.21.1. Fairy tale

    @@ -45491,7 +45540,7 @@ git push --follow-tags