第一次课的预习

  • 什么是操作系统?操作系统内核?ubuntu、redhat、安卓,是同一个操作系统吗?

What is the operating system?

An operating system (OS) is a software program that manages computer hardware and software resources and provides common services for computer programs. The OS acts as an interface between the computer hardware and software applications, allowing them to communicate with each other and function properly. It controls the allocation of memory, processes input and output, manages files and directories, supports networking, and provides user interfaces among other things. Examples of popular operating systems include Windows, Mac OS X, Linux, UNIX, Android, and iOS.

翻译:什么是操作系统

操作系统是管理硬件和软件资源的软件程序并且对计算机软件提供一般的服务,操作系统作为计算机硬件和应用软件之间的接口,可以让他们两个之间相互交流并且更好得发挥作用。它掌管着空间分配,进程的调度,管理文件和文件夹,提供网络和接口还有其他的一些东西,当下比较流行的操作系统包括Windows, Mac OS X, Linux, UNIX, Android, and iOS.

操作系统内核:

The operating system kernel is the core component of an operating system that provides essential services and resources for other programs and processes to operate. It manages hardware resources such as the CPU, memory, and input/output devices.

There are different types of operating system kernels, including monolithic kernels, microkernels, hybrid kernels, and exokernels. Each type has its advantages and disadvantages in terms of performance, scalability, and security.

Monolithic kernel has all the functionalities implemented in a single code base which makes it faster but also riskier if something goes wrong. Microkernels have minimum functionality implemented in kernel space and more in user space that can lead to slower performance. Hybrid kernels try to combine the best parts of each type while exokernels expose low-level hardware resources to applications, giving them control over the allocation and management of these resources.

Overall, the choice of kernel type depends on various factors, such as the system requirements, hardware resources, and runtime environment.

太长了,用百度翻译:操作系统内核是操作系统的核心组件,为其他程序和进程提供必要的服务和资源。它管理CPU、内存和输入/输出设备等硬件资源。

有不同类型的操作系统内核,包括单片内核、微内核、混合内核和外内核。每种类型在性能、可扩展性和安全性方面都有其优缺点。

单片内核具有在单个代码库中实现的所有功能,这使其速度更快,但如果出现问题,风险也更大。微内核在内核空间中实现的功能最少,在用户空间中实现更多,这可能导致性能降低。混合内核试图组合每种类型的最佳部分,而外部内核向应用程序公开低级硬件资源,使它们能够控制这些资源的分配和管理。

总体而言,内核类型的选择取决于各种因素,例如系统需求、硬件资源和运行时环境。

A monolithic kernel is an operating system architecture where the entire kernel, including device drivers, file system, and system calls, run in privileged kernel mode. This contrasts with other kernel architectures, such as microkernels or hybrid kernels, where certain kernel components or drivers run outside the kernel in user space.

说明单片内核的这种模式,对内核的操作都是在内核态下运行的,而其他的模式则是将一部分模块分到用户态下运行,运行的空间也不是在内核的空间,而是在用户空间下运行。

In a monolithic kernel, because everything runs in kernel mode, there is direct and efficient access to hardware resources which results in high performance。

这里再次说明单内核所有的操作都运行在内核态下。

Microkernel is a specific design pattern for operating systems. It is called "micro" because it only includes the most fundamental features and components of an operating system, such as memory management or inter-process communication, while all other OS services are implemented on top of it.

In contrast, a monolithic kernel has everything in one self-contained block that runs in kernel space. Microkernels, on the other hand, seek to minimize the complexity and minimize what kernel code has to run in kernel space, in order to make maintainability and security faster and more robust.

Benefits of microkernel architecture include:

Flexibility: New services can easily be added without requiring modifications in the kernel.

Modularity: Services can be written independently from each other.

Reliability and safety: As most of the operating system services do not run in kernel space, bugs and crashes occurring in those services do not crash the kernel, but they can be isolated and contained.

However, microkernels come with some notable disadvantages, such as:

Reduced performance, due to the overhead of inter-process communication between kernel components.

Greater complexity in the overall system architecture.

Harder development, since many details of the communication mechanisms have to be manually implemented.

