±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚcnblogs£¬Ö÷Òª½²½âÁ˸ÅÄģʽ£¬º¯Êý£¬Ôõô×ö¡£ÓÉdzÈëÉîÏ£Íû¶Ô´ó¼ÒÓаïÖú¡£ |
|
1. ʲôÊDzÙ×÷ϵͳµÄ½Ó¿Ú
¼ÈȻʹÓÃÕßÊÇͨ¹ý²Ù×÷ϵͳ½Ó¿ÚÀ´Ê¹ÓüÆËã»úµÄ£¬Äǵ½µ×ÊÇʲôÊDzÙ×÷ϵͳÌṩµÄ½Ó¿ÚÄØ£¿
½Ó¿Ú£¨interface£©Õâ¸ö´ÊÀ´Ô´ÓÚµçÆø¹¤³Ìѧ¿Æ£¬Ö¸µÄÊDzå×ùÓë²åÍ·µÄÁ¬½Ó¿Ú£¬Æðµ½½«µçÓëµçÆ÷Á¬½ÓÆðΪµÄ¹¦ÄÜ¡£ºóÀ´ÑÓÉìµ½Èí¼þ¹¤³ÌÀïÖ¸Èí¼þ°üÏòÍâÌṩµÄ¹¦ÄÜÄ£¿éµÄº¯Êý½Ó¿Ú¡£ËùÒÔ½Ó¿ÚÊÇÓÃÀ´Á¬½ÓÁ½¸ö¶«Î÷¡¢ÐźÅת»»ºÍÆÁ±Îϸ½Ú¡£
ÄǶÔÓÚ²Ù×÷ϵͳÀ´Ëµ£º²Ù×÷ϵͳͨ¹ý½Ó¿ÚµÄ·½Ê½£¬½¨Á¢ÁËÓû§Óë¼ÆËã»úÓ²¼þµÄ¹µÍ¨·½Ê½¡£Óû§Í¨¹ýµ÷ÓòÙ×÷ϵͳµÄ½Ó¿ÚÀ´Ê¹ÓüÆËã»úµÄ¸÷ÖÖ¼ÆËã·þÎñ¡£ÎªÁËÓû§ÓѺÃÐÔ£¬²Ù×÷ϵͳһ°ã»áÌṩÁ½¸öÖØÒªµÄ½Ó¿ÚÀ´Âú×ãÓû§µÄһЩһ°ãÐÔµÄʹÓÃÐèÇó£º
ÃüÁîÐУºÊµ¼ÊÊÇÒ»¸ö½Ðbash/shµÄ¶ËÖÕ³ÌÐòÌṩµÄ¹¦ÄÜ£¬¸Ã³ÌÐòµ×²ãµÄʵÖÊ»¹Êǵ÷ÓÃһЩ²Ù×÷ϵͳÌṩµÄº¯Êý¡£
´°¿Ú½çÃæ£º´°¿Ú½çÃæÍ¨¹ý±àдµÄ´°¿Ú³ÌÐò½ÓÊÕÀ´×Ô²Ù×÷ϵͳÏûÏ¢¶ÓÁеÄһЩÊó±ê¡¢¼üÅ̶¯×÷£¬½ø¶ø×ö³öһЩÏìÓ¦¡£
¶ÔÓÚ·ÇÒ»°ãÐÔʹÓÃÐèÇ󣬲Ù×÷ϵͳÌṩÁËһϵÁеĺ¯Êýµ÷ÓøøÈí¼þ¿ª·¢Õߣ¬ÓÉÈí¼þ¿ª·¢ÕßÀ´ÊµÏÖһЩÓû§ÐèÒªµÄ¹¦ÄÜ¡£ÕâЩº¯Êýµ÷ÓÃÓÉÓÚÊDzÙ×÷ϵͳÄÚºËÌṩµÄ£¬ÎªÁËÓбðÓÚÒ»°ãµÄº¯Êýµ÷Ó㬱»³ÆÎªÏµÍ³µ÷ÓᣱÈÈçÎÒÃÇʹÓÃCÓïÑÔ½øÐÐÈí¼þ¿ª·¢Ê±£¬¾³£ÓõÄprintfº¯Êý£¬ËüµÄÄÚ²¿Êµ¼Ê¾ÍÊÇͨ¹ýwriteÕâ¸öϵͳµ÷Óã¬ÈòÙ×÷ϵͳÄÚºËΪÎÒÃǰÑ×Ö·û´òÓ¡ÔÚÆÁÄ»Éϵġ£
ΪÁ˹淶²Ù×÷ϵͳÌṩµÄϵͳµ÷Óã¬IEEEÖÆ¶¨ÁËÒ»¸ö±ê×¼½Ó¿Ú×壬±»³ÆÎªPOSIX£¨Portable
Operating System Interface of Unix£©¡£Ò»Ð©ÎÒÃÇÊìϤµÄ½Ó¿Ú±ÈÈ磺fork¡¢pthread_create¡¢openµÈ¡£
2. Óû§Ä£Ê½ÓëÄÚºËģʽ
¼ÆËã»úÓ²¼þ×ÊÔ´¶¼ÊDzÙ×÷ϵͳÄں˽øÐйÜÀíµÄ£¬ÄÇÎÒÃÇ¿ÉÒÔÖ±½ÓÓÃÄÚºËÖеÄһЩ¹¦ÄÜÄ£¿éÀ´²Ù×÷Ó²¼þ×ÊÔ´Â𣿿ÉÒÔÖ±½Ó·ÃÎÊÄÚºËÖÐά»¤µÄһЩÊý¾Ý½á¹¹Âð£¿
µ±È»²»ÐУ¡ÓÐÈË»á˵£¬ÎªÊ²Ã´²»ÐÐÄØ£¿ÎÒÂòµÄµçÄÔ£¬Äں˴úÂëÔÚÄÚ´æÖУ¬ÄÇÄÚ´æ²»¶¼ÊÇÎÒ×Ô¼ºÂòµÄÂ𣿣¬ÎÒ×Ô¼º²»ÄÜ·ÃÎÊÂð£¿
ÏÖÔÚÎÒÃÇÔËÐеIJÙ×÷ϵͳ¶¼ÊÇÒ»¸ö¶àÈÎÎñ¡¢¶àÓû§µÄ²Ù×÷ϵͳ¡£Èç¹ûÿ¸öÓû§½ø³Ì¶¼¿ÉÒÔËæ±ã·ÃÎʲÙ×÷ϵͳÄں˵ÄÄ£¿é£¬¸Ä±ä״̬£¬ÄÇÕû¸ö²Ù×÷ϵͳµÄÎȶ¨ÐÔ¡¢°²È«ÐÔ¶¼´ó´ó½µµÍÁË¡£
ΪÁ˽«Äں˳ÌÐòÓëÓû§³ÌÐò¸ôÀ뿪£¬ÔÚÓ²¼þ²ãÃæÉÏÌṩÁËÒ»´Î»úÖÆ£¬½«³ÌÐòÖ´ÐеÄ״̬·ÖΪÁ˲»Í¬µÄ¼¶±ð£¬´Ó0µ½3£¬Êý×ÖԽС£¬·ÃÎʼ¶±ðÔ½¸ß¡£0´ú±íÄÚºË̬£¬ÔÚ¸ÃÌØÈ¨¼¶±ðÏ£¬ËùÓÐÄÚ´æÉϵÄÊý¾Ý¶¼ÊǿɼûµÄ£¬¿É·ÃÎʵġ£3´ú±íÓû§Ì¬£¬ÔÚÕâ¸öÌØÈ¨¼¶Ï£¬³ÌÐòÖ»ÄÜ·ÃÎÊÒ»²¿·ÖµÄÄÚ´æÇøÓò£¬Ö»ÄÜÖ´ÐÐһЩÏÞ¶¨µÄÖ¸Áî¡£
²Ù×÷ϵͳÔÚ½¨Á¢GTD±íµÄʱºò£¬½«GTDµÄÿ¸ö±íÏîÖеÄ2루4ÖÖÌØÈ¨¼¶±ð£©ÉèÖÃÎªÌØÈ¨Î»£¨DPL£©£¬È»ºó²Ù×÷ϵͳ½«Õû¸öÄÚ´æ·ÖΪ²»Í¬µÄ¶Î£¬²»Í¬µÄ¶Î£¬ÔÚGDT¶ÔÓ¦µÄ±íÏîÖеÄDPLλÊDz»Í¬µÄ¡£±ÈÈçÄÚºËÄÚ´æ¶ÎµÄËùÓÐÌØÈ¨Î»¶¼Îª00¡£¶øÓû§³ÌÐò·Ã´æÊ±£¬ÔÚ±£»¤Ä£Ê½Ï¶¼ÊÇͨ¹ý¶Î¼Ä´æÆ÷+IP¼Ä´æÆ÷À´·ÃÎʵ쬶ø¶Î¼Ä´æÆ÷ÀïÔòÓÃÁ½Î»±íʾµ±Ç°½ø³ÌµÄ¼¶±ð£¨CPL£©£¬ÊÇλÓÚÄÚºË̬»¹ÊÇÓû§Ì¬¡£
¼ÈÈ»Èç´Ë£¬ÄÇÎÒÃÇ»¹ÓÐʲô°ì·¨¿ÉÒÔµ÷ÓòÙ×÷ϵͳµÄÄں˴úÂëÄØ£¿²Ù×÷ϵͳΪÁËʵÏÖϵͳµ÷Óã¬ÌṩÁËÒ»¸öÖ÷¶¯½øÈëÄں˵ÄΩһ·½Ê½£ºÖжÏÖ¸Áîint¡£intÖ¸Áî»á½«GDT±íÖеÄDPL¸ÄΪ3£¬ÈÃÎÒÃÇ¿ÉÒÔ·ÃÎÊÄÚºËÖеĺ¯Êý¡£ËùÒÔËùÓеÄϵͳµ÷Óö¼±ØÐëͨ¹ýµ÷ÓÃintÖ¸ÁîÀ´ÊµÏÖ£¬´óÖµĹý³ÌÈçÏ£º
Óû§³ÌÐòÖаüº¬Ò»¶Î°üº¬intÖ¸ÁîµÄ´úÂë
²Ù×÷ϵͳдÖжϴ¦Àí£¬»ñÈ¡Ïàµ÷³ÌÐòµÄ±àºÅ
²Ù×÷ϵͳ¸ù¾Ý±àºÅÖ´ÐÐÏàÓ¦µÄ´úÂë
3. ÆÊÎöprintfº¯Êý
ÏÂÃæÎÒÃÇÒÔprintfº¯ÊýµÄµ÷ÓÃΪÀý£¬ËµÃ÷¸Ãº¯ÊýÊÇÈçºÎÒ»²½Ò»²½×îÖÕÂäÔÚÄں˺¯ÊýÉÏÈ¥µÄ¡£

