在内存中C和资源保护 [英] C and resource protection in memory

查看:150
本文介绍了在内存中C和资源保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们编译一个C程序,它只是产生一些机器可理解code。这code可以直接在硬件上运行,从<一讲href=\"http://stackoverflow.com/questions/23338684/does-a-c-c-program-run-the-cpu-or-on-the-kernel\">this问题。

所以我的问题是:


  1. 如果一个C程序可以直接在硬件上运行,怎么能内核来处理该计划的资源分配?


  2. 如果从编译器生成的可执行文件是在纯机器可理解的形式,那怎么办特权和非特权模式工作的?


  3. 如何内核管理硬件资源的权限,如果一个程序可以在硬件上无法通过内核直接运行?



解决方案

  

如果一个C程序可以直接在运行
  硬件如何处理内核资源分配给该
  程序。


内核负责管理的计算机的资源作为一个整体,包括如硬件资源。这意味着,用户级应用程序能够访问之类的硬件设备,写入到一个终端或读取文件,他们必须请求许可内核。这是通过使用完成的系统调用的由OS暴露,如通过@Marcus提到

不过,我不会说的程序运行的直接的关于在感硬件它不直接与硬件交互,作为一个内核模块/驱动器将。客户端程序将设置为参数的系统调用,然后的中断的内核和等待,直到内核的服务的中断请求制作的节目。

这就是为什么今天的操作系统被认为在运行的保护模式的,而不是过去的日子,当他们在跑的实模式的和程序可以,例如,更动硬件资源直接 - 并且可能搞砸。

如果你尝试写一个平凡的Hello World,在x86汇编程序这种区分变得非常清楚。我写和记录<一个href=\"https://en.wikipedia.org/wiki/X86_assembly_language#.22Hello_world.21.22_program_for_Linux_in_NASM_style_assembly\"相对=nofollow>这个几年前,转载如下:

 ;
;这个程序在32位保护模式下运行。
;打造:NASM -f精灵-F刺名.asm
;链接:LD -o名name.o
;
;在64位长模式下可以使用64位的寄存器(例如RAX代替EAX,RBX而不是EBX等)
;此外,在构建命令更改-f小精灵为-f ELF64。
;
段.data;用于初始化数据段
STR:DB世界,你好!,0AH;新行字符消息字符串结尾(10进制)
str_len:EQU $ - 海峡;字符串的长度Calcs(计算)(字节)减去STR的起始地址
                                            ;从这个地址($符号).text段;这是code节
全球_start; _start的入口点,需要由被看到全球范围内
                                            ;在C / C连接--equivalent主()++
_start:; _start过程的定义从这里开始
    MOV EAX,4;指定SYS_WRITE功能code(从OS向量表)
    MOV EBX,1;指定文件描述符标准输出--in的GNU / Linux,一切都作为文件处理,
                                             ;即使硬件设备
    MOV ECX,STR;举动开始_address_字符串消息到ECX寄存器
    MOV EDX,str_len;移动消息的长度(以字节为单位)
    INT 80H;中断内核来执行我们刚建立的系统调用 -
                                             ;在GNU /通过内核请求的Linux服务
    MOV EAX,1;指定sys_exit功能code(从OS向量表)
    MOV EBX,0;指定返回code的OS(零告诉OS一切正常)
    INT 80H;中断内核执行系统调用(退出)

请注意程序如何设置写入系统调用, SYS_WRITE ,然后指定在哪里写,是标准输出,串写,等等。

在换句话说,程序的本身不执行写入操作;它集东西,并通过使用特殊的中断请求内核做代表它, INT 80H

当你去餐馆吃饭,这里的一个可能的比喻可能。服务器将您的订单,但厨师是一个会做做饭。在这个比喻中,你是用户级应用程序,服务器以你的食物顺序是系统调用,并在厨房里的厨师是操作系统内核。


  

如果从GCC生成的可执行文件位于纯音机
  理解的形式的话怎么办特权和非特权模式
  工作?


从previous部分键控关闭,用户级程序始终运行在的用户的模式。当程序需要访问的东西(如终端,读文件等),它集东西,与上面的 SYS_WRITE 的例子,并要求内核做它于代中断。中断导致程序进入的内核的模式,并保持,直到内核已经完成了服务于客户端的请求,可呈现可包括完全否认它(例如试图读取用户没有权限的文件读)。

在内部,它的系统调用是负责发放 INT 80H 指令。用户级应用程序只看到系统调用,这是客户端和OS之间的通用接口。


  

