Mach Operating System

Mach

拜读了Mach: A New Kernel Foundation For UNIX Development,简单介绍一下这个系统。有些词我并不知道如何翻译,翻译可能不准确。

介绍

该系统提供如下新特性:

  1. 提供了最早的对tightly coupled 多核和loosely coupled 多核的支持,将 process 的abstraction分为tasks and threads。

  2. 提供了新的虚拟内存设计包括如下设计
    i. large, sparse virtual address space. 虚拟内存地址
    ii. copy-on-write virtual copy operations.写时复制技术
    iii. copy-on-write and read-write memory sharing between tasks. (任务间的写时复制技术和内存共享)and memory mapped files(文件实际存放的位置和虚拟内存地址映射起来).
    介绍:memory mapped files
    iv. 一个基于权限(capability based)的task间通信机制。这个机制可扩展至网络界限外,并保证capability protection(权限隔离)。

  3. 提供了一些系统工具,包括
    i. adb debugger
    ii. 远程文件访问
    iii. 对remote-procedure call的语言支持,包括C,pascal.

设计

计算环境

开发mach系统的背景
 之前UNIX使用pipe作为最基础的IPC机制。在越来越多的不同需求出现后,unix作出了很多改动来管理资源。Linux也提供了其他诸如 System V的机制,用以支持 ioctl. 这些改动的结果是出现了越来越多的system call和越来越多的选项。在单unix机器和多unix机器组成的系统中访问资源变的不再uniform。
 
 分布式环境和多核处理器的复杂度不断增加,需要回到最初的unix 模型中的一致,恒定的接口来访问系统资源。
 
 该系统的开发让用户可以自己定义processes为用户提供服务,而在之前想这样做必须直接修改os的代码。

MACH对系统资源的抽象(abstraction)

”The Mach kernel abstractions, in effect, provide a base upon which complete system environments may be built. The actual system running on any particular machine is a function of its servers rather than its kernel.“
mach提供了很多方法(function).实际上在运行的系统是mach系统所的function而不是mach的内核。
- 是不是想起了微内核架构
  • 任务task:线程的运行环境,是系统资源分配的最小单位。一个task包含了分页的虚拟内存空间以及隔离的访问系统资源权限。Linux对task的描述是一个process,只有一个thread execution of control.
  • 线程thread: 最小的使用cpu的单位。可以把它当成一个task中的program counter。在task中的所有线程共享task的资源。
  • port: 通信的信道,是一个由内核保护的包含message的queue。包括send和receive这两个operation.
  • message: typed collection of data objects used in communication between threads. 被赋予类型的数据对象。用于线程间的通信。

创建任务或者thread的过程会向代表该对象(指任务或者线程)的port返回访问权限。这个port可以被用来操作该任务。mach从task和thread的port中接受到来的信息(message),并根据该该message对正确的object执行操作。这个特性允许一个线程发送suspend指令给另一个thread,即使是在两个线程不再同一个node上运行。

MACH的设计

  1. the underlying port mechanism for communication provides support for object-style access to resources and capability based protection as well as network transparency.
  2. all systems abstractions allow extensibility both to multiprocessors and to networks of uniprocessor or multiprocessor nodes,
  3. support for parallelism (in the form of tasks with shared memory and threads) allows for a wide range of tightly coupled and loosely coupled multiprocessors提供了并行化的支持
  4. access to virtual memory is simple, integrated with message passing, and introduces no arbitrary restrictions on allocation, deallocation and virtual copy operations and yet allows both copy-on-write and read-write sharing. 能很容易地访问虚拟内存,提供了新的完善的内存分配特性。
任务和线程定义的诞生

在linux一个服务器应用会包括 process slots, file descriptor slots and page tables.

在原先的linux中,在一个有n个处理器的机器上,一个应用必须要创建n个进城才可以使用所有的处理器。

mach将task的抽象修改为task和thread。 Thread 是计算的最小单位,task是系统资源分配的最小单位。Task 是高overhead(性能/内存开销)的,很像一个进程(process)。Mach 允许多个thread存在于一个task中。在多核系统中,多个thread也许也可以并行运行(run in parallel).

