Java development system Kernel: Let the kernel recover from critical errors

Source: Internet
Author: User

For a more detailed explanation and code debugging demonstration process, see the video
Developing C language compilers in Java

For a more detailed explanation and code debugging demonstration process, see the video
How to get into Google, algorithm interview skills Comprehensive Promotion guide

If you are interested in machine learning, please refer to the link:
Machine learning: An Introduction to neural networks

For a more detailed explanation and code debugging demonstration process, see the video
Linux kernel Hacker, build your own kernel from zero

Microsoft's early DOS system, there is a serious problem is that if the application is running a problem, it will cause the whole system to collapse completely, our current system kernel also exists this problem, such as open api_call.asm, its contents are as follows:

[section. S32]
BITS 32
Call Main
Retf

Api_putchar:
mov edx, 1
mov al, [www.vboyule66.cn ESP + 4]
int 02Dh
Ret

%include "App.asm"
Call main when the CPU control is submitted to the application, execute the application code, after the application is finished, return to the call main statement of the next instruction to continue execution, call Main's next statement is RETF, its role is to get the kernel code from the stack global descriptor, Assign the descriptor in the Descriptor table subscript to register CS, at the same time take the address of the kernel to execute the statement from the stack, and assign a value to the register IP, so that the CPU controller will be re-returned to the kernel, suppose we make a change to the above code is as follows:

[section. S32]
BITS 32
Call Main
a pop eax; intentionally causing an error in the return address to cause an abnormal protection interruption
Retf
We execute the RETF statement, the kernel stored on the stack to execute the address of the statement popped, so that the return of the kernel address will be compromised, when the execution of the RETF statement, the IP pointer will execute any one of the memory of the place, so the CPU in the next execution will encounter an error, The above code is modified to run, as follows:
Write a picture description here

Since the IP pointer points to an invalid address, causing the CPU to execute an invalid instruction and cause the overall system to collapse, let's look at how to handle the system to recover from this critical error, which can prevent the application from executing malicious code and damaging the system.

When the CPU in the execution of the instruction error, for example, encountered an invalid instruction, then the CPU will start 0Dh interrupt, let the interrupt to handle the current abnormal situation, if the interrupt can not be processed, then the CPU will stop running, so there is the above situation. In order for the system to recover from the error, we need to implement the 0Dh interrupt, in the interrupt, we directly end the current running application, directly to the CPU control to return to the kernel.

We are now in the KERNEL.ASM register 0Dh interrupt, the code is modified as follows:

Label_idt:
%rep 13
Gate SelectorCode32, spurioushandler,0, da_386igate
%endrep

.0Dh:
Gate SelectorCode32, www.yszxylpt.com exceptionhandler,0, da_386igate

%rep 18
Gate SelectorCode32, www.boayulevip.cn spurioushandler,0, da_386igate
%endrep
The above code registers a 0Dh interrupt in the Interrupt Descriptor table, and the CPU calls the Exceptionhandler function when the interrupt is sent. Let's look at the implementation of this function, also in kernel.asm:

_exceptionhandler:
Exceptionhandler equ _exceptionhandler-$$
Cli
To switch the memory segment to the kernel.
mov ax, Selectorvram
MOV ds, ax
MOV es, ax
mov ax, www.vboyl130.cn selectorstack; switch to kernel stack segment
mov ss, ax

mov ecx, [0xfe4]; get the kernel stack pointer
Add ecx,-8
mov [ecx+4], SS; Save broken stack segment
mov [ECX], esp; save interrupt when stack pointer

mov esp, ecx; toggle kernel pointer

Call Inthandlerforexception

. Kill: Kills the application directly by handing the CPU over to the kernel
mov esp, [0xfe4]
STi
Popad
Ret
When the interrupt is run, the description of the kernel proprietary memory segment is assigned value to the register ds,es, so that the code can read and write the kernel's data directly while the kernel's stack description is assigned to the Register SS, so that the stack used by the code is the proprietary stack of the kernel. The stack segment descriptor and stack pointer used by the application at the time of the error are then stored in the kernel stack, and then the function inthandlerforexception is called for error handling, which is implemented in the C-language part of the kernel, which is the Write_vga_ In Desktop.c, let's look at the implementation of this function:

void inthandlerforexception (int *esp) {
g_console.cur_x = 8;
Cons_putstr ("INT 0D");
g_console.cur_x = 8;
G_console.cur_y + = 16;
Cons_putstr ("General Protected Exception");
G_console.cur_y + = 16;
g_console.cur_x = 8;
return 1;
It's very simple, just print two lines of string on the console, "INT OD", and "General Protected Exception", then return. Back to the Interrupt implementation section, the code for. Kill, when the kernel gives control to the application, it stores its stack pointer at the memory [0xfe4], and the kernel passes control over Start_app to the application, and we look at the Start_app code:

Start_app: void Start_app (int eip, int cs,int esp, int ds)
Cli
Pushad
...
mov [0XFE4], ESP
...
As you can see, Start_app, when executed, stores the data of all general-purpose registers on the stack by instruction, while storing the value of the ESP pointer, that is, the kernel stack pointer at the time of memory 0xfe4 this place, so. Kill instruction mov ESP [0XFE4] In fact, the above instruction mov [0xfe4], esp inverse operation, while Popad is also on the above instruction Pushad inverse operation. Let's see how Start_app is called, in WRITE_VGA_DESKTOP.C:

void Cmd_hlt () {
....
Start_app (0, 11*8,64*1024, 12*8);

Char *papp = (char*) (q + 0x100);
Showstring (Shtctl, Sht_back, 0, 179, COL8_FFFFFF, PAPP);

MEMMAN_FREE_4K (Memman, Buffer.pbuffer, buffer.length);
memman_free_4k (Memman, Q, 64 * 1024);
Start_app is called in cmd_hlt, and we know that when the C language calls a child function, it presses the address of the next instruction to be executed after calling the child function into the stack, so when the code above calls the Start_app function, the next instruction, Char *papp= ( char*) The address of this statement is pressed into the stack.

When the code at. Kill executes the statement Popad, the stack is stored on exactly the address of the statement Char *papp= (char*), so when we execute the instruction RET, the address of the statement is assigned to the IP register, so that the CPU is directly from Char *papp= ( char*) This statement begins execution, which is similar to what happens when the CPU control is returned to the kernel normally after the application has completed normal execution.

This way, when the application executes a critical error, the CPU triggers a 0DH exception handling interrupt, in which the code returns the CPU control directly to the kernel, which is tantamount to destroying the abnormal application, and the kernel continues to run in the same way as if nothing had happened. After the above code is modified, after executing the wrong application, the situation is as follows:

Write a picture description here

Thus, when the error occurs, the 0Dh interrupt is actually called, and the two lines of string are displayed to the console, and the kernel can continue to function normally without the whole system crashing down like the previous example.

For more detailed explanations and code demos, see the video.

Java development system Kernel: Let the kernel recover from critical errors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.