如何内核管理硬件资源时的权限
  程序可以在硬件无法通过内核直接运行?


如果你遵循了previous解释,你现在可以看到内核充当的把关的各项方案通过使用 INT在此门敲 80H 指令。

When we compile a C program, it just generates some machine-understandable code. This code can directly run on the hardware, telling from this question.

So my questions are:

  1. If a C program can directly run on the hardware, how can the kernel handle the resource allocation for this program?

  2. If the executable generated from the compiler is in pure machine-understandable form, then how do the privileged and non-privileged modes work?

  3. How does the kernel manage the permission of hardware resources if a program can directly run on the hardware not through the kernel?

解决方案

If a C program can directly run on the hardware how can kernel handle the resource allocation to this program.

The kernel is responsible for managing the resources of the computer as a whole, including resources like the hardware. This means that, for user-level applications to be able to access things like hardware devices, write to a terminal, or read a file, they have to ask the kernel for permission. This is done by using the system calls exposed by the OS, as mentioned by @Marcus.

However, I wouldn't say that the program runs directly on hardware in the sense that it does not interact with the hardware directly, as a kernel module/driver would. A client program will set up the arguments for a system call and then interrupt the kernel and wait until the kernel services the interrupt request the program made.

This is why OSes today are said to run in protected mode, as opposed to the old days when they ran in real mode and a program could, for example, mess around with hardware resources directly --and potentially screw things up.

This distinction becomes very clear if you try writing a trivial "hello world" program in x86 assembly. I wrote and documented this one several years ago, reproduced below:

;
; This program runs in 32-bit protected mode.
;  build: nasm -f elf -F stabs name.asm
;  link:  ld -o name name.o
;
; In 64-bit long mode you can use 64-bit registers (e.g. rax instead of eax, rbx instead of ebx, etc.)
; Also change "-f elf " for "-f elf64" in build command.
;
section .data                           ; section for initialized data
str:     db 'Hello world!', 0Ah         ; message string with new-line char at the end (10 decimal)
str_len: equ $ - str                    ; calcs length of string (bytes) by subtracting the str's start address
                                            ; from this address ($ symbol)

section .text                           ; this is the code section
global _start                           ; _start is the entry point and needs global scope to be 'seen' by the
                                            ; linker --equivalent to main() in C/C++
_start:                                 ; definition of _start procedure begins here
    mov eax, 4                   ; specify the sys_write function code (from OS vector table)
    mov ebx, 1                   ; specify file descriptor stdout --in gnu/linux, everything's treated as a file,
                                             ; even hardware devices
    mov ecx, str                 ; move start _address_ of string message to ecx register
    mov edx, str_len             ; move length of message (in bytes)
    int 80h                      ; interrupt kernel to perform the system call we just set up -
                                             ; in gnu/linux services are requested through the kernel
    mov eax, 1                   ; specify sys_exit function code (from OS vector table)
    mov ebx, 0                   ; specify return code for OS (zero tells OS everything went fine)
    int 80h                      ; interrupt kernel to perform system call (to exit)

Notice how the program sets up the write system call, sys_write, and then specifies the file descriptor of where to write, being stdout, the string to write, and so on.

In other words, the program itself does not perform the write operation; it sets things up and asks the kernel to do it on its behalf by using a special interrupt, int 80h.

A possible analogy here might be when you go to a restaurant. The server will take your order, but the chef is the one that will do the cooking. In this analogy, you are the user-level application, the server taking your food order is the system call, and the chef in the kitchen is the OS kernel.

If the executable generated from the gcc is in pure machine understandable form then how do the privileged and non-privileged mode work?

Keying off from the previous section, user level programs always run in user mode. When the program needs access to something (e.g. terminal, read a file, etc.), it sets things up, as with the sys_write example above, and asks the kernel to do it on its behalf with an interrupt. The interrupt causes the program to go into kernel mode and remains there until the kernel has completed servicing the client's request --which may include denying it altogether (e.g. trying to read a file the user has no privilege to read).

Internally, it's the system call that's responsible for issuing the int 80h instruction. User-level applications just see the system call, which is the common interface between the client and the OS.

How does the kernel manage the permission of hardware resources when a program can directly run on hardware not through the kernel?

If you followed the previous explanations, you can now see that the kernel acts as a gatekeeper and that programs "knock" on this gate by using the int 80h instruction.

这篇关于在内存中C和资源保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