ͼ1£ºÓ¦ÓóÌÐò¡¢¿âº¯ÊýºÍÄÚºËϵͳµ÷ÓÃÖ®¼äµÄ¹ØÏµ
printfº¯ÊýÊÇCÓïÑÔµÄÒ»¸ö¿âº¯Êý£¬Ëü²¢²»ÊÇÕæÕýµÄϵͳµ÷Óã¬ÔÚUnixÏ£¬ËüÊÇͨ¹ýµ÷ÓÃwriteº¯ÊýÀ´Íê³É¹¦Äܵġ£
writeº¯ÊýÄÚ²¿¾ÍÊǵ÷ÓÃÁËintÖжϡ£Ò»°ãµÄϵͳµ÷Óö¼Êǵ÷ÓÃ0x80ºÅÖжϡ£¶ø²Ù×÷ϵͳÖÐÒ»°ã²»»áµÄÏÔʽµÄд³öwriteµÄʵÏÖ´úÂ룬¶øÊÇͨ¹ý_syscall3ºêÕ¹¿ªµÄʵÏÖ¡£_syscall3ÊÇרÃÅÓÃÀ´´¦ÀíÓÐ3¸ö²ÎÊýµÄϵͳµ÷Óõĺ¯ÊýµÄʵÏÖ¡£Í¬Àí»¹ÓÐ_syscall0¡¢_syscall1ºÍ_syscall2µÈ£¬Ä¿Ç°×î´óÖ§³ÖµÄ²ÎÊý¸öÊýΪ3¸ö£¬ÕâÈý¸ö²ÎÊýÊÇͨ¹ýebx,
ecx,edx´«µÝµÄ¡£Èç¹ûÓÐϵͳµ÷ÓõIJÎÊý³¬¹ýÁË3¸ö£¬ÄÇô¿ÉÒÔͨ¹ýÒ»¸ö²ÎÊý½á¹¹ÌåÀ´½øÐд«µÝ¡£
// linux/lib/write.c
#define __LIBRARY__
#include <unistd.h>
//
_syscall3(int,write,int,fd,const char *,buf,off_t,count) |
// linux/include/unistd.h
#define _syscall3(type,name,atype,a,btype,b,ctype,c)
\
type name(atype a,btype b,ctype c) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name),"b" ((long)(a)),"c"
((long)(b)),"d" ((long)(c))); \
if (__res>=0) \
return (type) __res; \
errno=-__res; \
return -1; \
} |
ËùÒÔºêÕ¹¿ªºó£¬writeº¯ÊýµÄʵÏÖʵÏÖΪ£º
int write(int
fd, const char *buf, off_t count)
{
long __res;
__asm__ volatile ("int $0x80"
: "=a" (__res)
: "0" (__NR_write),"b" ((long)(a)),"c"
((long)(b)),"d" ((long)(c)));
if (__res>=0)
return (type) __res;
errno=-__res;
return -1;
} |
ÎÒÃÇ¿´µ½Êµ¼Êº¯ÊýÄÚ²¿²¢Ã»ÓÐ×öÌ«¶àµÄÊÂÇ飬Ö÷Òª¾ÍÊǵ÷ÓÃint 0x80£¬½«°ÑÏà¹ØµÄ²ÎÊý´«µÝ¸øÒ»Ð©Í¨ÓüĴæÆ÷£¬µ÷ÓõĽá¹ûͨ¹ýeax·µ»Ø¡£ÆäÖÐÒ»¸öºÜÖØÒªµÄµ÷ÓòÎÊýÊÇ__NR_writeÕâ¸öÒ²ÊÇÒ»¸öºê£¬¾ÍÊÇwirteµÄϵͳµ÷Óúţ¬ÔÚlinux/include/unistd.hÖб»¶¨ÒåΪ4£¬Í¬Ñù»¹ÓкܶàÆäËûϵͳµ÷Óúš£ÒòΪËùÓеÄϵͳµ÷Óö¼ÊÇͨ¹ýint
0x80£¬ÄÇÔõô֪µÀ¾ßÌåÐèҪʲô¹¦ÄÜÄØ£¬Ö»ÄÜͨ¹ýϵͳµ÷ÓúÅÀ´Ê¶±ð¡£
ÏÂÃæÎÒÃÇÀ´¿´¿´int 0x80ÊÇÈçºÎÖ´Ðеġ£ÕâÊÇÒ»¸öϵͳÖжϣ¬²Ù×÷ϵͳ¶ÔÓÚÖжϴ¦ÀíÁ÷³ÌÒ»°ãΪ£º
¹ØÖжϣºCPU¹Ø±ÕÖжÎÏìÓ¦£¬¼´²»ÔÙ½ÓÊÜÆäËüÍⲿÖжÏÇëÇó
±£´æ¶Ïµã£º½«·¢ÉúÖжϴ¦µÄÖ¸ÁîµØÖ·Ñ¹Èë¶ÑÕ»£¬ÒÔʹÖжϴ¦ÀíÍêºóÄÜÕýÈ·µØ·µ»Ø¡£
ʶ±ðÖжÏÔ´£ºCPUʶ±ðÖжϵÄÀ´Ô´£¬È·¶¨ÖжÏÀàÐͺţ¬´Ó¶øÕÒµ½ÏàÓ¦µÄÖжϷþÎñ³ÌÐòµÄÈë¿ÚµØÖ·¡£
±£»¤ÏÖ³¡Ëù£º½«·¢ÉúÖжϴ¦ÀíÓйؼĴæÆ÷£¨ÖжϷþÎñ³ÌÐòÖÐҪʹÓõļĴæÆ÷£©ÒÔ¼°±êÖ¾¼Ä´æÆ÷µÄÄÚ´æÑ¹Èë¶ÑÕ»¡£
Ö´ÐÐÖжϷþÎñ³ÌÐò£º×ªµ½ÖжϷþÎñ³ÌÐòÈë¿Ú¿ªÊ¼Ö´ÐУ¬¿ÉÔÚÊʵ±Ê±¿ÌÖØÐ¿ª·ÅÖжϣ¬ÒÔ±ãÔÊÐíÏìÓ¦½Ï¸ßÓÅÏȼ¶µÄÍⲿÖжϡ£
»Ö¸´ÏÖ³¡²¢·µ»Ø£º°Ñ¡°±£»¤ÏÖ³¡¡±Ê±Ñ¹Èë¶ÑÕ»µÄÐÅÏ¢µ¯»ØÔ¼Ä´æÆ÷£¬È»ºóÖ´ÐÐÖжϷµ»ØÖ¸ÁIRET£©£¬´Ó¶ø·µ»ØÖ÷³ÌÐò¼ÌÐøÔËÐС£
ǰ3Ïîͨ³£ÓÉ´¦ÀíÖжϵÄÓ²¼þµç·Íê³É£¬ºó3Ïîͨ³£ÓÉÈí¼þ£¨ÖжϷþÎñ³ÌÐò£©Íê³É¡£

