I think microkernel meaning (2) - Analysis of zircon

 Brief introduction

Kernel messages are several different types of objects. These objects can be called directly by syscall, and these objects are C ++ classes that implement the interface by the scheduler, located in kernel / object directory. Many are self-contained high order, some containing low-level primitives lk.

syscall

Interactive user space code and kernel objects by syscall, most simply by Handle. In user space, a 32-bit integer Handle (zx_handle_t). Handle process exists in the table, the table Handle has a parameter, when the process initiated syscall, the kernel will check the parameters of the actual launch Handle calls process. Further examination Handle type the correct kernel (delivery thread Handle Handle to the needs of syscall event will cause an error), and Handle must have the required permissions to operate the request.

From the perspective of access syscall may be divided into three categories:

1. There is no limit (rarely), for example zx_clock_get (), zx_nanosleep () can be called from any thread.

2. Handle required as a first parameter, which indicates an object to be operated (and many major), e.g. zx_channel_write () and zx_port_queue ().

3. Create a new object, but does not require Handle, such as zx_event_create (), zx_channel_create (). Job syscall invoked by the process control belongs.

syscall provided by libzircon.so, libzircon.so is a virtual shared library provided by Zircon kernel to user space (see virtual Dynamic Shared Object or vDSO). They are C ELF ABI functions, format zx_noun_verb () or zx_noun_verb_direct-object ().

Syscalls.abigen defined by the system call, the process comprising the abigen tools and the glue code libzircon file, as well as the kernel syscall library.

Handles and Permissions

Objects can be linked to multiple Handles (1 or multiple processes).

For almost all of the objects associated with it when the last of the Handle is closed, the object is destroyed or irreversibly into the final state.

Handles may be achieved by writing Channel moving from one process to another process; can also zx_process_start () passed as a parameter to the Handle first thread of a new process.

Handle operation is governed by permissions or objects associated with the Handle. Two Handles associated with the same object may have different permissions.

The syscall zx_handle_duplicate () and zx_handle_replace () Handles may additionally be used to give the same object, the Handle can be obtained as before, may be reduced permissions. zx_handle_close () closes Handle, Handle objects close associate of release, if it is closed Handle is the last Handle associated with the object, the object will be released. zx_handle_close_many () for closing a set Handles.

Kernel object ID

Each kernel object has an ID, a 64-bit unsigned integer, can be used to identify an object, and is unique in the system operation. This also means that the kernel object ID can not be reused.

There are two special objects on the river ID: ZX_KOID_INVALID is 0, are used as sentries; ZX_KOID_KERNEL only one core has its own ID.

Run the code: Jobs, processes and threads

Jobs contain process, and defines the various resource constraints. Jobs attributable to the parent Jobs, until the root Job, created by the kernel and root Job sent userboot.

No Job Handle, a process where the thread is not possible to create another process or Job's.

Loader has user-space implementation.

Messaging: Sockets and channel

Socket and IPC channel is subject, it is two-terminal bi-directional. Creating a Socket or Channel will return two Handles are related to the endpoint object.

Socket stream oriented, as one or more data bytes written or units alone. Short write (buffer is not enough) and short-read (to read than the multi-cache) are supported.

通道是面向数据报的,有最大消息长度64K(可以配置的更小),一个消息可以最多附属1024个Handles。不支持短读短写,一个消息要不就是合适,要不就是不合适。

当一个消息写入通道,其就会被从发送进程中切除,当接收进程把一个消息和Handle从通道中读出,这个Handles就会被加入到接收进程。在这两个事件期间,Handles会一直存在,除非通道另一端的接收进程关闭了端点,其中的所有Handles都关了。

对象和信号

对象最多可以有32个信号(由zx_signals_t类型和ZX_SIGNAL宏定义),这些信号可以代表一部分对象的当前状态:比如通道和Socket所处的可读或可写的状态;进程和线程则可能是TERMINATED。还有其他状态不列举。

线程可能等一到多个对象的信号来唤醒。

等待:等待一或多或端口

一个线程可能使用zx_object_wait_one()或zx_object_wait_many()陷入等待一到多个信号(一或多个Handles的);这两个syscalls都可以设置超时。

如果一个线程要等待大量的Handles,更高效的做法是使用端口;端口是一个可以绑定其他对象的对象,当信号插入到对象,端口会收到一个包含信号源信息的包。

事件和事件对

事件是最简单的对象,除了采集活跃信号外没有其他动作。

一个事件对是一对可以互相发出信号的事件。一个有用的属性就是,当一个事件对的一端关闭(所有关联的Handles都关闭),另一端就会发生PEER_CLOSED信号。

共享内存:虚拟内存对象

虚拟内存对象表示一组内存物理页面(或等待缺页中断建立映射的页面)。

zx_vmar_map()和zx_vmar_unmap()分别可以将其映射进入进程或断开映射。映射页的权限可以用zx_vmar_protect()调整。

虚拟内存对象也可以通过zx_vmo_read()和zx_vmo_write()直接读或写入。用映射的方式使用可以避免写入和读取,直接在其上操作,并发送到其他进程。

地址空间管理

虚拟内存地址克难攻坚提供管理进程内存空间的抽象。在进程创建时,根虚拟内存地址空间的Handle会提供给进程创建者,这一Handle会关联到一个VMAR,这个VMAR跨有整个地址空间;这个空间可以通过zx_vmar_map()和zx_vmar_allocate()简历到物理地址的映射。zx_vmar_allocate()可以被用于生成另外一个虚拟内存地址空间,被称作子空间或孩子,可以用于将地址空间的一部分组合在一起。

Futexes

Futexes是内核原语,用于用户空间原子操作,实现高效的同步原语;比如Mutexes,只需要在竞争的场合发起一个syscall即可;一般来说,他们只被是现在标准库中。Zircon的libc和libc++为互斥、条件变量等提供C11、C++和线程的API,基于Futexes实现。

发布了24 篇原创文章 · 获赞 3 · 访问量 2336

Guess you like

Origin blog.csdn.net/ytfy339784578/article/details/103946521