2017-2018-1 20179219 "Linux kernel Fundamentals and analysis" Fifth week assignment

Source: Internet
Author: User
Tags function definition function prototype volatile

Lab Operation:

The 64-bit system call function you selected is the socket number 41st.

The function prototype is an int socket (int domain, int type, int protocol); Used to create sockets, commonly used in network programming

Function parameters:

Domain protocol family, commonly used protocol families have af_inet, Af_inet6, af_local, respectively, to create a IPv4 socket, IPv6 socket, local process communication sockets
Type socket types, common with Sock_stream, SOCK_DGRAM, Sock_raw, respectively, for creating streaming sockets, datagram sockets, raw sockets
Protocol protocol, common by Ipproto_tcp, IPPTOTO_UDP, respectively, the use of TCP protocol, UDP protocol

  

The code for the Experiment program is:

1 #include <sys/types.h>2 #include <sys/socket.h>4int  main ()  5{
int temp; 6 Socket (Af_inet,sock_raw,0);
Temp=socket (2,3,0);
7 printf ("Return temp%d\n", temp); 8 return 0 ; 9 }

Find the Af_inet definition:

Find the SOCK_RAW definition:

The experimental actions are:

The above results indicate that the header file #include<stdio.h> is missing, and no warning is added.

Using the embed assembler code to implement the program, the code is:

#include <sys/types.h> #include <sys/socket.h> #include <stdio.h>int main () {int temp;asm volatile (" int $0x80 ":" =a "(temp):" A "(" a "()," B "(2)," C "(3)," D "(0)//eax The interrupt number, EBX in the parameter 2,ecx put parameter 3,edx in the parameter 0);
printf ("Return temp%d\n", temp); return 0;}

Run as:

Study notes:

Kernel state: At high execution level, code can execute privileged instruction to access any physical address, at which point the CPU execution level corresponds to the kernel state
User status: In low-level state, the code is limited in scope and can only be active within the level allowed
Execution Level 0 maximum indicates kernel state, the higher the number level the lower
Protect the scene: Enter the interrupt procedure to save the data of the register to be used
Resume the scene: Exit the terminal program, restore the data to save the Register
System call: The operating system interacts with the hardware device to provide a set of interfaces for the user-state process
API: Application Programming Interface
The difference between an API and a system call: The API is just a function definition; a system call sends a clear request to the kernel via a soft interrupt
Interrupt vector: The entry address of the Interrupt service program
System invocation: A set of program interfaces or application programming interfaces that comprise all system calls provided by the operating system implementation
Register Pass parameter restriction: the length of the parameter must not exceed the register length and cannot exceed 32 bits; no more than 6 parameters (EBX,ECX,EDX,ESI,EDI,EBP) outside of the system call number (EAX)

C Language inline assembly syntax:
__ASM__VOLATILE__ (
Assembly statement template;
Output part;
Input part;
Destruction of the description section);
Volatile means not to allow the compiler to optimize the code, you can not write
eg.
#include <stdio.h>
{
/*val1+val2=vla3*/
unsigned int val1=1;
unsigned int val2=2;
unsigned int val3=0;
printf ("val1:%d,val2:%d,val3:%d\n", VAL1,VAL2,VLA3);
ASM volatile (
"Movl $0,%%eax\n\t"//clear eax to 0
"Addl%1,%%eax\n\t"//eax+=vla1
"Addl%2,%%eax\n\t"//eax+=vla2
"Movl%%eax,%0\n\t"//val2=eax
: "=m" (VLA3)//Output section. =m to write memory.
: "C" (Val1), "D" (val2)//input section. C means ecx,d represents edx, which is stored val1 with ECX, with edx storage val2
);
printf ("val1:%d+val2:%d=vla3:%d\n", VAL1,VAL2,VAL3);
return 0;
}

2017-2018-1 20179219 "Linux kernel Fundamentals and analysis" Fifth week assignment

Related Article

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.