2024-07 Monthly Archive
中断上下文和进程上下文 / Interrupt context and process context
Process context can be go into the sleep, preemptable, It perform time consumable task, acquiring and releasing mutex.
Interrupt context is when the interrupt occurs state/priority goes to interrupt handler, and current process stops/saves until we complete interrupt, Interrupt context is not time consumable, non preemptable, It cannot go into the sleep.
linux - Difference between interrupt context and process context? - Stack Overflow
Stack in x86
Stack pointer 寄存器所指向的内存空间被映射在了页表里了吗?
是的,operating system - How does page table handle both stack and heap memory addresses? - Stack Overflow,这意味着 Userspace 可以不使用 POP 和 PUSH 来操作栈,可以通过直接访问内存来操作栈。push 5
is the same as lea rsp, [rsp-8]; mov qword [rsp], 5
:x86 - How do I correctly manipulate the stack in assembly? - Stack Overflow
rcu_register_thread()
/ rcu_unregister_thread()
QEMU
void rcu_register_thread(void)
{
assert(get_ptr_rcu_reader()->ctr == 0);
qemu_mutex_lock(&rcu_registry_lock);
QLIST_INSERT_HEAD(®istry, get_ptr_rcu_reader(), node);
qemu_mutex_unlock(&rcu_registry_lock);
}
void rcu_unregister_thread(void)
{
qemu_mutex_lock(&rcu_registry_lock);
QLIST_REMOVE(get_ptr_rcu_reader(), node);
qemu_mutex_unlock(&rcu_registry_lock);
}
Can establish TCP connection but can't SSH / SSH hang
可以用这种方式 debug:
linux - I am trying to SSH into a server and it hangs at login - Super User
- run
/usr/sbin/sshd -d -D -p 222
on the server. - run
ssh -v -C -A -X -p 222 lei@ipaddr
on the client.
遇到过的情况是 ssh2_msg_kexinit sent but not received
。
这里遇到过类似的问题:
Socat to test TCP port
// On server
nc -l 0.0.0.0 1234
// On client
socat STDIO TCP4:10.239.53.13:1234
如果连接上了,你在 client 的 prompt 里输入任何内容敲入回车,都能在 server 端看到。
SPTE bits in destination side after live migration
做迁移时,因为 destination 端只有 source 端的 GFN 以及 GFN 所对应的页的内容的信息,但是并没有这个 GFN 在 source 端所对应的 SPTE 的哪些 bits 被置上了。Destination 端怎么保证这些 bits 和 source 端保持一致呢?
可能是 QEMU 只负责把数据放到对应的页里。刚开始 destination side 的 EPT 页表没有任何内容,只有通过不断的 page fault 才会慢慢把页表建立起来。但是,假如说在 source 端我们 write protect 某一个页,就是不想让 VM 来用,那迁过去之后 VM 就可以用了,这是不是是不对的?
UMWAIT
Check if host support UMONITOR (cat /proc/cpuinfo | grep waitpkg
) "mwait/umonitor/tpause are shared the same flag waitpkg".
Logging with raw data in kernel
trace_printk()
和 printk()
都会输出 timestamp 信息,虽然我们可以通过这个方式 linux kernel printk output - Stack Overflow 来取消 printk()
的信息,但是 kernel 调用 printk()
本身对性能也有很大的影响,可能也会影响到测试本身。
VMCS Fields
APPENDIX B FIELD ENCODING IN VMCS
此站点正在尝试打开
HTTPS 只需要首次允许,但是 HTTP 站点就需要每次都允许,非常烦人。
- 地址栏输入 chrome://flags/
- 搜索 Insecure origins treated as secure,配置允许网站,需要协议和端口。
- 再次跳转会显示始终允许选项
此站点正在尝试打开 ,chrome/edge 允许http网站打开url schema-CSDN博客
Typesense kernel config
TASK_IO_ACCOUNTING
, TASK_XACCT
.
容器存储层 / Docker 文件系统分层存储原理
镜像使用的是分层存储,容器也是如此。每一个容器运行时,是以镜像为基础层,在其上创建一个当前容器的存储层,我们可以称这个为容器运行时读写而准备的存储层为容器存储层。容器存储层的生存周期和容器一样,容器消亡时,容器存储层也随之消亡。因此,任何保存于容器存储层的信息都会随容器删除而丢失。
我们在 Dockerfile 里 mkdir 相当于就是在修改存储层。
按照 Docker 最佳实践的要求,容器不应该向其存储层内写入任何数据,容器存储层要保持无状态化。所有的文件写入操作,都应该使用数据卷(Volume)、或者绑定宿主目录,在这些位置的读写会跳过容器存储层,直接对宿主(或网络存储)发生读写,其性能和稳定性更高。
Google Cloud Run
容器的状态有未创建、Active 和 Idle 三种。在容器空闲时好像只收 1/10 的费用。
默认情况下,Cloud Run 仅会在以下情况下对分配给实例的 CPU 和内存收费:
- 实例正在启动。
- 实例正在正常关停(处理 SIGTERM 信号)。
- 该实例正在处理至少一个请求。 计费实例时间从第一个请求的开始时间算起,到最后一个请求的结束时间为止,如下图所示:
如果您设置了实例数下限,那么当这些实例未处理请求时,您还将按不同的“空闲”费率计费(也就是说当容器处于 idle 状态的时候)。请参阅上表。 但是看下来好像空闲的时候只有 CPU 的价格打折了,内存的价格并没有打折。所以最好还是自动 shutdown 比较好。
同源策略 / same-origin policy
[3 Ways to Fix the CORS Error — and How the Access-Control-Allow-Origin Header Works | by David Katz | Medium](https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9) |
For every HTTP request to a domain, the browser attaches any HTTP cookies associated with that domain.
The only trouble is that the browser automatically includes any relevant cookies stored for a domain when another request is made to that exact domain. Say you clicked on a particularly trick website evil-site.com
. The evil site also has the ability send a request to facebook.com/api
. Since the request is going to the facebook.com
domain, the browser includes the relevant cookies. Evil-site sends the session cookie, and gains authenticated access to facebook-clone. Your account has been successfully hacked with a cross-site request forgery attack. 这样相当于 evil site 利用你的 cookie 登录到了 facebook 当中,并且可以接下来请求一些只有登录之后才能获取的内容。
fix 很简单,在 facebook.com
网站加入 Access-Control-Allow-Origin 就行了。
/usr/bin/ld: Cannot find
-lc
: yum install glibc-static
ENQCMD
这是 specific to Intel DSA 的一个指令,就像 MOVDIR64
一样。所以要了解这个指令做了什么需要首先熟悉 Intel DSA。
allows software to write commands to enqueue registers, which are special device registers accessed using memory-mapped I/O (MMIO).
在每一个 Device 中可以实现一个通过 MMIO 访问的 register。Enqueue registers expect writes to have the following format:
511 32 31 30 20 19
DEVICE SPECIFIC COMMAND PRIV RESERVED PASID
The ENQCMD instruction begins by reading 64 bytes of command data from its source memory operand.
The address of the destination operand is specified in a general-purpose register.
Page Attribute Table (PAT)
The Page Attribute Table (PAT) is a processor feature available on many x86 and x86-64 CPUs.
It acts as a companion to Memory Type Range Registers (MTRRs) and provides a finer-grained control over how different areas of memory are cached.
Terminal recorders
Name | 遇到的问题 |
---|---|
asciinema/agg | Nerd Font icons are not supported well and cannot be rendered to gif. |
terminalizer | Use NPM which I dislike. |
VHS | 这不是一个录屏工具,而是你需要事先告诉你想要执行的那些命令,然后它来执行 |
menyoki | Cargo install failed |
t-rec | No X11, cannot build |
termtosvg | Obslete |
看起来还是 asciinema/agg 好一点。
Asciinema/Agg
asciinema 虽然可以 upload,但是可能因为字体什么的原因,和我们看到的会有一些区别,所以可以用 agg 直接生成 gif:
首先下载 fonts 到指定目录:
mkdir -p ~/.local/share/fonts
wget https://github.com/ryanoasis/nerd-fonts/releases/lastest/UbuntuMono.zip
cp UbuntuMono.zip ~/.local/share/fonts
cd ~/.local/share/fonts
de UbuntuMono.zip
rm UbuntuMono.zip
然后转化成 gif:
# Ctrl-d to exit
# We build from source
~/p/asciinema/target/release/asciinema rec -c fish persistent.cast
agg --font-family "UbuntuMono Nerd Font" persistent.cast persistent.gif
Split a large commit to patch set
You can use the Github compare tool to achieve this:
https://github.com/tristone13th/lspmark.nvim/compare/054ff99e0f615a75208d8ccb05dd3a6865392296..ff0eee58cffbcf1d20276c7fef43cce51bb7b1af
WRITE_ONCE()
Kernel
Please write a lua function that has an input "symbol", which has the following properties:
- symbol.name
- symbol.kind
- symbol.start.line
- symbol["end"].line
- symbol.start.character
- symbol["end"].character
We also have a global variable which is a table, the key is filename and the value is also a table, the value table has key kind and the value is also a table, the table has key name the value is an array that has start line, end line, start character end character which represents a range. Could you write a lua function to judge if the input "symbol" is in one of the range?
How to write a telescope extension
telescope.nvim/developers.md at master · nvim-telescope/telescope.nvim
Take this folder as an example:
https://github.com/cbochs/grapple.nvim/tree/main/lua/telescope/_extensions
Binary translation
一篇不错的关于 binary translation 的硕士学位论文:
https://github.com/Martins3/Bare-Metal-Binary-Translator/blob/master/paper/thesis.pdf
icount
In QEMU
icount 在 QEMU 中全称为 TCG Instruction Counting。它是 TCG 用于指令计数的一个组件,当 CPU 在 icount 模式下 sleep 时,通过它来计算时间。
To enable icount in QEMU, you can use the -icount
option on the command line. The -icount
option accepts several parameters to control its behavior:
qemu-system-x86_64 -icount shift=7 -hda /path/to/disk.img
TCG Instruction Counting — QEMU documentation
QEMU timer 模块分析 | ⚡️ Rickylss Blog ⚡️
QEMU_CLOCK_VIRTUAL_RT
QEMU
这个和 QEMU_CLOCK_REALTIME
以及 QEMU_CLOCK_VIRTUAL
的区别是什么?看起来像是这两者的结合体。
- Outside
icount
mode, this clock is the same asQEMU_CLOCK_VIRTUAL
. - In
icount
mode, this clock counts nanoseconds while the virtual machine is running. It is used to increaseQEMU_CLOCK_VIRTUAL
while the CPUs are sleeping and thus not executing instructions.(如果是这样,为什么不直接用 REALTIME 呢)。
Real-time clock tied to the virtual machine's execution, but it is not paused when the VM's vCPUs are blocked or waiting.
qemu_cond_timedwait()
QEMU
阻塞当前线程,两种情况下被唤醒:
- The condition is met
- Timeout
qemu_cond_timedwait(cond, &bql, ms);
SCM_RIGHTS
SCM_RIGHTS
is a feature of UNIX domain sockets. It allows one process to pass a fd to another process through the UNIX domain socket.
After passing a file descriptor (fd) from one process to another using SCM_RIGHTS
, you can close the file descriptor in the source process. The file descriptor in the destination process will remain valid and operational. This is because SCM_RIGHTS
creates a new file descriptor in the destination process that refers to the same underlying file or resource.
-uuid
In QEMU
System uuid.
This UUID helps identify the virtual machine instance uniquely and can be used in various scenarios.
Delete all git commits in a Github repo
# Checkout/create orphan branch (this branch won't show in git branch command):
git checkout --orphan latest_branch
# Add all the files to the newly created branch:
git add -A
# Commit the changes:
git commit -am "commit message"
# Delete main (default) branch (this step is permanent):
git branch -D main
# Rename the current branch to main:
git branch -m main
# Finally, all changes are completed on your local repository, and force update your remote repository:
git push -f origin main
git - how to delete all commit history in github? - Stack Overflow
What is Boot Sector? / x86_bootsect
QEMU
可以看到下面 x86_bootsect
就是 512 bytes。
unsigned char x86_bootsect[] = {
0xfa, 0x0f, 0x01, 0x16, 0xb8, 0x7c, 0x66, 0xb8, 0x01, 0x00, 0x00, 0x00,
0x0f, 0x22, 0xc0, 0x66, 0xea, 0x20, 0x7c, 0x00, 0x00, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe4, 0x92, 0x0c, 0x02,
0xe6, 0x92, 0xb8, 0x10, 0x00, 0x00, 0x00, 0x8e, 0xd8, 0x66, 0xb8, 0x41,
0x00, 0x66, 0xba, 0xf8, 0x03, 0xee, 0xb3, 0x00, 0xb8, 0x00, 0x00, 0x10,
0x00, 0xc6, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00, 0x00, 0x3d, 0x00, 0x00,
0x40, 0x06, 0x7c, 0xf1, 0xb8, 0x00, 0x00, 0x10, 0x00, 0xfe, 0x00, 0x05,
0x00, 0x10, 0x00, 0x00, 0x3d, 0x00, 0x00, 0x40, 0x06, 0x7c, 0xf2, 0xfe,
0xc3, 0x80, 0xe3, 0x3f, 0x75, 0xe6, 0x66, 0xb8, 0x42, 0x00, 0x66, 0xba,
0xf8, 0x03, 0xee, 0xa1, 0xbe, 0x7c, 0x00, 0x00, 0x83, 0xf8, 0x00, 0x74,
0xd3, 0xb8, 0x04, 0x00, 0x10, 0x00, 0x8b, 0x00, 0x83, 0xf8, 0x01, 0x74,
0xc7, 0xb0, 0xf1, 0xe6, 0xb2, 0xb8, 0x04, 0x00, 0x10, 0x00, 0xc7, 0x00,
0x01, 0x00, 0x00, 0x00, 0x66, 0xb8, 0x01, 0x24, 0x66, 0xba, 0x04, 0x06,
0x66, 0xef, 0x66, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0x00, 0x9a, 0xcf, 0x00, 0xff, 0xff, 0x00, 0x00,
0x00, 0x92, 0xcf, 0x00, 0x27, 0x00, 0xa0, 0x7c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
};
The term "boot sector" can refer generically to any sector that contains boot code, including both the MBR^ and the VBR^. However, it is often used more specifically to describe the VBR.
The boot sector is the first sector (usually 512 bytes) of a storage device, such as a hard disk or a USB drive, which contains the machine code to be executed by the BIOS or UEFI firmware during the boot process.
The last two bytes of a valid boot sector contain a signature (0x55AA
), which the BIOS checks to confirm that the sector contains executable boot code.
The 0x55AA
signature is present in both the MBR and the VBR. In both contexts, it serves a similar purpose: to indicate that the sector contains valid boot code (Please see the format in MBR^ and VBR^).
不能把 boot sector 和 MBR 混淆在一起。MBR 和 boot sector 都是磁盘中的一个磁区(sector),但是他两个的区别是 MBR 是整个磁盘的第一个 sector,boot sector(如果我们指的是 partition boot sector 的话)是每个分区的第一个 sector。
启动顺序有先后。PC 机启动时,先执行 MBR,再根据选择执行某个分区下的 partition boot sector。
bootloader 应该在 partition boot sector 中而不是在 MBR 中,因为我们需要在 BIOS 里指定了 boot 顺序并且 boot 到了对应的分区之后,在 boot 分区的过程中才能去选择我们要启动什么 kernel。
VBR (Volume Boot Record) / Partition Boot Sector
VBR 有另一个别名叫做 Partition Boot Sector。
The VBR is located at the beginning of each partition on the storage device. Each partition has its own VBR.
Offset Size Description
0x000 3 Jump Instruction
0x003 8 OEM Name
0x00B 25 BIOS Parameter Block (BPB)
0x024 26 Extended BPB
0x03E 448 Boot Code
0x1FE 2 Boot Signature (0x55AA)
MBR 和 VBR 尽管都是 512 bytes,但是他们的 format 是不一样的,这一点要注意。
Secondary MMU
In virtualized environments, secondary MMUs (often called Extended Page Tables (EPT) in Intel's VT-x or Nested Page Tables (NPT) in AMD's AMD-V) help manage the translation of guest virtual addresses to host physical addresses, providing an additional layer of memory management.
Create alias for python class
这篇文章很对,可以参考:Create Alias for Python Class | Xianghu's Blog
对于 deepcopy mutable 成员的问题,可以试试这个答案:https://stackoverflow.com/a/49329915/23683193
BufEnter / BufWinEnter
两者的区别在于是否这一个 buffer 同时被多个 windows share。
buffers - What's the difference between BufWinEnter
and BufEnter
? - Vi and Vim Stack Exchange
FLAGS
/ EFLAGS
/ RFLAGS
FLAGS
: 16 bits, EFLAGS
: 32 bits, RFLAGS
: 64 bits. The wider registers retain compatibility with their smaller predecessors.
For each individual bits: FLAGS register - Wikipedia
RFLAGS.IF
其实就是表示 CPU 开不开中断。
Determines whether or not the (CPU) will respond immediately to maskable hardware interrupts
.
This flag does not affect the handling of
- non-maskable interrupts (NMIs) or,
- software interrupts generated by the
INT
instruction.
When set, the processor recognizes external interrupts on the INTR pin. When set, interrupts are recognized and acted on as they are received. The bit can be cleared to turn off interrupt processing temporarily.