0x822a5b87的博客

到码头整点薯条吃

多道程序与分时多任务

内存调度模型

flowchart LR
    %% 按类型定义专属样式(遵循通俗约定:函数=蓝、结构体=绿、寄存器=紫、栈=浅红、内存=黄、管理组件=深蓝)
    classDef func fill:#4299e1, stroke:#2563eb, stroke-width:2px, color:#fff, font-weight:bold, rounded:8px, font-size:14px;
    classDef struct fill:#4ade80, stroke:#16a34a, stroke-width:2px, color:#1e293b, font-weight:bold, rounded:8px, font-size:14px;
    classDef reg fill:#a855f7, stroke:#7e22ce, stroke-width:2px, color:#fff, font-weight:bold, rounded:6px, font-size:13px;
    classDef stack fill:#fecaca, stroke:#dc2626, stroke-width:2px, color:#7f1d1d, font-weight:600, rounded:8px, font-size:13px;
    classDef memory fill:#fde047, stroke:#ca8a04, stroke-width:2px, color:#78350f, font-weight:600, rounded:8px, font-size:13px;
    classDef manager fill:#1e40af, stroke:#1e3a8a, stroke-width:2px, color:#fff, font-weight:bold, rounded:10px, font-size:14px;
    classDef transition stroke:#64748b, stroke-width:1.5px, stroke-linecap:round, font-size:12px, font-family:Arial;
    classDef subgraphTitle fill:#1e293b, font-weight:bold, font-size:14px, font-family:Arial;

    %% 栈(USER_STACK/KERNEL_STACK)
    subgraph USER_STACK["USER_STACK"]
        direction LR
        US1("..."):::stack
        US0("UserStack 0"):::stack
    end
    subgraph KERNEL_STACK["KERNEL_STACK"]
        direction LR
        KS1("..."):::stack
        KS0("KernelStack 0"):::stack
    end

    %% 内存/二进制代码
    subgraph Memory["Memory"]
       direction LR
        M1("..."):::memory
        M0("Task 0 Code"):::memory
    end

    %% 任务相关(结构体:TaskControlBlock/TaskStatus/TaskContext/TrapContext)
    subgraph tasks["tasks"]
       subgraph TaskControlBlock1["TaskControlBlock1"]
        TaskContext1("..."):::struct
        TaskStatus1("..."):::struct
       end
       subgraph TaskControlBlock0["TaskControlBlock0"]
        subgraph TaskContext0["TaskContext0"]
            ra0("ra"):::reg 
            sp0("sp"):::reg
            s0("s0 ~ s11"):::reg
        end
        TaskStatus0("TaskStatus 0"):::struct
       end
    end
    subgraph TrapContextList["TrapContextList"]
        OtherTrapContext("..."):::struct
        subgraph TrapContext["TrapContext"]
            x0("x0~x31"):::reg
            ssstatus0("sstatus"):::reg
            sepc0("sepc"):::reg
        end
    end

    %% 管理组件(TaskManager/TASK_MANAGER)
    TaskManager("TaskManager"):::manager
    TASK_MANAGER("TASK_MANAGER"):::manager

    %% 函数(main/__restore)
    main("main"):::func
    __restore("__restore"):::func

    %% 原有连接关系(保持不变,统一应用transition样式)
    TaskManager -->|延迟加载全局唯一实例| TASK_MANAGER:::transition
    main -->|1. load_apps 加载应用代码| Memory:::transition
    main -->|2. run_first_task触发初始化| TASK_MANAGER:::transition
    TASK_MANAGER -.->|初始化| tasks:::transition
    sp0 -.->|__restore使用sp作为参数恢复上下文| __restore:::transition
    sepc0 -.-> M0:::transition
    x0 -.->|sp| US0:::transition
    KS0 -.->|存储了TrapContext| TrapContext:::transition
    ra0 --> __restore:::transition
    sp0 --> KS0:::transition

    %% 优化子图标题样式(统一居中、加粗)
    style USER_STACK fill:#fef2f2, stroke:#fecaca, stroke-width:1px, padding:10px;
    style KERNEL_STACK fill:#fef2f2, stroke:#fecaca, stroke-width:1px, padding:10px;
    style Memory fill:#fffbeb, stroke:#fde047, stroke-width:1px, padding:10px;
    style tasks fill:#ecfccb, stroke:#4ade80, stroke-width:1px, padding:10px;
    style TrapContextList fill:#ecfccb, stroke:#4ade80, stroke-width:1px, padding:10px;

