Introduction to Linux interfaces for virtual networking

Introduction to Linux interfaces for virtual networking | Red Hat Developer

Ssh-based ping: measure character-echo latency and bandwidth for an interactive ssh session

spook/sshping: ssh-based ping: measure character echo latency and bandwidth

Another KVM

From KVM switch - Wikipedia

A KVM switch is a hardware device that allows a user to control multiple computers from one or more sets of keyboards, video monitors, and mice.

C 语言内嵌汇编

内联汇编很可怕吗?看完这篇文章,终结它! - 1024搜-程序员专属的搜索引擎

内存地址以 1 byte 为一个基本单元的原因

为什么内存地址是以字节为单位? - 简书

MSR

Reading and writing to these registers is handled by the rdmsr and wrmsr instructions, respectively. As these are privileged instructions, they can be executed only by the operating system.

也就是说 rdmsr 和 wrmsr 是在硬件上就支持的指令,这个是特权指令,所以只能由操作系统来执行。

Some MSRs are read-only, such as IA32_TME_CAPABILITY.

Some MSRs can be locked, such as:

This MSR is used to lock the following MSRs. Any write to the following MSRs will be ignored after they are locked. The lock is reset when CPU is reset.

in the MKTME spec.

BIOS is a type of firmware used during the booting process

The origin of the word firmware is a mid-point between hardware and software - software embedded on hardware. It refers to software that it is stored in non-volatile memory (such as ROM, EEPROM or Flash memory) on a hardware device, and is used by the device itself.

BIOS 所在的 chip 是 EEPROM,和 Disk Flash 都是一样的,所以虽然叫 ROM,也是可写的,这就是为什么 BIOS 可以被更新。

terminology - What exactly is microcode and how does it differ from firmware? - Super User

What is the difference between BIOS and firmware? - Super User

PIO (Programmed input–output), Interrupt I/O and DMA

Three ways to transfer data:

  • PIO:
    • PMIO: CPU 直接使用 IN、OUT 指令就可以访问到对应的外围设备。
    • MMIO: CPU 直接通过普通指令。
  • Interrupt I/O: 中断控制器会监控设备是否准备好进行传输,并发中断给 CPU,从而使其停下处理传输。
    • PMIO: …
    • MMIO: …
  • DMA:

In PIO, The CPU stays in the loop to know if the device is ready for transfer and has to continuously monitor the peripheral device. In Interrupt I/O, there is no need for the CPU to stay in the loop as the interrupt command interrupts the CPU when the device is ready for data transfer. Thus the performance of the system is enhanced to some extent. The same shortage for both:

  • I/O 传输速率受 CPU 的速度限制
  • 每次 I/O 传输必须执行许多指令

Difference between Programmed and Interrupt Initiated I/O - GeeksforGeeks

Ioctl

ioctl()函数详解_yulongfei的专栏-CSDN博客_ioctl函数的作用

Motivation: Talking To Device Files

Device driver

When a calling program invokes a routine in the driver, the driver issues commands to the device (drives it). Once the device sends data back to the driver, the driver may invoke routines in the original calling program.

The task of writing drivers thus usually falls to software engineers or computer engineers who work for hardware-development companies. This is because they have better information than most outsiders about the design of their hardware. Moreover, it was traditionally considered in the hardware manufacturer's interest to guarantee that their clients can use their hardware in an optimum way.

Device driver - Wikipedia

Stack Overflow, Stack Exchange, Area 51: What's the difference in a nutshell?

Stack Overflow is a Q&A site for programmers. This is where you can ask and answer questions about programming.

Stack Overflow became so popular that users wanted to create Stack Overflow-like sites about other subjects. Stack Exchange is the name of the network of sites which users have created. You can see a list of the sites created here.

Meaning of rc like 2.6.37-rc5

Meaning of "rc5" in "linux kernel 2.6.37-rc5" - Unix & Linux Stack Exchange

Clone 时默认只 clone 主分支

如果想要以 --depth 1 的设置克隆所选分支,使用:

git clone -b <branch-name### --depth 1 <url>

这个方法也适用于 clone 一个 tag,只需要把 branch-name 换成 tag-name 就行了。

Does the major version number (4.x vs 5.x) in Linux release mean anything?

No. The major version number is incremented when the number after the dot starts looking "too big." There is literally no other reason.

Does the odd-even number in Linux release still mean anything?

A long time ago Linux used a system where odd numbers after the first dot indicated pre-release, development kernels (e.g. 2.1, 2.3, 2.5). This scheme was abandoned after the release of kernel 2.6 and these days pre-release kernels are indicated with "-rc".