对Task和thread上的操作是由向对应的代表该thread或者thread的port发送消息发起。Thread可以被suspend,resumed, destroyed,created。如果对task执行suspend和resume,会对其下的所有thread执行这两个operation。task也可以被destroyed和created。

task之间的关系是树形结构,该树形结构由task create这个操作产生。标准的unix fork operation即一个有一个线程的task 创建另外一个有单个线程的task,内存为copy-on-write。

MACH应用程序并行化 parallelism
应用程序并行可以由如下几个方式达到:
  1. 通过在共享的address space(地址空间)中创建一个由很多thread execution control的task,这些thread 使用共享的内存进行同步化和线程间通信。
  2. 通过task create这个操作来创建很多task(即这些task之间的关系为某task创建了某个task的关系),这些task共享了特定的内存区域。(restricted region of memory)
  3. 通过message来创建很多task。

mach的虚拟内存管理

mach允许应用程序进行如下内存操作:

  1. 分配local memory
  2. 取消local memory 分配
  3. 设置某个区域内存的隔离
  4. 设置区域间的内存间的继承关系
mach的fork:

当一个fork被唤起的时候,根据parent的地址的继承的值,mach会为新task创建一个新的内存map。继承的值可以为shared,copy或者none,可以为每一个page设置单独的继承的值。

  1. 被标示为shared的页,被parent和child(地址空间)共享。child和parent map都有read和write的权限
  2. 被标示copy的页会被复制到child的map中。
  3. 被标示为none则标示这个page完全不会被传递给子address map(地址空间)。
    新的分配的内存全部copy-on-write。
Protection 隔离机制
  1. protection的设置也可以到达page这一层。
  2. 对每一个page组,存在两个 protection值,当前值 current和最大值 maximum. protection值表示了硬件的权限(permission)。
  3. current可以改变,maximum只可以被降低。
  4. current和maximum都由read,write 和execute的组合构成。
  5. 必须要得到machine硬件的支持。(比如有些machine并不支持分配execute的权限,支持的机器就可以使用mach这样的隔离机制。)

在这里插入图片描述
The resulting address map will be a one megabyte address space, with the first 64K read-only and the range from 32K to 128K will be shared by children created with the fork operation.
前64k read only,32-128k在执行fork时,会与子任务共享。

Mach 处理page faults和page-out的operation

虚拟内存在被创建时,可能会带一个pager作为该部分memory的文件系统。当page fault出现时,内核会将fault 变成一个从文件系统中获得数据的请求。
mach提供了基础的paging的服务,没有pager的内存部分会自动的设置为空,page-out会在默认的文件系统中执行。

虚拟内存的实现

mach实现了 machine-dependent data(由机器不同而不同的数据)和 machine-independent data(与机器无关的数据)。

虚拟内存的实现使用了如下的数据结构:

  1. address maps: doubly linked lists of map entries, each entry describing the properties of a region of virtual memory. There is a single address map associated with each task.即每一个任务都有其对应的address map,来表述其所在的virtual memory.

  2. share maps: special address maps that describe regions of memory that are shared between tasks. A sharing map provides a level of indirection from address maps, allowing operations that affect shared memory to affect all maps without back pointers. 所有任务共享的map。提供了从address map出来的indirection导流。在没有back pointer的情况下,使得对共享内存的操作成为可能。

  3. VM objects: units of backing storage. A VM object specifies resident pages as well as where to find non-resident pages. VM objects are pointed at by address maps. Shadow objects are used to hold pages that have been copied after a copy-on-write fault. VM object不仅定义了resident page,也定义了non-resident page。vm object 由address map中的指针指向。shadow object用来存储在产生copy-on-write fault时被复制的page。

  4. page structures: specify the current attributes for physical pages in the system (e.g., mapped in what object, active/reclaimable/free). page structure用于存储当前系统中物理页的attribute。
    在这里插入图片描述
    在这里插入图片描述

    在machine-dependent section和machine independent section 中的虚拟内存的实现是不相同的。在machine-dependent section的virtual memory的实现中,该实现是基于所有该机器虚拟内存的信息。machine-independent section的实现则包括了一个简单的隔离/确认接口。
    这种实现的优点:

  5. 对于不同部分(section)来说,页page的大小的可以不相同