这段说明了什么是微内核,以及其优缺点。简而言之就是只在内核中实现最基本的内容包括内存管理和进程的通信,其他的内容都以模块的形式进行添加,因为在内核态运行的时候少,所以出问题也很难导致内核崩溃。

混合内核就是他们之间的中和(老中和了,看名字就知道)。

Ubuntu and Red Hat are different Linux distributions, while Android is a mobile operating system based on the Linux kernel.

Ubuntu is a Debian-based Linux distribution that is free and open-source. It was developed by Canonical Ltd. and it is widely used in desktops, servers, cloud computing, and IoT devices due to its easy-to-use interface and robustness.

Red Hat Enterprise Linux (RHEL), on the other hand, is an enterprise-level version of Linux developed by Red Hat Inc. that provides support and security updates over a long period. It is more commonly used in corporate environments, data centers, and virtualization.

Android is a mobile operating system developed by Google based on the Linux kernel. It is designed primarily for touch screen mobile devices such as smartphones and tablets.

Although they all have their roots in Linux, these operating systems serve quite different purposes and use cases.

总结:他们都起源于linux,但是由不同公司开发,发行,维护。相当于不同的发行版

具体解释:https://zhuanlan.zhihu.com/p/81883894?utm_id=0

  • 什么是内核态和用户态?在arm架构下是怎么区分的?

内核态(Kernel Mode):运行操作系统程序,操作硬件

用户态(User Mode):运行用户程序

程序状态字(PSW)是保存CPU状态并跟踪程序执行的寄存器。计算机中的每一条指令都需要CPU执行操作并相应地更新其PSW。PSW存储CPU(用户或主管)的当前模式、条件代码标志(如零、负或溢出标志)和错误条件等信息。

所以查psw寄存器相应的位就行。

  • 什么是虚拟内存技术?这个技术用来干什么?需要硬件什么样的支持?

就是为每个进程创建一片虚拟空间,要用的时候调页调到内存。解决内存不够用的问题。

Memory Management Unit (MMU): MMU translates virtual addresses into physical addresses at runtime. It maps virtual memory pages to physical memory pages, manages page faults, and protects memory against unauthorized access.

Secondary Storage: Virtual memory relies on secondary storage devices such as hard drives, solid-state drives or flash memory to temporarily store pages of data that do not fit in RAM.

Address Translation Capabilities: The CPU needs to be equipped with address translation capabilities to convert virtual addresses into physical addresses via the MMU.

Caching Mechanism: CPU caching can enhance the performance of virtual memory by storing frequently accessed pages inside the cache memory.

百度翻译:

内存管理单元(MMU):MMU在运行时将虚拟地址转换为物理地址。它将虚拟内存页面映射到物理内存页面,管理页面故障,并保护内存免受未经授权的访问。

辅助存储:虚拟内存依靠辅助存储设备(如硬盘驱动器、固态驱动器或闪存)临时存储不适合RAM的数据页。

地址转换功能:CPU需要配备地址转换功能,以便通过MMU将虚拟地址转换为物理地址。

缓存机制:CPU缓存可以通过在缓存内存中存储频繁访问的页面来增强虚拟内存的性能。

  • 什么是中断?这个技术用来干什么?需要硬件什么样的支持?

In computing, an interruption is a signal sent to the processor indicating that an event or process has occurred that requires immediate attention. Interrupts are used for many different purposes such as control of hardware devices, task scheduling, and handling errors.

When an interrupt occurs, the processor stops executing its current task and switches to a special mode called interrupt mode. The processor saves the current state of the program being executed, so it can resume from where it left off later on. Then the interrupt handler, a small program designed to handle a specific type of interrupt, takes over and performs the required action.

Interrupts can be generated by hardware devices such as timers, network cards, and disk controllers, or they can be generated by software processes such as system calls or other programs that need to communicate with the operating system. Interrupts can be classified into two categories: hardware interrupts and software interrupts.

Hardware interrupts are generated by hardware devices, and their priority is usually determined by the device they originate from. Software interrupts are generated by software processes, and their priority can be set by the operating system.

Interrupts are an important mechanism in modern computer systems that allow multiple tasks to be executed concurrently without requiring a separate processor for each task. Interrupts allow the processor to switch between tasks quickly and efficiently, providing the illusion of multitasking to the user.

以上是什么是中断讲得很清楚了。优点就是快,不再需要查一个硬件端口的状态位,它有事会通知你。