The Linux Kernel Archives - Releases

QEMU release cadence

For example:

7.2.0: stable release
8.0.0-rc0: one week before, soft freeze; Tag rc0 when hard freeze
8.0.0-rc1
8.0.0-rc2
8.0.0-rc3
...
8.0.0: stable release
...

Soft freeze: The soft feature freeze is the beginning of the stabilization phase of QEMU's development process. By the date of the soft feature freeze, maintainers must have sent their pull request to the mailing list. This means that features, and in particular non-trivial ones, must have been merged into maintainer trees before the soft freeze date.

Hard freeze: After the hard feature freeze, the master branch in git is no longer open for general development. Only bug fixes will be accepted until the next release.

Planning - QEMU

Linux kernel release cadence

The release which happens at the end of the merge window will be called 5.6-rc1. The -rc1 release is the signal that the time to merge new features has passed, and that the time to stabilize the next kernel has begun.

During the merge window, the maintainers instead focus on following the upstream changes, fixing merge window fallout, collecting bug fixes, and allowing themselves a breath. Please respect that.

Three main channel:

  • mainline
  • stable
  • longterm: Backporting bugfixes for older kernel trees. Only important bugfixes are applied to such kernels and they don't usually see very frequent releases

Pre-patch or RC (release candidate) are mainline kernel pre-releases, maintained and released by Linus Torvalds.

New mainline kernels are released every 9-10 weeks. After each mainline kernel is released, it is considered "stable."

The Linux Kernel Archives - Releases

Remove uninstall kernel

software uninstall - How can I remove compiled kernel? - Ask Ubuntu

Git conflict

If conflicts occur, just edit the file to resolve them, it contains the conflict information.

Markdown & neovim

This plugin can jump to any headings in markdown, LaTeX…

crispgm/telescope-heading.nvim: An extension for telescope.nvim that allows you to switch between headings

Revert multiple commits

How to revert multiple git commits? - Stack Overflow

Git head

When working with Git, only one branch can be checked out at a time - and this is what's called the "HEAD" branch.

It is possible for HEAD to refer to a specific revision that is not associated with a branch name. This situation is called a detached HEAD. This happens when you checkout a specific commit, tag, or remote branch.

$ cat .git/HEAD
ref: refs/heads/master

What's the difference between HEAD, working tree and index, in Git?

What's the difference between HEAD, working tree and index, in Git? - Stack Overflow

Translation unit in C

A translation unit roughly consists of a source file after it has been processed by the C preprocessor, meaning that header files listed in #include directives are literally included, sections of code within #ifndef may be included, and macros have been expanded.

Translation unit (programming) - Wikipedia

Inline

The inline specifier (like the register storage class) is only a compiler hint, and the compiler is free to completely ignore it.

c - What's the difference between "static" and "static inline" function? - Stack Overflow

What is external linkage and internal linkage?

c++ - What is external linkage and internal linkage? - Stack Overflow

Static inline

详解static inline关键字 - 知乎

FYI

FYI (pronounced EFF-WAI-AI ) is an abbreviation for "For your information," and is often used in forwarding E-mail or printed material to colleagues or friends. It usually means that information is simply being shared and that no immediate action is required or expected.

What is for your information (FYI)? - Definition from WhatIs.com

English name

Comma in English name

However, in English, when a comma is between two names, it usually indicates "Last name, First Name."