举个🌰:在一个machine中 machine dependent page 在vax这个寄存器上的大小为512k,machine independent page的大小通常为512k* 2^n.

进程间通信 Port 和message:Mach interprocess communication

Port is the basic mach transportation abstraction. Port 是被隔离的kernel 对象。message
可以被由task放进port里,也可以从port中移除。
Operations on a window are requested by a client task by sending a message to the port representing that window. Ports used in this way can be thought of as though they were capabilities to objects in an object oriented system。
message包含一个长度确定的header以及不同大小的不同类型属性的对象。message也许可以包括port的capability以及嵌入的指针。一个message最多可以传递整个task的address space。
message可以同步被接受或者异步接受。在unix系统中,signal可被用于处理在一个task control flow 之外的incoming message。一个task可以创建或者分配线程来处理异步的event。

在这里插入图片描述
一个典型的message 交互过程如图所示:
task A向port 2 发送了一条信息。taskA有向port2 发送message和从port1 接收消息的权利。 At some later time, task B which has receive rights to port P2 receives that message which may in turn contain send rights to port P1 (for the purposes of sending a reply message back to task A). Task B then (optionally) replies by sending a message to P1.

在这里插入图片描述
当port2 存满的的时候,task a可以等待port2不再满的时候再发送消息,即suspend这个发送的过程,也可以使这个发送消息的operation返回一个错误code,也可以让kernel代为接收这个消息,但kernel代收的前提是在kernel向a告知a给b的消息已经被post之前,a不能给b发送消息。

Copy-On-write

The copy-on-write data then resides in a temporary kernel address map
until task B receives the message. At that point the data is removed from
the temporary address map. The operating system kernel determines where in
the address space of B the newly received message data is placed, allowing the
kernel to minimize memory mapping overhead. Any attempt by either A or B
to change a page of this copy-on-write data results in a copy of that page being
made and placed into that task’s address space.

上图展示了copy-on-write的mechanism。在A的address space中,被发送的message会被标记为copy-on-write,每一个被标记为要被copy的页会被copy到A 的address space中。数据会先被放到暂时的kernel address map中直到b收到这个消息,当数据被这个address map中清除的时候,内核会决定在b的什么地方放置这个消息,并且预留空间给b存放这个message,但是并不copy数据到该位置。只有在对改变该data的操作发生时,os才将a上的数据copy到b。

MACH Network communication and security网络交流以及安全。

mach内核本身并没有提供任何同一个网路间的进程间通信的工具。但是mach内核的interprocess communication被用户层的task扩展。这个task被称作 network server。对于在远程的节点上的task来说,一个network server 很好的扮演了一个本地的representative。被发送到远程接收者(发送给远程端口的消息)的message,很多时候是被发送给本地的一个local server。

当一个task发送一个message到另一个节点上的目标端口时,这个task并不知道最终的节点是否对于在执行task的node来说是本地的(不知道目标port的节点是本地节点还是远程节点)。
通过加密措施,mach的安全的保证措施可以被扩展到网络环境中。

一个节点上的network servers 一起实现了网络端口(network port)的抽象。network port是对网络形式的port ,(network port is the network representation of a port to which tasks on more than one node have access rights. )只有一个不超过节点有访问的权限。A network server maintains a mapping between network ports (accessible to tasks on its node) and their corresponding local ports。 network server 维护了一个map,从network ports到他们对应的本地端口。
In operation, when a network server receives a message from a task trying
to send a message to a remote destination port, it maps the local destination
port into a destination network port identifier. The network server then derives
the address of the destination node from the network port identifier and sends
the message over the network to this node. The destination network server,
on receiving the network message, maps the network port identifier into a local
destination port and forwards the message to its ultimate destination. Each
network server holds receive rights to those network ports for which the receive
rights to the corresponding local ports are held by local tasks. Send and ownership rights to network ports are handled in the same way except that send rights
to a network port may be held by many network servers. Messages are typed
collections of data objects and any message may contain port access rights. Network servers must examine the type tags of data sent or received in messages
over the network to recognize the transmission of such access rights and take
appropriate action

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值