实现

应用加载

阅读全文 »

批处理系统

综述

在本章节中,我们实现一个简单的批处理系统,内核在启动之后,将多个程序打包输入并按顺序执行,而本章节我们会实现以下几个重要逻辑:

  1. 通过加载器在启动时动态的加载代码到并执行;
  2. 实现错误处理,保证在单个程序出错时不影响其他的程序。这个位置我们会引入一些新的概念,例如 privilege。同时我们也可以看到操作系统和普通的应用程序的区别,例如:操作系统有自己的内核栈,这个栈是完全独立于任何程序的。
  3. 操作系统通过trap机制来处理 interruptionexception 事件。
---
title: Compiling rCore-test
---
flowchart TB

subgraph desc
    direction TB
    build_system("构建系统"):::purple
    dir("文件夹"):::green
    products("源文件/构建产物"):::animate
end


subgraph 构建过程
    direction LR
    makefile("Makefile"):::purple
    build("build.py"):::purple

    target("target/riscv64gc-unknown-none-elf/release"):::green
    build_dir("build"):::green

    source_code("*.rs"):::animate
    bin("*.bin"):::animate
    elf("*.elf"):::animate
    object("*.object"):::animate


    source_code -->|输入| build -->|编译| object -.->|输出到文件夹| target
    object -->|输入| makefile -->|rust-objcopy| bin -.->|输出到文件夹| target
    object -->|输入| makefile -->|cp| elf -.->|输出到文件夹| target

    bin -->|cp| build_dir
    elf -->|cp| build_dir
end

desc --> 构建过程

classDef pink 0,fill:#FFCCCC,stroke:#333, color: #fff, font-weight:bold;
classDef green fill: #695,color: #fff,font-weight: bold;
classDef purple fill:#968,stroke:#333, font-weight: bold;
classDef error fill:#bbf,stroke:#f65,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
classDef coral fill:#f8f,stroke:#333,stroke-width:4px;
classDef animate stroke-dasharray: 8,5,stroke-dashoffset: 900,animation: dash 25s linear infinite;
阅读全文 »

项目的全部代码在这里:ardi

整体逻辑图解

我们的项目包含以下文件,各模块功能如下:

  1. *.rs 文件:Rust 源代码文件,定义了程序的核心执行逻辑;
  2. linker.ld:链接脚本(Linker Script),用于告知链接器如何合并目标文件、编排内存布局,最终生成符合要求的二进制文件;
  3. entry.asm:启动引导文件(汇编编写),负责初始化基础执行环境,引导 CPU 定位并跳转到 Rust 程序的入口地址;
  4. rustsbi-qemu.bin :SBI(Supervisor Binary Interface)固件文件,作为内核与硬件的隔离层 / 抽象层,为内核提供硬件相关的基础服务(详见 什么是 SBI);
  5. Makefile : 构建脚本,通过声明编译目标(target)、依赖关系及执行命令,简化程序的编译、运行、加载等流程管理。
flowchart LR

rustsbi(rustsbi-qemu.bin):::coral
linker("linker.ld"):::green
entry("entry.asm"):::green
rust("*.rs"):::green
qemu("qemu"):::error

cargo("cargo"):::error

linker --> cargo
entry --> cargo
rust --> cargo

cargo --> elf -->|rust-objcopy| binary

subgraph elf
    direction TB
    os_comment("elf格式的二进制文件") -.-> os("os"):::pink
end