Hardware interrupts require support from the CPU and Interrupt Controller to work.

When an interrupt occurs, the hardware sends a signal to the CPU via the Interrupt Controller. The CPU then stops executing its current task and jumps to the interrupt handler routine. Once the handler routine is finished, the CPU can return to the previous task.

This process requires support from the hardware, specifically:

Interrupt pins on the CPU: The CPU must have interrupt pins that can receive signals from the Interrupt Controller.

Interrupt Controller: The Interrupt Controller manages interrupt signals from various hardware devices and sends them to the CPU.

With these two components working together, hardware interrupts can be utilized by the system to handle asynchronous events that require immediate attention.

总结需要中断控制器和CPU上的中断引脚。

以下是百度百科对中断控制器的描述。

多个外部中断源共享中断资源,必须解决相应的一些问题,例如CPU芯片上只有一个INTR输入端,多个中断源如何与INTR连接、中断矢量如何区别、各中断源的优先级如何判定等。可编程中断控制器8259A就是为此目的而设计的,它可以接收多个外部中断源的中断请求,并进行优先级判断,选中当前优先级最高的中断请求,并将此请求送到CPU的INTR端。当CPU响应中断并进入中断服务程序的处理过程后,中断控制器仍负责对外部中断请求的管理。

  • 栈用来干什么?硬件怎么支持栈?

栈是用来存东西的,比如寄存器什么之类的。用DRAM里的一块地址和寄存器中的栈指针(SP),我记得学汇编的时候还有一个专门的寄存器是用于指向栈的内部的,但我忘了叫什么名字了。

  • 进程切换大概是怎么做的?是在内核态完成还是用户态完成?

  1. Save the current process state: When the operating system decides to switch from one process to another, it needs to save the current process's state, including its registers, program counter, and other relevant bits of information, into memory.

  1. Load the new process state: After saving the current process state, the operating system then loads the state of the next process that needs to be executed. It gets this information from the process control block (PCB), which contains all the information related to a specific process.

  1. Update the Memory Management Unit (MMU): As part of the process switch, the OS updates the MMU to ensure that the new process has access to the correct memory spaces.

  1. Jump to the new process: Finally, the OS jumps to the new process's saved program counter, which begins executing the next set of instructions.

上面的四个步骤总结:保存现场,切换pcb,更新内存管理单元,切换pc开始执行。在内核态,因为我们自己写的多线程的程序的调度从来都是内核的调度策略决定的,很多时候输出的顺序我都不知道。

  • 熟悉arm64汇编指令,主要包含数据移动、栈操作指令、函数调用与返回。一个简单的程序如下,要能看懂,同时特别是对栈和函数调用返回相关的操作。

我们从main函数开始一条一条得看

stp x29,x30,[sp,#-48]!

将x29和x30存到栈里面,x29是栈底寄存器,x30是LR,!表示将sp更新为sp-48.

str wzr,[x29,#20]
  • wzr is the name of the 64-bit zero register in ARM Assembly. It always contains a value of zero, so storing it can be useful when we need to clear out some memory location(s).

ret

ret (short for "return") in ARM64 is an instruction used to return from a function or subroutine.

When a function is called, the return address (i.e., the address of the instruction immediately following the bl instruction that called the function) is saved on the stack. When the ret instruction is executed, it loads this saved address from the stack into the program counter (PC) register, effectively returning control to the calling code at the instruction immediately following the original bl call.

简述作用:return的缩写,把lr值写到pc里。

bl

The BL instruction is a 32-bit instruction used for calling subroutines in the same way function calls are made in other programming languages. When executed, the BL instruction does the following:

Save the address of the next instruction (the return address) into the link register (LR)

Calculate the target address by adding an immediate offset to the current program counter (PC) value

Change the value of the PC to the target address

总结:将下条地址存到LR中,然后改变pc跳转。

ldp x29,x30,[sp],#48

ldp 应该是load pair,这个代码是将x29和x30的值从sp指向的地址恢复,然后sp加48,我们注意到函数最开始在sp-48出存了x29和x30的值,然后sp减了48,说明执行了个代码x29,x30,sp的值都复原了,ret也就能返回正常位子了。

下面两张图是最后一道题目的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值