ͼ2£ºÏµÍ³µ÷ÓÃÖжϴ¦ÀíÁ÷³Ì
ÄÇ0x80ºÅÖжϵĴ¦Àí³ÌÐòÊÇÊ²Ã´ÄØ£¬ÎÒÃÇ¿ÉÒÔ¿´Ò»Ï²Ù×÷ϵͳÊÇÈçºÎÉèÖÃÕâ¸öÖжÏÏòÁ¿±íµÄ¡£ÔÚ²Ù×÷ϵͳ³õʼ»¯Ê±shecd_initº¯ÊýÀµ÷ÓÃÁË
set_system_gate(0x80,
&system_call); |
ÎÒÃÇÉîÈë¿´Ò»ÏÂset_system_gateº¯Êý×öÁËʲô
#define _set_gate(gate_addr,type,dpl,addr)
\
__asm__ ("movw %%dx,%%ax\n\t" \
"movw %0,%%dx\n\t" \ "movl
%%eax,%1\n\t" \ "movl %%edx,%2"
\
: \
: "i" ((short) (0x8000+(dpl<<13)+(type<<8))),
\ "o" (*((char *) (gate_addr))),
\ "o" (*(4+(char *) (gate_addr))),
\ "d" ((char *) (addr)),"a"
(0x00080000))
#define set_system_gate(n,addr) \
_set_gate(&idt[n],15,3,addr) |
ͨ¹ýÉÏÃæµÄ´úÂ룬ÎÒÃÇ¿ÉÒÔ¿´³ö£¬set_system_gate°ÑµÚ0x80ÖжϱíµÄ±íÏîÖÐÖжϴ¦Àí³ÌÐòÈë¿ÚµØÖ·ÉèÖÃΪ&system_call¡£²¢ÇÒ°ÑÄÇÒ»ÏîIDT±íÖеÄDPLÉèÖÃÁËΪ3,
·½±ãÓû§³ÌÐò¿ÉÒÔÈ¥·ÃÎÊÕâ¸öµØÖ·¡£
ËùÒÔinit 0x80×îÖջᱻsystem_callÕâ¸öº¯ÊýµØÖ·´¦µÄ´úÂëÀ´Êµ¼Ê´¦Àí¡£ÈÃÎÒÃÇ¿´ÏÂsystem_call×öÁËʲôÊÂÇé¡£
# linux/kernel/system_call.s
nr_system_calls=72 # ×î´óµÄϵͳµ÷ÓøöÊý
.globl _system_call
system_call:
cmpl $nr_system_calls-1,%eax # eaxÖзŵÄϵͳµ÷Óúţ¬ÔÚwriteµÄµ÷Óùý³ÌÖÐΪ__NR_write
= 4
ja bad_sys_call
push %ds # ÏÂÃæÊÇһЩ¼Ä´æÆ÷±£»¤£¬ºóÃæ»¹Òªµ¯³ö
push %es
push %fs
pushl %edx
pushl %ecx # push %ebx,%ecx,%edx as parameters
pushl %ebx # to the system call
movl $0x10,%edx # set up ds,es to kernel space
mov %dx,%ds #¡¡°ÑdsµÄ¶Î±êºÅÉèÖÃΪ0001 0000(×îºó£²Î»ÊÇÌØÈ¨¼¶)£¬ËùÒԶκÅΪ4£¬ÄÚºË̬Êý¾Ý¶Î
mov %dx,%es
movl $0x17,%edx # °ÑfsµÄ¶Î±êºÅÉèÖÃΪ0001 0111(×îºó£²Î»ÊÇÌØÈ¨¼¶)£¬ËùÒԶκÅΪ5£¬Óû§Ì¬Êý¾Ý¶Î
mov %dx,%fs
call sys_call_table(,%eax,4) # ʵ¼ÊµÄϵͳµ÷ÓÃ
pushl %eax
movl current,%eax
cmpl $0,state(%eax) # state ¼ì²âÊÇ·ñΪ¾ÍÐ÷״̬
jne reschedule # ½øÈëµ÷¶È³ÌÐò
cmpl $0,counter(%eax) # counter ²é¿´ÐźÅ״̬
je reschedule
ret_from_sys_call:
movl current,%eax # task[0] cannot have signals
cmpl task,%eax
je 3f
cmpw $0x0f,CS(%esp) # was old code segment supervisor
?
jne 3f
cmpw $0x17,OLDSS(%esp) # was stack segment =
0x17 ?
jne 3f
movl signal(%eax),%ebx
movl blocked(%eax),%ecx
notl %ecx
andl %ebx,%ecx
bsfl %ecx,%ecx
je 3f
btrl %ecx,%ebx
movl %ebx,signal(%eax)
incl %ecx
pushl %ecx
call do_signal
popl %eax
3: popl %eax
popl %ebx
popl %ecx
popl %edx
pop %fs
pop %es
pop %ds
iret |
ÎÒÃÇ¿ÉÒÔ·¢ÏÖ£¬ÉÏÃæ´úÂëÖд󲿷ִúÂëÊǼĴæÆ÷״̬±£´æÓë»Ö¸´£¬¶ÑÕ»¶ÎµÄÇл»¡£ºËÐÄ´úÂëΪcall sys_call_table(,%eax,4)£¬ËüÊÇÒ»¸öº¯Êýµ÷Ó㬺¯ÊýµÄµØÖ·Îªsys_call_table(,%eax,4)
= sys_call_table + 4*%eax˵Ã÷sys_call_tableΪһ¸öÊý×éÈë¿Ú£¬Êý×éÖеÄÔªËØ³¤¶È¶¼Îª4¸ö×Ö½Ú£¬ÎÒÃÇÒª·ÃÎÊÊý×éÖеĵÚ%eax¸öÔªËØ¡£¶ø%eax¼´ÎªÏµÍ³µ÷Óúš£sys_call_table¾ÍÊÇËùÓÐϵͳµ÷Óõĺ¯ÊýÖ¸ÕëÊý×é¡£
// ¶¨ÒåÔÚ linux/include/linux/sys.h
fn_ptr sys_call_table[] = { sys_setup, sys_exit,
sys_fork, sys_read,
sys_write, sys_open, sys_close, sys_waitpid, sys_creat,
sys_link,
sys_unlink, sys_execve, sys_chdir, sys_time, sys_mknod,
sys_chmod,
sys_chown, sys_break, sys_stat, sys_lseek, sys_getpid,
sys_mount,
sys_umount, sys_setuid, sys_getuid, sys_stime,
sys_ptrace, sys_alarm,
sys_fstat, sys_pause, sys_utime, sys_stty, sys_gtty,
sys_access,
sys_nice, sys_ftime, sys_sync, sys_kill, sys_rename,
sys_mkdir,
sys_rmdir, sys_dup, sys_pipe, sys_times, sys_prof,
sys_brk, sys_setgid,
sys_getgid, sys_signal, sys_geteuid, sys_getegid,
sys_acct, sys_phys,
sys_lock, sys_ioctl, sys_fcntl, sys_mpx, sys_setpgid,
sys_ulimit,
sys_uname, sys_umask, sys_chroot, sys_ustat, sys_dup2,
sys_getppid,
sys_getpgrp, sys_setsid, sys_sigaction, sys_sgetmask,
sys_ssetmask,
sys_setreuid,sys_setregid }; |
µ½ÕâÀÎÒÃÇÕÒµ½ÁË×îÖÕÕæÕýµÄÖ´ÐкËÐĺ¯ÊýµØÖ·sys_write£¬Õâ¸öÊDzÙ×÷ʵÏÖµÄÄں˴úÂ룬ËùÓÐµÄÆÁÄ»´òÓ¡¾ÍÊÇÓɸú¯Êý×îÖÕʵÏÖ¡£ËüÀïÃæÉæ¼°IOµÄһЩӲ¼þÇý¶¯º¯Êý£¬ÎÒÃÇÔÚÕâÀï¾Í²»ÔÙ¼ÌÐøÉîÈëÁË¡£
µ½´Ë£¬ÎÒÃÇÒѾͨ¹ýprintfÕâÑùÒ»¸öÉϲãµÄº¯Êý½Ó¿Ú£¬Çå³þ²Ù×÷ϵͳÊÇÈçºÎÒ»²½²½ÎªÁËÎÒÃÇÌṩÁËÒ»¸öÄں˵÷Óõķ½·¨¡£Èç´ËµÄ¾«Ï¸¿ØÖÆ£¬ÈÃÈ˸Ð̾¡£
4. ÎÒÃÇÈçºÎΪ²Ù×÷ϵͳÌí¼ÓÒ»¸öϵͳµ÷ÓÃ
ÏÂÃæ¼òµ¥ËµÃ÷һϣ¬ÈçºÎÔÚ²Ù×÷ϵͳԴÂëÖÐÌí¼ÓÁ½¸öÎÒÃÇ×Ô¼ºµÄϵͳµ÷ÓÃwhoamiºÍiam
iamϵͳµ÷ÓðÑÎÒÃÇÖ¸¶¨µÄÒ»¸ö×Ö·û´®±£´æÔÚÄÚºËÖС£
whoami°ÑÄÚºËÖеÄͨ¹ýiamÉèÖõÄÄǸö×Ö·û´®¶ÁÈ¡³öÀ´¡£
ÏÂÃæÊǾßÌåµÄ²Ù×÷²½Öè¡£
ÔÚlinux/kernelÎļþ¼Ð¼ÓÈëÒ»¸ö×Ô¶¨ÒåµÄÎļþwho.c
ÔÚwho.cÖÐʵÏÖsys_iamºÍsys_whoami£¬ÐèҪעÒâµÄʵÏÖÕâÁ½¸öº¯Êýʱ£¬ÐèÒªÓÃÓÚÓû§Õ»Êý¾ÝÓëÄÚºËÕ»Êý¾Ý¿½±´¡£
ÔÚlinux/include/linux/sys.hÖеÄsys_call_tableÖÐÌí¼ÓÁ½¸öÊý×éÏî¡£
ÐÞ¸Älinux/kernel/system_call.sÖеÄϵͳµ÷ÓøöÊýnr_system_calls¡£
ÓÃint 0x80ʵÏÖiamºÍwhoamiº¯Êý¡£
±àдÓû§³ÌÐòµ÷ÓÃÉÏÃæÁ½¸öº¯Êý¡£
ҪעÒâµÄÊÇ£ºÔÚϵͳµ÷ÓõĹý³ÌÖУ¬¶Î¼Ä´æÆ÷dsºÍesÖ¸ÏòÄÚºËÊý¾Ý¿Õ¼ä£¬¶øfs±»ÉèÖÃÖ¸ÏòÓû§Êý¾Ý¿Õ¼ä¡£Òò´ËÔÚʵ¼ÊÊý¾Ý¿éÐÅÏ¢´«µÝ¹ý³ÌÖÐLinuxÄں˾ͿÉÒÔÀûÓÃfs¼Ä´æÆ÷À´Ö´ÐÐÄÚºËÊý¾Ý¿Õ¼äÓëÓû§Êý¾Ý¿Õ¼äÖ®¼äµÄÊý¾Ý¸´Öƹ¤×÷£¬²¢ÇÒÔÚ¸´Öƹý³ÌÖÐÄں˳ÌÐò²»ÐèÒª¶ÔÊý¾Ý±ß½ç·¶Î§×÷Èκμì²é²Ù×÷¡£±ß½ç¼ì²é²Ù×÷ÓÉCPU×Ô¶¯Íê³É¡£Äں˳ÌÐòµÄʵ¼ÊÊý¾Ý´«Ë͹¤×÷¿ÉÒÔʹÓÃget_fs_byte()ºÍputs_fs_bypte()µÈº¯Êý½øÐС£
|