do not put a comma between the first and last name [#2926471] Drupal.org

Linux kernel selftests

Linux Kernel Selftests — The Linux Kernel documentation

Global gitignore

Global gitignore (github.com)

Bear

rizsotto/Bear: Bear is a tool that generates a compilation database for clang tooling.

Bear's output is empty

Troubleshooting · rizsotto/Bear Wiki (github.com)

Compiledb

nickdiego/compiledb: Tool for generating Clang's JSON Compilation Database files for make-based build systems. (github.com)

TLB shoot-down

A quick example:

  1. You have some memory shared by all of the processors in your system.
  2. One of your processors restricts access to a page of that shared memory.
  3. Now, all of the processors have to flush their TLBs, so that the ones that were allowed to access that page can't do so any more.

The actions of one processor causing the TLBs to be flushed on other processors is what is called a TLB shootdown.

caching - What is TLB shootdown? - Stack Overflow

Email clients info for Linux

Email clients info for Linux — The Linux Kernel documentation

Working process to contribute to Linux community

Working with the kernel development community — The Linux Kernel documentation

A guide to the Kernel Development Process

A guide to the Kernel Development Process — The Linux Kernel documentation

Git patch

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository.

Git diff、git diff head、git diff --cached 三者详细区分

git diff、git diff head、git diff --cached三者详细区分 - 沉着前进 - 博客园

Non-merge commit

git - What is a non-merge commit? - Software Engineering Stack Exchange

Therefore, if you're trying to express the amount of work that went into a specific branch, you should measure the commits except merge commits, since they are not a measure of development on the actual branch.

Git am

apply mail.

Posting patches

5. Posting patches — The Linux Kernel documentation

When basing on mainline, start with a well-known release point - a stable or -rc release - rather than branching off the mainline at an arbitrary spot.

Patch series / patchset

A patchset is a series of patches that are related, that describe changes to add new functionality, or fix bugs in a specific driver. The idea of a patchset is to break changes into a logical series.

PatchPhilosophy - Linux Kernel Newbies

How to wirte a patch reference

PatchPhilosophy - Linux Kernel Newbies

5. Posting patches — The Linux Kernel documentation

Why kernel development still uses email

Why kernel development still uses email [LWN.net]

[PATCH v6 0/7] KVM: PKS Virtualization support
[PATCH v5 0/7] KVM: PKS Virtualization support

CR0.WP

When set, the CPU can't write to read-only pages when privilege level is 0.

Clangd 是 llvm 的一个子项目

llvm-project/clang-tools-extra/clangd at main · llvm/llvm-project (github.com)

you can use clangd as a lsp for kernel development, remember to use

./scripts/clang-tools/gen_compile_commands.py

to generate compile_commands.json.

But how does this scripts know how to generate compile_commands.json?

The answer is, kernel has it's own config/build system named Kconfig/kbuild, during the build process, it will output a .<target>.cmd file in makefile syntax, which records the command line and all the prerequisites (including the configuration) for a target. Those .<target>.cmd files are used by gen_compile_commands.py to generate compile_commands.json, so it is neccessary to first make then invoke gen_compile_commands.py.

Kernel panic

Kernel panic - Wikipedia

Dark reader

enable dark mode for all websites.

include/uapi In kernel source tree

networking - What's in include/uapi of kernel source project - Stack Overflow

Scripts: add a tool to produce a compile_commands.json file

kernel/git/torvalds/linux.git - Linux kernel source tree

WARN_ON_ONCE() Kernel

仅仅打印一次消息。Once 不是一旦的一次,而是一次的意思。

Typora 中文站,访问不了可以在这里下载

Typora 官方中文站 (typoraio.cn)

What is unity build?

https://github.com/clangd/clangd/issues/45#issuecomment-640792276

Double hash in C

c++ - What does ## (double hash) do in a preprocessor directive? - Stack Overflow

Device and driver in Linux

我们可以将内核中收集和记录信息的那一部分编写成一个字符设备驱动程序。虽然没有实际对应的物理设备,但这并没什么问题:Linux 的设备驱动程序本来就是一个软件抽象,它可以结合硬件提供服务,也完全可以作为纯软件提供服务。

linux内核空间与用户空间信息交互方法

What is the difference between kernel drivers and kernel modules?

A driver may be built statically into the kernel file on disk. A driver may also be built as a kernel module so that it can be dynamically loaded later. (And then maybe unloaded.)

Standard practice is to build drivers as kernel modules where possible, rather than link them statically to the kernel, since that gives more flexibility.

linux - What is the difference between kernel drivers and kernel modules? - Unix & Linux Stack Exchange

What's the difference between insmod and modprobe?

modprobe is the intelligent version of insmod. insmod simply adds a module where modprobe looks for any dependency and loads them.

linux - What's the difference between insmod and modprobe? - Stack Overflow

Kconfig

A language for building kernel.

Kconfig Language — The Linux Kernel documentation

Exploring the Linux kernel: The secrets of Kconfig/kbuild Opensource.com

NMI

硬件中断当中有一部分是不可屏蔽的,也就是 NMI,性能检测数据、不可逆的硬件错误等等都是使用 NMI 来实现的。

In computing, a non-maskable interrupt (NMI) is a hardware interrupt that standard interrupt-masking techniques in the system cannot ignore. It typically occurs to signal attention for non-recoverable hardware errors. Some NMIs may be masked, but only by using proprietary methods specific to the particular NMI.

An NMI is often used when response time is critical or when an interrupt should never be disabled during normal system operation. Such uses include reporting non-recoverable hardware errors, system debugging and profiling, and handling of special cases like system resets.

Allocating variables on the stack in x86 assembly. rbp and rsp vs esp and ebp.

Allocating variables on the stack in x86 assembly. rbp and rsp vs esp and ebp - Stack Overflow

Root operation / non-root operation / root mode / non-root mode / vmexit / vmentry


Ring 0 Ring 1-3
root mode KVM QEMU
non-root mode Guest Kernel Guest Userspace

我们可以从 guest userspace 直接 exit 到 root mode 吗?

应该是可以的。Note that sensitive instructions are not the privileged ones. So an instruction like sgdt, which is legal in user space (ring 3), causes a VM exit in VMX non-root mode because the VMM has to fake the guest GDT.

x86 - In full virtualization context, what happens on guest OS system calls? - Stack Overflow

VMExit 会引起 Ring 的变化吗?比如从 guest kernel space 直接 exit 到了 host userspace,或者从 guest user space 直接 exit 到了 host 的 kernel space?

  Ring 0 -> Ring 3 Ring 3 -> Ring 0
VMEntry 允许,因为比如 sgdt 指令的存在,可以从 Guest userspace 直接 exit 不允许,因为 VMLAUNCH/VMRESUME 是只能运行在 Ring 0 特权级
VMExit 不允许,因为我们不会从 Ring 3 VMEntry 进来,所以我们不会 exit 到 Ring 3 允许,因为 sgdt 指令的存在

除此之外,Ring 0 到 Ring 0 不管 entry 还是 exit 都是允许的;Ring 3 到 Ring 3 不管 entry 还是 exit 都是不允许的。

Sadly, WSL2 doesn't have network connection when there is a VPN on the host. we can use following tool to tackle this…

sakai135/wsl-vpnkit: Provide network connectivity to WSL 2 when blocked by VPN

here is the related issue:

WSL2 , problem with network connection when VPN used (PulseSecure) · Issue #5068 · microsoft/WSL

Low latency ssh

sshuttle/sshuttle: Transparent proxy server that works as a poor man's VPN. Forwards over ssh. Doesn't require admin. Works with Linux and MacOS. Supports DNS tunneling.

Apt needs additional proxy setting

Acquire::http::Proxy "<proxy>";
Acquire::https::Proxy "<proxy>";

Configure proxy for APT? - Ask Ubuntu

or

sudo -E update

How to list all kernel modules loaded?

5.2.21. /proc/modules Red Hat Enterprise Linux 4 Red Hat Customer Portal

Two leading scores in Linux kernel

c - What does __init mean in the Linux kernel code? - Stack Overflow

What's the purpose of atomic_t

what is the purpose of atomic_t? - C / C++

What is the difference between ioctl(), unlocked_ioctl() and compat_ioctl()?

linux - What is the difference between ioctl(), unlocked_ioctl() and compat_ioctl()? - Unix & Linux Stack Exchange

File operations

Linux 字符设备驱动结构(四)—— file_operations 结构体知识解析_zqixiao_09的博客-CSDN博客_kiocb结构体

Big kernel lock (BKL)

Giant lock - Wikipedia

Motivation for IOCTL

The system call ioctl() is provided for device-specific custom commands (such as format, reset and shutdown) that are not provided by standard system calls such as read(), write() and mmap().

ioctl() 起初是用来管理设备用的,userspace 发起 ioctl(),kernel 里对应的 driver 响应这个 ioctl,不难看出,相比于其他 well-defined 的 syscall 比如 read() 等等,ioctl() 是一个很 implementation-specific 的东西,因此,我们可以扩展一下 ioctl() 的语义,因为 ioctl() 的参数其实是一个 fd,我们打开设备会有 fd,其不止可以用来控制设备,因为我们可以把其他东西也模拟成为一个设备,比如说 KVM 就可以被实现成为一个设备,我们可以通过打开 /dev/kvm 来打开这个内核虚拟出来的设备并通过 ioctl() 来对其进行控制。

lab06.pdf

Detached HEAD

git checkout 本质上是修改 HEAD 里面的内容来让它指向不同 分支 的,而 HEAD 文件指向的分支就是我们当前的分支,但是有时候 HEAD 不会指向任何分支,即指向了一个没有分支名字的 commit,即 HEAD 处于游离状态(detached HEAD)。

The advantages of a Kernel Sub-Maintainer

ols2010-pages-155-158.pdf

Major and Minor Numbers in device files (主设备号,次设备号)

crw-rw-rw- 1 root   root    1, 3   Feb 23 1999  null
crw------- 1 root   root   10, 1   Feb 23 1999  psaux
crw------- 1 rubini tty     4, 1   Aug 16 22:22 tty1
crw-rw-rw- 1 root   dialout 4, 64  Jun 30 11:19 ttyS0
crw-rw-rw- 1 root   dialout 4, 65  Aug 16 00:00 ttyS1
crw------- 1 root   sys     7, 1   Feb 23 1999  vcs1
crw------- 1 root   sys     7, 129 Feb 23 1999  vcsa1
crw-rw-rw- 1 root   root    1, 5   Feb 23 1999  zero

The major number identifies the driver associated with the device. For example, /dev/null and /dev/zero are both managed by driver 1, whereas virtual consoles and serial terminals are managed by driver 4.

The minor number is used only by the driver specified by the major number; other parts of the kernel don’t use it, and merely pass it along to the driver. It is common for a driver to control several devices (as shown in the listing); the minor number provides a way for the driver to differentiate among them.

For short: major numbers are for determining a driver, and minor numbers are for determining a device in a driver.

Major and Minor Numbers - Linux Device Drivers, Second Edition [Book]

Where do I find the version of a Linux kernel source tree?

Where do I find the version of a Linux kernel source tree? - Stack Overflow

What Are the Differences Between ISO and IMG Files?

There is no difference in the structure of ISO and IMG formats if the IMG file is uncompressed. It is possible for an IMG format file to be renamed with the ISO file extension and then opened in software that only recognizes the ISO file format.

What Are the Differences Between ISO and IMG Files?

Create image on ubuntu

Image building Ubuntu

Create image for qemu to run

qemu_usage/0001-make-guest-image at master · liujunming/qemu_usage

Memory pool

Memory pool - Wikipedia

MMIO / PMIO

Each I/O device monitors the CPU's address bus and responds to any CPU access of an address assigned to that device, connecting the data bus to the desired device's hardware register.

Port-mapped I/O (PMIO) often uses a special class of CPU instructions designed for performing I/O, such as the in and out. I/O devices have a separate address space from general memory.

Memory-mapped I/O (MMIO) uses the same physical address space to address both memory and I/O devices. So a memory address may refer to either RAM or I/O device. Thus, the CPU instructions used to access the memory can also be used for accessing devices.

Memory-mapped I/O and port-mapped I/O - Wikipedia

Advantage:

  • By discarding the extra complexity that port I/O brings, a CPU requires less internal logic and is thus cheaper, faster, easier to build, consumes less power and can be physically smaller;
  • instructions that perform an ALU operation directly on a memory operand (loading an operand from a memory location, storing the result to a memory location, or both) can be used with I/O device registers as well.

As you look at this, the crucial thing to keep in mind is that the CPU doesn’t really know anything about what it’s connected to. It talks to the outside world through its pins but it doesn’t care what that outside world is.

When the northbridge receives a physical memory request it decides where to route it: should it go to RAM? Video card maybe? This routing is decided via the memory address map. For each region, the memory map knows the device that owns that region. (MMIO 的这段映射程序是存在于 Northbridge 上面)

This memory layout is permanent, but user programs do not see it directly - instead, they run into their own virtual address space to which the kernel can decide to map, wherever it wants, physical memory and IO ranges.

Whose job to execute MMIO map?

Address decoder - Wikipedia

An address decoder has two or more inputs for address bits and one or more outputs for device selection signals.

When the address for a particular device appears on the address inputs, the decoder asserts the selection output for that device.

  • A dedicated, single-output address decoder may be incorporated into each device on an address bus,
  • or a single address decoder may serve multiple devices.

For PCI:

  • Memory BARs: Bit 31-4 is the base address.
  • I/O BARs: Bit 31-2 is the base address.

Whose job to establish MMIO map? BIOS or OS?

In sum, the BIOS:

  • Discovers the device's BARs.
  • For each BAR, asks the BAR how much address space it needs.
  • Assigns the BAR a block of address space.
  • Programs each BAR with the base addresses (this also configure the address decoder).

How is memory mapped to certain hardware? How is MMIO accomplished exactly? - Super User

Can MMIO mapping be updated in runtime?

MMIO process

In sum, the BIOS:

  • Discovers the device's BARs (base address register). Each device can have up to six BARs.
  • For each BAR, asks the BAR how much address space it needs.
  • Assigns the BAR a block of address space.
  • Programs the BAR with the base address of the chosen block of memory address space.

You can, of course, find the process detailed on Wikipedia's PCI configuration space - Wikipedia page.

How is memory mapped to certain hardware? How is MMIO accomplished exactly? - Super User

Motherboard Chipsets and the Memory Map | Many But Finite

mmap (memory-mapped file I/O), not to be confused with memory-mapped I/O