subgraph binary
    direction TB
    os_bin_comment("二进制文件") -.-> os_bin("os.bin"):::pink
end

binary --> rustsbi --> qemu

classDef pink 0,fill:#FFCCCC,stroke:#333, color: #fff, font-weight:bold;
classDef green fill: #695,color: #fff,font-weight: bold;
classDef purple fill:#968,stroke:#333, font-weight: bold;
classDef error fill:#bbf,stroke:#f65,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
classDef coral fill:#f8f,stroke:#333,stroke-width:4px;
classDef animate stroke-dasharray: 8,5,stroke-dashoffset: 900,animation: dash 25s linear infinite;
阅读全文 »

前言

最近在学习 RISCV 的过程中,碰见了一个比较奇特的BUG,在下面的代码中,我们出现了一个神秘的未定义行为(Undefined Behavior):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

void main()
{
char a;
unsigned int b;
unsigned long c;

a = 0x88;
b = ~a;
c = ~a;

printf("a=0x%x, ~a=0x%x, b=0x%x, c=0x%lx\n", a, ~a, b, c);
}

本来是一段很简单的用于测试C语言下的隐式转换规则的代码,结果很神奇的是在两个不同的平台下出现了截然不同的输出!

x86/x86_64

阅读全文 »

最近由于学习 risc-v,需要使用到 gdb 来调试 elf 文件,本来想使用大学期间比较熟悉的 gdbinit 或者 peda。折腾了接近两天时间,修复了各种问题,最后还是失败了。直接切换到 pwndbg 解决全部问题。

pwndbg

回想这两天痛苦的查文档,翻github的issue却从来没有想过时代在变化,大学期间的明星项目peda已经断更五年多了。

综述

本文是在学习python的过程中的一个简单的练手小项目,用于熟悉python内部的一些机制,全部的代码位于 tiny package manager

整体的逻辑是,当我们在编译一个 yarn 项目的过程中,我们会存在一个 package.json 文件,在文件中我们会声明项目的所有依赖。我们通过该依赖文件来进行一个包依赖分析,判断是否项目是否存在版本冲突,如果可以编译,则给出选择对应的版本。

例如:

---
title: dependencies
---
flowchart LR

jest("jest"):::pink

jest2("0.0.61"):::green
jest1("0.0.71"):::green

express-resource("express-resource-*"):::purple
underscore("underscore-*"):::purple
sji("sji-*"):::purple


express("express-*"):::purple
api-easy("api-easy-*"):::purple
mongoose("mongoose-*"):::purple

indirect01("indirect dependency ..."):::animate

jest --> jest1
jest --> jest2

jest1 --> express-resource --> indirect01
jest1 --> underscore --> indirect01
jest1 --> sji --> indirect01

jest2 --> express --> indirect01
jest2 --> api-easy --> indirect01
jest2 --> mongoose --> indirect01
jest2 --> express-resource

classDef pink 1,fill:#FFCCCC,stroke:#333, color: #fff, font-weight:bold;
classDef green fill: #696,color: #fff,font-weight: bold;
classDef purple fill:#969,stroke:#333, font-weight: bold;
classDef error fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
classDef coral fill:#f9f,stroke:#333,stroke-width:4px;
classDef animate stroke-dasharray: 9,5,stroke-dashoffset: 900,animation: dash 25s linear infinite;
阅读全文 »

插件逻辑

dify 的插件管理中,整个体系逻辑比较混乱,这里使用该文本记录一下 dify 的插件管理逻辑。

插件概述

一个插件以及插件对应的工具的组成部分如下图所示:

---
title: 插件
---
flowchart LR

plugin_id("plugin_id"):::green
organization1("organization"):::animate
plugin_name1("plugin_name"):::animate
slash1("/"):::error

plugin_id --> organization1 --> slash1 --> plugin_name1


plugin_unique_identifier("plugin_unique_identifier"):::green

organization2("organization"):::animate
slash2("/"):::error
symbol2(":"):::error
at2("@"):::error
plugin_name2("plugin_name"):::animate
version1("version"):::animate
checksum1("checksum"):::animate

plugin_unique_identifier --> organization2 --> slash2 --> plugin_name2 --> symbol2 --> version1 --> at2 --> checksum1

provider("full_provider"):::green

organization3("organization"):::animate
slash3_1("/"):::error
plugin_name3("plugin_name"):::animate
slash3_2("/"):::error
provider_name3("provider"):::animate

provider --> organization3 --> slash3_1 --> plugin_name3 --> slash3_2 --> provider_name3

classDef pink 1,fill:#FFCCCC,stroke:#333, color: #fff, font-weight:bold;
classDef green fill: #696,color: #fff,font-weight: bold;
classDef purple fill:#969,stroke:#333, font-weight: bold;
classDef error fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
classDef coral fill:#f9f,stroke:#333,stroke-width:4px;
classDef animate stroke-dasharray: 9,5,stroke-dashoffset: 900,animation: dash 25s linear infinite;
阅读全文 »

Conclusion of Linear Algebra For Everyone

REFERENCES

Glossary of Terms

terms descprition
Matrix A rectangular array of numbers, symbols, or expressions arranged in rows and columns.
Vector An ordered set of numbers, often represented as a column or row matrix.
Scalar A single number, as opposed to a vector or matrix.
Transpose A matrix operation that flips a matrix over its .
Determinant A scalar value that can be computed from a square matrix
Eigenvector A non-zero vector that only changes by a scalar factor when a linear transformation is applied to it.
Eigenvalue The scalar factor by which an eigenvector is scaled during a linear transformation.
Rank The dimension of the column space or row space of a matrix.
Dot Product The scalar product of two vectors, obtained by multiplying the corresponding components and summing the results.
Cross Product The vector product of two vectors in three-dimensional space, resulting in a vector that is to the original vectors
Orthogonal Two vectors are orthogonal if their dot product is zero
space Represents a collection of vectors with certain properties
Vector Space A vector space is a set of vectors along with operations of vector addition and scalar multiplication that satisfy certain properties.
Subspace A subspace is a subset of a vector space that is itself a vector space. It contains the zero vector, is closed under vector addition and scalar multiplication, and satisfies the other properties of a vector space.
Inner product space An inner product space is a vector space equipped with an inner product, which is a generalization of the dot product. The inner product defines a notion of angle and length in the space.
Normed Vector Space A normed vector space is a vector space equipped with a norm, which is a function that assigns a non-negative length to each vector and satisfies certain properties.
Hilbert Space A Hilbert space is a complete inner product space, meaning that it is a vector space equipped with an inner product that is also a complete metric space with respect to the norm induced by the inner product.
Upper Triangular Matrix A Upper Triangular Matrix is a matrix in which all the elements below the main diagonal are zero, and all the elements on or above the main diagonal can be zero or non-zero.
Lower Triangular Matrix A Lower Triangular Matrix is a matrix in which all the elements above the main diagonal (the diagonal from the top left to the bottom right) are zero, and all the elements on or below the main diagonal can be zero or non-zero.
Orthogonal Matrix
CR C stands for Column, R stands for Row
LU Lower tirangular and Upper triangular
QR QR decomposition as Gram-Schmidt orthogonalization Orthogonal Q and triangular R
阅读全文 »

BOX-256

前言

最近在学习 RISC-V,但是指令太多,并且学习起来确实很无聊,所以找到了这个小游戏来学习一下汇编的思路。虽然不是真正的标准 RISC-V,但是可以作为学习路上的一个参考工具,下面是 BOX-256 的一些描述:

BOX-256 is a 8-bit fantasy computer, with 256 bytes of memory, 16 color 16x16 display. It is also a programming game, where the player tries to pass the graphics tests and optimize the code to perfection. The ultimate goal is to use as few CPU cycles or lines of code as possible, by employing multithreading and other optimization tricks.

此外,文档里提到了一些比较重要的细节这里也稍微列举一下:

阅读全文 »
0%