<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

  • 随笔 - 21
  • 文章 - 0
  • 评论 - 2
  • 引用 - 0

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

2010年10月25日

Linux 中 x86 的内联汇编

如果您是 Linux 内核的开发人员,您会发现自己经常要对与体系结构高度相关的功能进行编码或优化代码路径。您很可能是通过将汇编语言指令插入到 C 语句的中间(又称为内联汇编的一种方法)来执行这些任务的。让我们看一下 Linux 中内联汇编的特定用法。(我们将讨论限制在 IA32 汇编。)

GNU 汇编程序简述

让我们首先看一下 Linux 中使用的基本汇编程序语法。GCC(用于 Linux 的 GNU C 编译器)使用 AT&T 汇编语法。下面列出了这种语法的一些基本规则。(该列表肯定不完整;只包括了与内联汇编相关的那些规则。)

寄存器命名
寄存器名称有 % 前缀。即,如果必须使用 eax,它应该用作 %eax。

源操作数和目的操作数的顺序
在所有指令中,先是源操作数,然后才是目的操作数。这与将源操作数放在目的操作数之后的 Intel 语法不同。

mov %eax, %ebx, transfers the contents of eax to ebx.

操作数大小
根据操作数是字节 (byte)、字 (word) 还是长型 (long),指令的后缀可以是 b、w 或 l。这并不是强制性的;GCC 会尝试通过读取操作数来提供相应的后缀。但手工指定后缀可以改善代码的可读性,并可以消除编译器猜测不正确的可能性。

movb %al, %bl -- Byte move
movw %ax, %bx -- Word move
movl %eax, %ebx -- Longword move

立即操作数
通过使用 $ 指定直接操作数。

movl $0xffff, %eax -- will move the value of 0xffff into eax register.

间接内存引用
任何对内存的间接引用都是通过使用 ( ) 来完成的。

movb (%esi), %al -- will transfer the byte in the memory 
pointed by esi into al
register


内联汇编

GCC 为内联汇编提供特殊结构,它具有以下格式:

GCG 的 "asm" 结构

   asm ( assembler template

: output operands (optional)

: input operands (optional)

: list of clobbered registers
(optional)

);

本例中,汇编程序模板由汇编指令组成。输入操作数是充当指令输入操作数使用的 C 表达式。输出操作数是将对其执行汇编指令输出的 C 表达式。

内联汇编的重要性体现在它能够灵活操作,而且可以使其输出通过 C 变量显示出来。因为它具有这种能力,所以 "asm" 可以用作汇编指令和包含它的 C 程序之间的接口。

一个非常基本但很重要的区别在于 简单内联汇编只包括指令,而 扩展内联汇编包括操作数。要说明这一点,考虑以下示例:

内联汇编的基本要素

{
int a=10, b;
asm ("movl %1, %%eax;

movl %%eax, %0;"
:"=r"(b) /* output */
:"r"(a) /* input */
:"%eax"); /* clobbered register */
}

在上例中,我们使用汇编指令使 "b" 的值等于 "a"。请注意以下几点:

  • "b" 是输出操作数,由 %0 引用,"a" 是输入操作数,由 %1 引用。
  • "r" 是操作数的约束,它指定将变量 "a" 和 "b" 存储在寄存器中。请注意,输出操作数约束应该带有一个约束修饰符 "=",指定它是输出操作数。
  • 要在 "asm" 内使用寄存器 %eax,%eax 的前面应该再加一个 %,换句话说就是 %%eax,因为 "asm" 使用 %0、%1 等来标识变量。任何带有一个 % 的数都看作是输入/输出操作数,而不认为是寄存器。
  • 第三个冒号后的修饰寄存器 %eax 告诉将在 "asm" 中修改 GCC %eax 的值,这样 GCC 就不使用该寄存器存储任何其它的值。
  • movl %1, %%eax 将 "a" 的值移到 %eax 中, movl %%eax, %0 将 %eax 的内容移到 "b" 中。
  • 因为 "b" 被指定成输出操作数,因此当 "asm" 的执行完成后,它将反映出更新的值。换句话说,对 "asm" 内 "b" 所做的更改将在 "asm" 外反映出来。

现在让我们更详细的了解每一项的含义。


汇编程序模板

汇编程序模板是一组插入到 C 程序中的汇编指令(可以是单个指令,也可以是一组指令)。每条指令都应该由双引号括起,或者整组指令应该由双引号括起。每条指令还应该用一个定界符结尾。有效的定界符为新行 (\n) 和分号 (;)。 '\n' 后可以跟一个 tab(\t) 作为格式化符号,增加 GCC 在汇编文件中生成的指令的可读性。 指令通过数 %0、%1 等来引用 C 表达式(指定为操作数)。

如果希望确保编译器不会在 "asm" 内部优化指令,可以在 "asm" 后使用关键字 "volatile"。如果程序必须与 ANSI C 兼容,则应该使用 __asm__ 和 __volatile__,而不是 asm 和 volatile。


操作数

C 表达式用作 "asm" 内的汇编指令操作数。在汇编指令通过对 C 程序的 C 表达式进行操作来执行有意义的作业的情况下,操作数是内联汇编的主要特性。

每个操作数都由操作数约束字符串指定,后面跟用括弧括起的 C 表达式,例如:"constraint" (C expression)。操作数约束的主要功能是确定操作数的寻址方式。

可以在输入和输出部分中同时使用多个操作数。每个操作数由逗号分隔开。

在汇编程序模板内部,操作数由数字引用。如果总共有 n 个操作数(包括输入和输出),那么第一个输出操作数的编号为 0,逐项递增,最后那个输入操作数的编号为 n -1。总操作数的数目限制在 10,如果机器描述中任何指令模式中的最大操作数数目大于 10,则使用后者作为限制。


修饰寄存器列表

如果 "asm" 中的指令指的是硬件寄存器,可以告诉 GCC 我们将自己使用和修改它们。这样,GCC 就不会假设它装入到这些寄存器中的值是有效值。通常不需要将输入和输出寄存器列为 clobbered,因为 GCC 知道 "asm" 使用它们(因为它们被明确指定为约束)。不过,如果指令使用任何其它的寄存器,无论是明确的还是隐含的(寄存器不在输入约束列表中出现,也不在输出约束列表中出现),寄存器都必须被指定为修饰列表。修饰寄存器列在第三个冒号之后,其名称被指定为字符串。

至于关键字,如果指令以某些不可预知且不明确的方式修改了内存,则可能将 "memory" 关键字添加到修饰寄存器列表中。这样就告诉 GCC 不要在不同指令之间将内存值高速缓存在寄存器中。


操作数约束

前面提到过,"asm" 中的每个操作数都应该由操作数约束字符串描述,后面跟用括弧括起的 C 表达式。操作数约束主要是确定指令中操作数的寻址方式。约束也可以指定:

  • 是否允许操作数位于寄存器中,以及它可以包括在哪些种类的寄存器中
  • 操作数是否可以是内存引用,以及在这种情况下使用哪些种类的地址
  • 操作数是否可以是立即数

约束还要求两个操作数匹配。


常用约束

在可用的操作数约束中,只有一小部分是常用的;下面列出了这些约束以及简要描述。有关操作数约束的完整列表,请参考 GCC 和 GAS 手册。

寄存器操作数约束 (r)
使用这种约束指定操作数时,它们存储在通用寄存器中。请看下例:

asm ("movl %%cr3, %0\n" :"=r"(cr3val));

这里,变量 cr3val 保存在寄存器中,%cr3 的值复制到寄存器上,cr3val 的值从该寄存器更新到内存中。指定 "r" 约束时,GCC 可以将变量 cr3val 保存在任何可用的 GPR 中。要指定寄存器,必须通过使用特定的寄存器约束直接指定寄存器名。

a   %eax
b %ebx
c %ecx
d %edx
S %esi
D %edi

内存操作数约束 (m)
当操作数位于内存中时,任何对它们执行的操作都将在内存位置中直接发生,这与寄存器约束正好相反,后者先将值存储在要修改的寄存器中,然后将它写回内存位置中。但寄存器约束通常只在对于指令来说它们是绝对必需的,或者它们可以大大提高进程速度时使用。当需要在 "asm" 内部更新 C 变量,而您又确实不希望使用寄存器来保存其值时,使用内存约束最为有效。例如,idtr 的值存储在内存位置 loc 中:

 ("sidt %0\n" : :"m"(loc));

匹配(数字)约束
在某些情况下,一个变量既要充当输入操作数,也要充当输出操作数。可以通过使用匹配约束在 "asm" 中指定这种情况。

asm ("incl %0" :"=a"(var):"0"(var));

在匹配约束的示例中,寄存器 %eax 既用作输入变量,也用作输出变量。将 var 输入读取到 %eax,增加后将更新的 %eax 再次存储在 var 中。这里的 "0" 指定第 0 个输出变量相同的约束。即,它指定 var 的输出实例只应该存储在 %eax 中。该约束可以用于以下情况:

  • 输入从变量中读取,或者变量被修改后,修改写回到同一变量中
  • 不需要将输入操作数和输出操作数的实例分开

使用匹配约束最重要的意义在于它们可以导致有效地使用可用寄存器。


一般内联汇编用法示例

以下示例通过各种不同的操作数约束说明了用法。有如此多的约束以至于无法将它们一一列出,这里只列出了最经常使用的那些约束类型。

"asm" 和寄存器约束 "r" 让我们先看一下使用寄存器约束 r 的 "asm"。我们的示例显示了 GCC 如何分配寄存器,以及它如何更新输出变量的值。

int main(void)
{
int x = 10, y;

asm ("movl %1, %%eax;

"movl %%eax, %0;"
:"=r"(y) /* y is output operand */
:"r"(x) /* x is input operand */
:"%eax"); /* %eax is clobbered register */
}

在该例中,x 的值复制为 "asm" 中的 y。x 和 y 都通过存储在寄存器中传递给 "asm"。为该例生成的汇编代码如下:

main:
pushl %ebp
movl %esp,%ebp
subl $8,%esp
movl $10,-4(%ebp)
movl -4(%ebp),%edx /* x=10 is stored in %edx */
#APP /* asm starts here */
movl %edx, %eax /* x is moved to %eax */
movl %eax, %edx /* y is allocated in edx and updated */
#NO_APP /* asm ends here */
movl %edx,-8(%ebp) /* value of y in stack is updated with

the value in %edx */

当使用 "r" 约束时,GCC 在这里可以自由分配任何寄存器。在我们的示例中,它选择 %edx 来存储 x。在读取了 %edx 中 x 的值后,它为 y 也分配了相同的寄存器。

因为 y 是在输出操作数部分中指定的,所以 %edx 中更新的值存储在 -8(%ebp),堆栈上 y 的位置中。如果 y 是在输入部分中指定的,那么即使它在 y 的临时寄存器存储值 (%edx) 中被更新,堆栈上 y 的值也不会更新。

因为 %eax 是在修饰列表中指定的,GCC 不在任何其它地方使用它来存储数据。

输入 x 和输出 y 都分配在同一个 %edx 寄存器中,假设输入在输出产生之前被消耗。请注意,如果您有许多指令,就不是这种情况了。要确保输入和输出分配到不同的寄存器中,可以指定 & 约束修饰符。下面是添加了约束修饰符的示例。

int main(void)
{
int x = 10, y;

asm ("movl %1, %%eax;

"movl %%eax, %0;"
:"=&r"(y) /* y is output operand, note the

& constraint modifier. */
:"r"(x) /* x is input operand */
:"%eax"); /* %eax is clobbered register */
}

以下是为该示例生成的汇编代码,从中可以明显地看出 x 和 y 存储在 "asm" 中不同的寄存器中。

main:
pushl %ebp
movl %esp,%ebp
subl $8,%esp
movl $10,-4(%ebp)
movl -4(%ebp),%ecx /* x, the input is in %ecx */
#APP
movl %ecx, %eax
movl %eax, %edx /* y, the output is in %edx */
#NO_APP
movl %edx,-8(%ebp)


特定寄存器约束的使用

现在让我们看一下如何将个别寄存器作为操作数的约束指定。在下面的示例中,cpuid 指令采用 %eax 寄存器中的输入,然后在四个寄存器中给出输出:%eax、%ebx、%ecx、%edx。对 cpuid 的输入(变量 "op")传递到 "asm" 的 eax 寄存器中,因为 cpuid 希望它这样做。在输出中使用 a、b、c 和 d 约束,分别收集四个寄存器中的值。

asm ("cpuid"
: "=a" (_eax),
"=b" (_ebx),
"=c" (_ecx),
"=d" (_edx)
: "a" (op));

在下面可以看到为它生成的汇编代码(假设 _eax、_ebx 等... 变量都存储在堆栈上):

movl -20(%ebp),%eax /* store 'op' in %eax -- input */
#APP
cpuid
#NO_APP
movl %eax,-4(%ebp) /* store %eax in _eax -- output */
movl %ebx,-8(%ebp) /* store other registers in
movl %ecx,-12(%ebp)
respective output variables */
movl %edx,-16(%ebp)

strcpy 函数可以通过以下方式使用 "S" 和 "D" 约束来实现:

asm ("cld\n

rep\n

movsb"

: /* no input */

:"S"(src), "D"(dst), "c"(count));

通过使用 "S" 约束将源指针 src 放入 %esi 中,使用 "D" 约束将目的指针 dst 放入 %edi 中。因为 rep 前缀需要 count 值,所以将它放入 %ecx 中。

在下面可以看到另一个约束,它使用两个寄存器 %eax 和 %edx 将两个 32 位的值合并在一起,然后生成一个64 位的值:

#define rdtscll(val) \
__asm__ __volatile__ ("rdtsc" : "=A" (val))
The generated assembly looks like this (if val has a 64 bit memory space).
#APP
rdtsc
#NO_APP
movl %eax,-8(%ebp) /* As a result of A constraint
movl %edx,-4(%ebp)
%eax and %edx serve as outputs */
Note here that the values in %edx:%eax serve as 64 bit output.


使用匹配约束

在下面将看到系统调用的代码,它有四个参数:

#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
"d" ((long)(arg3)),"S" ((long)(arg4))); \
__syscall_return(type,__res); \
}

在上例中,通过使用 b、c、d 和 S 约束将系统调用的四个自变量放入 %ebx、%ecx、%edx 和 %esi 中。请注意,在输出中使用了 "=a" 约束,这样,位于 %eax 中的系统调用的返回值就被放入变量 __res 中。通过将匹配约束 "0" 用作输入部分中第一个操作数约束,syscall 号 __NR_##name 被放入 %eax 中,并用作对系统调用的输入。这样,这里的 %eax 既可以用作输入寄存器,又可以用作输出寄存器。没有其它寄存器用于这个目的。另请注意,输入(syscall 号)在产生输出(syscall 的返回值)之前被消耗(使用)。


内存操作数约束的使用

请考虑下面的原子递减操作:

__asm__ __volatile__(
"lock; decl %0"
:"=m" (counter)
:"m" (counter));

为它生成的汇编类似于:

#APP
lock
decl -24(%ebp) /* counter is modified on its memory location */
#NO_APP.

您可能考虑在这里为 counter 使用寄存器约束。如果这样做,counter 的值必须先复制到寄存器,递减,然后对其内存更新。但这样您会无法理解锁定和原子性的全部意图,这些明确显示了使用内存约束的必要性。


使用修饰寄存器

请考虑内存拷贝的基本实现。

   asm ("movl $count, %%ecx;

up: lodsl;

stosl;

loop up;"
: /* no output */
:"S"(src), "D"(dst) /* input */
:"%ecx", "%eax" ); /* clobbered list */

当 lodsl 修改 %eax 时,lodsl 和 stosl 指令隐含地使用它。%ecx 寄存器明确装入 count。但 GCC 在我们通知它以前是不知道这些的,我们是通过将 %eax 和 %ecx 包括在修饰寄存器集中来通知 GCC 的。在完成这一步之前,GCC 假设 %eax 和 %ecx 是自由的,它可能决定将它们用作存储其它的数据。请注意,%esi 和 %edi 由 "asm" 使用,它们不在修饰列表中。这是因为已经声明 "asm" 将在输入操作数列表中使用它们。这里最低限度是,如果在 "asm" 内部使用寄存器(无论是明确还是隐含地),既不出现在输入操作数列表中,也不出现在输出操作数列表中,必须将它列为修饰寄存器。


结束语

总的来说,内联汇编非常巨大,它提供的许多特性我们甚至在这里根本没有涉及到。但如果掌握了本文描述的基本材料,您应该可以开始对自己的内联汇编进行编码了。


参考资料

posted @ 2010-10-25 11:28 ChinaPanda 阅读(271) | 评论 (0)编辑 收藏

2010年1月11日

飞鸽协议以及其使用的UDP数据包格式和文件传输逻辑

     摘要: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 ...  阅读全文

posted @ 2010-01-11 15:23 ChinaPanda 阅读(1961) | 评论 (0)编辑 收藏

2010年1月7日

转 部分飞鸽协议


最近看到一些朋友在编写网络程序是遇到一些问题,故把以前做IPMSG时翻译的文档贴过来,希望对网络编程新手有所帮助,在寻找编程项目的同学们也可参照此文档写出自己的IPMSG。

本文只包含其中几个比较重要的命令以及运行机制的中文翻译,更详细的内容请参照文后的IPMSG 协议英文文档

声明:下述协议内容略去了一些在编写程序过程中没有用到协议内容,最初的Ipmsg协议是用日文写的,下面协议内容由本人(cugb_cat)翻译自Mr.Kanazawa的英文文档。本翻译文档可任意传播和使用。

        IP信使传输协议(第9版草案)         1996/02/21
                2003/01/14 修订

                H.Shirouzu
            shirouzu@h.email.ne.jp


关于IP信使:
  IP信使使用TCP/UDP协议提供收发消息及文件(目录)。
特性:
IP信使能够安装在任何一个安装了TCP/IP协议栈的操作系统上,使用在线用户的动态识别机制,可以和在线所有用户进行信息交换。
运行机制介绍:
使用TCP/UDP端口(默认端口为2425),消息的收发使用UDP协议,文件(文件夹)的收发使用TCP协议。
1、   命令字:
1)   基本命令字(32位命令字的低8位)
    IPMSG_NOOPERATION     不进行任何操作
    IPMSG_BR_ENTRY     用户上线
    IPMSG_BR_EXIT         用户退出
    IPMSG_ANSENTRY     通报在线
    IPMSG_SENDMSG         发送消息
    IPMSG_RECVMSG         通报收到消息
    IPMSG_GETFILEDATA     请求通过TCP传输文件
    IPMSG_RELEASEFILES   停止接收文件
    IPMSG_GETDIRFILES     请求传输文件夹
2)   选项位(32位命令字的高24位)
IPMSG_SENDCHECKOPT   传送检查(需要对方返回确认信息)
IPMSG_FILEATTACHOPT   传送文件选项
3)   附件类型命令(文件类型命令字的低8位)
IPMSG_FILE_REGULAR   普通文件
IPMSG_FILE_DIR     目录文件
IPMSG_FILE_RETPARENT   返回上一级目录
2、   数据包格式(使用字符串):
1)   数据包格式(版本1的格式)
版本号(1):包编号:发送者姓名:发送者主机名:命令字:附加信息
2)   举例如下
“1:100:shirouzu:Jupiter:32:Hello”
3、   数据包处理总述:
1)   用户识别
当IPMSG 启动时,命令IPMSG_BR_ENTRY被广播到网络中,向所有在线的用户提示一个新用户的到达(即表示“我来了”);所有在线用户将把该新上线用户添 加到自己的用户列表中,并向该新上线用户发送IPMSG_ANSENTRY命令(即表示“我在线”);该新上线用户接收到IPMSG_ANSENTRY命 令后即将在线用户添加到自己的用户列表中。
2)   收发消息
使用IPMSG_SENDMSG命令发送消息,消息内容添加在附加信息中;在接收消息时,如果对方要求回信确认(IPMSG_SENDCHECKOPT位打开),则需发送IPMSG_RECVMSG命令并将对方发送的数据包的编号放在附加信息中一同发送至发送消息方
3)   附加文件的扩充(添加于第9版)
带有IPMSG_FILEATTACHOPT位的IPMSG_SENDMSG命令可用来传输文件,文件属性及内容添加在附加信息中,文件内容添加在消息内 容后并以’\0’与之分隔开。传输文件时以下信息将被添加到消息内容之后(包括格式):文件序号:文件名:大小(单位:字节):最后修改时间:文件属性 [: 附加属性=val1[,val2…][:附加信息=…]]:\a:文件序号…
(文件大小、最后修改时间和文件属性为十六进制数,如果文件名中包含’:’则使用“::”代替)。
接收端开始接收文件时,请求传输文件命令IPMSG_GETFILEDATA将发送到发送端的TCP端口(和UDP的发送端口相同),并将发送端发送的包 编号:文件序号:偏移量(全为十六进制格式)写到附加信息区一同发送,文件发送端接收到该请求信息并进行校验正确后即开始发送文件(不使用任何格式,亦不 进行加密)。
当接收端接收到目录文件时,将发送附加信息区为发送端发送的包编号:文件序号:偏移量(全为十六进制格式)的 IPMSG_GETDIRFILES命令,以用来请求传输目录文件;发送端则将头信息长度:文件名:文件大小:文件属性:文件内容添加到附加信息区(除了 文件名和文件内容外,其余皆为十六进制),头信息长度是从头信息长度开始到文件内容前的‘:’分割符为止的字符个数。
当文件属性为IPMSG_FILE_DIR时,IPMsg能够自动识别其为目录,下一个文件的数据在该目录之后。
当文件属性为IPMSG_FILE_RETPARENT时,IPMsg识别其动作为返回上一级目录,在这种情况下,文件名为‘.’其属性为当前目录的值。

附IPMSG协议英文版:



Original ipmsg protocol specification is written in Japanese.
This document was translated by Mr.Kanazawa.
This document is not verified yet.

----------------------------------------------------------------------
    IP Messenger communication protocol (Draft-9) 1996/02/21
                                Modified 2003/01/14

                                    H.Shirouzu
                              shirouzu@h.email.ne.jp
----------------------------------------------------------------------

About IP Messenger
    This is a Send/Receive message service using the TCP/UDP Port.

Characteristics
    IP Messenger can be installed in any OS if TCP/IP is used on your machine.
    Dynamic member recognition can be done within your network or specified network.
    You can exchange messages between all IPMsg members.

Function description
    Use TCP/UDP port(default:2425). See the following descriptions
    (Message Send/Receive: UDP, File Send/Receive: TCP)

1. Command

  1) Command functions (Low 8 bits from command number 32 bits)

    IPMSG_NOOPERATION No Operation
    IPMSG_BR_ENTRY Entry to service (Start-up with a Broadcast command)
    IPMSG_BR_EXIT Exit from service (End with a Broadcast command)
    IPMSG_ANSENTRY Notify a new entry
    IPMSG_BR_ABSENCE Change absence mode

    IPMSG_BR_ISGETLIST Search valid sending host members
    IPMSG_OKGETLIST Host list sending notice
    IPMSG_GETLIST Host list sending request
    IPMSG_ANSLIST Host list sending

    IPMSG_SENDMSG Message transmission
    IPMSG_RECVMSG Message receiving check

    IPMSG_READMSG Message open notice
    IPMSG_DELMSG Message discarded notice
    IPMSG_ANSREADMSG Message open confirmation notice(added from version-8 )

    IPMSG_GETFILEDATA File Transfer request by TCP
    IPMSG_RELEASEFILES Discard attachment file
    IPMSG_GETDIRFILES Attachment hierarchical file request

    IPMSG_GETINFO Get IPMSG version info.
    IPMSG_SENDINFO Send IPMSG version info.

    IPMSG_GETABSENCEINFO Get absence sentence
    IPMSG_SENDABSENCEINFO Send absence sentence

    IPMSG_GETPUBKEY RSA Public Key Acquisition
    IPMSG_ANSPUBKEY RSA Public Key Response

  2) Option flag (High 24 bits from command number 32 bits)

    IPMSG_ABSENCEOPT Absence mode(Member recognition command)
    IPMSG_SERVEROPT Server(Reserved)
    IPMSG_DIALUPOPT Send individual member recognition command

    IPMSG_SENDCHECKOPT Transmission check
    IPMSG_SECRETOPT Sealed message
    IPMSG_READCHECKOPT Sealed message check(added from ver8 )
    IPMSG_PASSWORDOPT Lock
    IPMSG_BROADCASTOPT Broadcast message
    IPMSG_MULTICASTOPT Multi-cast(Multiple casts selection)
    IPMSG_NEWMUTIOPT New version multi-cast(reserved)
    IPMSG_AUTORETOPT Automatic response(Ping-pong protection)
    IPMSG_NOLOGOPT No log files
    IPMSG_NOADDLISTOPT Notice to the members outside of BR_ENTRY

    IPMSG_FILEATTACHOPT File attachment
    IPMSG_ENCRYPTOPT Code

    IPMSG_NOPOPUPOPT (No longer valid)
    IPMSG_RETRYOPT Re-send flag(Use when acquiring HOSTLIST)

  3) Extended code flag (hex format combination)

    IPMSG_RSA_512
    IPMSG_RSA_1024
    IPMSG_RSA_2048
    IPMSG_RC2_40
    IPMSG_RC2_128
    IPMSG_RC2_256
    IPMSG_BLOWFISH_128
    IPMSG_BLOWFISH_256
    IPMSG_SIGN_MD5

  4) Extended files for attachment (fileattr low 8 bits)

    IPMSG_FILE_REGULAR
    IPMSG_FILE_DIR
    IPMSG_FILE_RETPARENT
    IPMSG_FILE_SYMLINK
    IPMSG_FILE_CDEV
    IPMSG_FILE_BDEV
    IPMSG_FILE_FIFO
    IPMSG_FILE_RESFORK

  5) Attachment file extended attribute(fileattr high 24 bits)

    IPMSG_FILE_RONLYOPT
    IPMSG_FILE_HIDDENOPT
    IPMSG_FILE_EXHIDDENOPT
    IPMSG_FILE_ARCHIVEOPT
    IPMSG_FILE_SYSTEMOPT

  6) Extended file attribute for attachment file

    IPMSG_FILE_UID
    IPMSG_FILE_USERNAME
    IPMSG_FILE_GID
    IPMSG_FILE_GROUPNAME
    IPMSG_FILE_PERM
    IPMSG_FILE_MAJORNO
    IPMSG_FILE_MINORNO
    IPMSG_FILE_CTIME
    IPMSG_FILE_MTIME
    IPMSG_FILE_ATIME
    IPMSG_FILE_CREATETIME

    IPMSG_FILE_CREATOR
    IPMSG_FILE_FILETYPE
    IPMSG_FILE_FINDERINFO

    IPMSG_FILE_ACL
    IPMSG_FILE_ALIASFNAME
    IPMSG_FILE_UNICODEFNAME


2.Command format(Use all character strings)

  1) Command(Format version-1)

    Ver(1) : PacketNo : SenderName : SenderHost : CommandNo : AdditionalSection

  2) An example for Message Send/Receive by using the current command format

    "1:100:shirouzu:jupiter:32:Hello"


3.Command process overview

  1) Member recognition

    An IPMSG_BR_ENTRY command notifies a new entry to the current
    members at start-up.

    All members add the new member to their list after getting a notification message.
    An IPMSG_ANSENTRY command sends a message back to the new member.

    The new member gets the current member data by a
    IPMSG_ANSENTRY command. All members can communicate as long as an
    IP packet exists.

    An IPMSG_BR_ABSENCE command broadcasts absence mode cancel or
    nickname change to all members. However, an IPMSG_ANSENTRY command
    does not send a message back, which is different from an IPMSG_BR_ENTRY
    command.

    IPMSG_BR_ENTRY, IPMSG_ANSENTRY, and IPMSG_BR_ABSENCE commands
    use an IPMSG_ABSENCEOPT flag for absence mode. Input a nickname to
    additional command.
    Add an IPMSG_DIALUPOPT flag for dial-up users who can't be reached by
    a broadcast command. A member recognition command needs to be
    sent individually to the members with this optional flag.

    (Extended group)IPMSG_BR_ENTRY and IPMSG_BR_ABSENCE commands
    sends a group name by adding the new group name after the current
    command format character strings (Input '
\0' between the current
    command and extended name).

  2) Send/Receive Message
    Send Message uses an IPMSG_SENDMSG command that can input a message
    in the extended area.
    Receive Message sends back an IPMSG_RECVMSG command only
    if an IPMSG_SENDCHECKOPT flag is ON. Input the original packet number
    to the extended area.

    Broadcast Message Send uses an IPMSG_BOADCASTOPT command
    and an IPMSG_SENDMSG flag should be ON.
    Auto-Send packet(absence notice) needs to be added to IPMSG_AUTORETOPT
    for ping-pong protection. If either one or another packet is ON, then
    confirmation/auto-send packet is not sent back.

    Send Message Sealing needs to be an IPMSG_SECRETOPT packet ON.
    In this case, Receive Message sends an IPMSG_READMSG command.
    Input the original packet number to the extended area.

    (Additional IPMSG_NOADDLISTOPT)
    When receiving an IPMSG_SENDMSG packet from a host that is
    not on your Send/Receive list, IPMsg will either confirm a host by
    sending an IPMSG_BR_ENTRY command or add a host name to
    the Send/Receive list.
    However, single-shot Message Send/Receive action needs to be avoided.
    Add an IPMSG_NOADDLISTOPT flag to an IPMSG_SENDMSG command.

    (Additional IPMSG_READCHECKOPT from version-8 )
    When an IPMSG_READMSG command contains an IPMSG_READCHECKOPT flag,
    IPMsg process is the same as IPMSG_SENDMSG with an
    IPMSG_SENDCHECKOPT flag.
    However, Send Message uses an IPMSG_ANSREADMSG command,
    not IPMSG_RECVMSG.

  3) Message Send/Receive 亅encrypted extension (Added in the version-9 )

    Use the combination of Public-key(RSA) and common key(RC2/Blowfish).
    (Encrypted extension area is used in hex format.)

    (Public key acquisition)Send an IPMSG_GETPUBKEY command to Receive
    Message. Receive Message gets an IPMSG_ANSPUBKEY that
    means receiving RSA public key from Send Message.

    IPMSG_GETPUBKEY/IPMSG_ANSPUBKEY both require the value which is
    encryption capability (Exp. IPMSG_RSA_1024) flag uses "OR" at first
    part of extension

    In addition, In IPMSG_ANSPUBKEY, public key written as EE-NNNNNN
    E=Exponent丄N=method)devide by '
:'. and Input the Fdelimiter '-'
    between E and N.

    This sequence can be skipped after the 2nd Send/Receive process by
    memorizing public key and encrypted data.
   
    (Encrypted message)After a sender creates a common key that is
    supported both sender and receiver, a common key can encrypt a message.
    In addition, a receiver'
s public key encrypts the common key.


    (Encrypted message transmission) IPMSG_ENCRYPTOPT is used in
    IPMSG_SENDMSG. At the first part of extension, input the value which
    is 'or' resoult from Convination of public key and common key type .
    Then use common key which encrypt with public key devide by ':'.
    Then input message which is eccrypted by public key devide by ':'.
    If both supports IPMSG_SIGN_XXX, then add ':' and signeture.

    Also, In the method of encode padding, PKCS#1ECB key is used for RSA,
    PKCS#5 CBC common key is used for RC2/blowfish.

    Also, The Packet related to Entry manifestation the capability of
    ecryption support using IPMSG_ENCRYPTOPT

  4) Extension with file attachment(Available from version-9 )

    An IPMSG_SENDMSG command with an IPMSG_FILEATTACHOPT flag for
    File transfer (download permission)notification sends a message
    with attachment.
    Input '\0' after the message and attachment file data.
   

    fileID:filename:size:mtime:fileattr[:extend-attr=val1
    [,val2...][:extend-attr2=...]]:\a:fileID...
    (size, mtime, and fileattr describe hex format.
      If a filename contains ':', please replace with "::".)

    When Receive Message downloads an attachment file, an IPMSG_GETFILEDATA
    command requests a data transmission packet to the TCP port that is the same number
    as the UDP sending port number. Input packetID:fileID: offset to the extended area.
    (Use all hex format.)
    File Transfer side receives the request. After recognizing that it's a correct request,
    then send the specified data (no format)

    When the data receiving side downloads a hierarchical attachment file,
    use an IPMSG_GETDIRFILES command and input a packetID:fileID
    to the extended area and send a data transmission request packet.
    (all hex format)

    Data sending side sends the following hierarchical data format.
    header-size:filename:file-size:fileattr[:extend-attr=val1
    [,val2...][:extend-attr2=...]]:contents-data
    Next headersize: Next filename...
    (All hex format except for filename and contetns-data)

    header-size is from the beginning of header-size to the delimiter '
:'
    that is before contents-data. extend-attr can be omitted and used multiple
    extended attributes. Use '
=' for data input.

    When fileattr is IPMSG_FILE_DIR, IPMsg recognizes that it is automatically
    in the directory, the next file data is after the directory.

    When fileattr is IPMSG_FILE_RETPARENT, IMPsg recognizes that it returns
    to the parent directory. In this case, File name is always "." and the attribute
    value is the current directory data.

    Sending process starts from the attachment directly and returns the
    IPMSG_FILE_RETPARENT command to the attachment directory.

    Add an IPMSG_FILEATTACHOPT flag for an Entry packet to support the
    attachment file.

  5) Other commands

    When acquiring different versions, send an IPMSG_GETINFO command.
    Receiving side sends the version information character string to
    extended area.

    Send an IPMSG_GETABSENCEINFO command for acquiring an absence message.
    Receiving side sends an IPMSG_SENDABSENCEINFO back if the status is absence mode.
    If the status is not absence mode, a character string "Not absence mode" will be sent back.

  6) Confirmation/Retry

    If a confirmation packet for IPMSG_SENDMSG or IPMSG_RECVMSG is not delivered
    within a specified time, then it will be sent again.
    A number of retry actions or interval period is depended on the current condition.


4. Other

  1) Linefeed

    Linefeed characters in Send Message is standardized with UNIX type ('
0x0a').
    Please change if needed.

  2) Delimiter '
:'

    '
:' is used as a delimiter. You can't use this delimiter for user name
    and host name.
    If the use/host names contain a ':', please replace with another sign,
    for an example ';'.
    Although using this delimiter isn

posted @ 2010-01-07 11:15 ChinaPanda 阅读(540) | 评论 (0)编辑 收藏

2009年12月23日

ACE about server

     摘要:   阅读全文

posted @ 2009-12-23 15:59 ChinaPanda 阅读(660) | 评论 (1)编辑 收藏

2009年12月8日

AT&T指令集

     摘要: GAS中每个操作都是有一个字符的后缀,表明操作数的大小。 C声明 GAS后缀 大小(字节) ...  阅读全文

posted @ 2009-12-08 15:48 ChinaPanda 阅读(1788) | 评论 (1)编辑 收藏

2009年10月18日

一致与非一致性代码段

     摘要:   阅读全文

posted @ 2009-10-18 02:08 ChinaPanda 阅读(976) | 评论 (0)编辑 收藏

2009年9月27日

第二章 系统架构浏览2.5

     摘要:   阅读全文

posted @ 2009-09-27 23:13 ChinaPanda 阅读(607) | 评论 (0)编辑 收藏
copy on write

copy on write是引用计数的概念中的一种重要的实现技术。  
   
  对象之间的复制只通过引用计数来表示,内部其实只有一份对象实例。一旦对象发生变化,才创建对象,然后复制原对象。  
  这可以大大提高效率。

posted @ 2009-09-27 19:41 ChinaPanda 阅读(235) | 评论 (0)编辑 收藏

2009年9月23日

CPU结构介绍

64位处理器

这里的64位技术是相对于32位而言的,这个位数指的是CPU GPRs(General-Purpose Registers,通用寄存器)的数据宽度为64位,64位指令集就是运行64位数据的指令,也就是说处理器一次可以运行64bit数据。64bit处 理器并非现在才有的,在高端的RISC(Reduced Instruction Set Computing,精简指令集计算机)很早就有64bit处理器了,比如SUN公司的UltraSparc Ⅲ、IBM公司的POWER5、HP公司的Alpha等。
64bit计算主要有两大优点:可以进行更大范围的整数运算;可以支持更大的内存。不能因为数 字上的变化,而简单的认为64bit处理器的性能是32bit处理器性能的两倍。实际上在32bit应用下,32bit处理器的性能甚至会更强,即使是 64bit处理器,目前情况下也是在32bit应用下性能更强。所以要认清64bit处理器的优势,但不可迷信64bit。
要实现真正意义上的64位计算,光有64位的处理器是不行的,还必须得有64位的操作系统以及 64位的应用软件才行,三者缺一不可,缺少其中任何一种要素都是无法实现64位计算的。目前,在64位处理器方面,Intel和AMD两大处理器厂商都发 布了多个系列多种规格的64位处理器;而在操作系统和应用软件方面,目前的情况不容乐观。因为真正适合于个人使用的64位操作系统现在就只有 Windows XP X64,而Windows XP X64本身也只是一个过渡性质的64位操作系统,在Windows Vista发布以后就将被淘汰,而且Windows XP X64本身也不太完善,易用性不高,一个明显的例子就是各种硬件设备的驱动程序很不完善,而且现在64位的应用软件还基本上没有,确实硬件厂商和软件厂商 也不愿意去为一个过渡性质的操作系统编写驱动程序和应用软件。所以要想实现真正的64位计算,恐怕还得等到Windows Vista普及一段时间之后才行。
目前主流CPU使用的64位技术主要有AMD公司的AMD64位技术、Intel公司的 EM64T技术、和Intel公司的IA-64技术。其中IA-64是Intel独立开发,不兼容现在的传统的32位计算机,仅用于Itanium(安 腾)以及后续产品Itanium 2,一般用户不会涉及到,因此这里仅对AMD64位技术和Intel的EM64T技术做一下简单介绍。
AMD64位技术
AMD64的位技术是在原始32位X86指令集的基础上加入了X86-64扩展64位X86指 令集,使这款芯片在硬件上兼容原来的32位X86软件,并同时支持X86-64的扩展64位计算,使得这款芯片成为真正的64位X86芯片。这是一个真正 的64位的标准,X86-64具有64位的寻址能力。
X86-64新增的几组CPU寄存器将提供更快的执行效率。寄存器是CPU内部用来创建和储存 CPU运算结果和其它运算结果的地方。标准的32-bit x86架构包括8个通用寄存器(GPR),AMD在X86-64中又增加了8组(R8-R9),将寄存器的数目提高到了16组。X86-64寄存器默认位 64-bit。还增加了8组128-bit XMM寄存器(也叫SSE寄存器,XMM8-XMM15),将能给单指令多数据流技术(SIMD)运算提供更多的空间,这些128位的寄存器将提供在矢量 和标量计算模式下进行128位双精度处理,为3D建模、矢量分析和虚拟现实的实现提供了硬件基础。通过提供了更多的寄存器,按照X86-64标准生产的 CPU可以更有效的处理数据,可以在一个时钟周期中传输更多的信息。
EM64T技术
Intel官方是给EM64T这样定义的:EM64T全称Extended Memory 64 Technology,即扩展64bit内存技术。EM64T是Intel IA-32架构的扩展,即IA-32e(Intel Architectur-32 extension)。IA-32处理器通过附加EM64T技术,便可在兼容IA-32软件的情况下,允许软件利用更多的内存地址空间,并且允许软件进行 32 bit线性地址写入。EM64T特别强调的是对32 bit和64 bit的兼容性。Intel为新核心增加了8个64 bit GPRs(R8-R15),并且把原有GRPs全部扩展为64 bit,如前文所述这样可以提高整数运算能力。增加8个128bit SSE寄存器(XMM8-XMM15),是为了增强多媒体性能,包括对SSE、SSE2和SSE3的支持。
Intel为支持EM64T技术的处理器设计了两大模式:传统IA-32模式(legacy IA-32 mode)和IA-32e扩展模式(IA-32e mode)。在支持EM64T技术的处理器内有一个称之为扩展功能激活寄存器(extended feature enable register,IA32_EFER)的部件,其中的Bit10控制着EM64T是否激活。Bit10被称作IA-32e模式有效(IA-32e mode active)或长模式有效(long mode active,LMA)。当LMA=0时,处理器便作为一颗标准的32 bit(IA32)处理器运行在传统IA-32模式;当LMA=1时,EM64T便被激活,处理器会运行在IA-32e扩展模式下。
目前AMD方面支持64位技术的CPU有Athlon 64系列、Athlon FX系列和Opteron系列。Intel方面支持64位技术的CPU有使用Nocona核心的Xeon系列、使用Prescott 2M核心的Pentium 4 6系列和使用Prescott 2M核心的P4 EE系列。


IA-32(Intel Architecture-32)称为英特尔32位结构,表示指令集结构ISA(Instruction Set Architecture)。

IA-32处理器(或称32位80x86处理器)就是指具有该指令集结构的处理器,例如英特尔公司的80386、80486以及Pentium各代处理器。AMD的32位处理器兼容这个结构。

posted @ 2009-09-23 00:10 ChinaPanda 阅读(425) | 评论 (0)编辑 收藏

2009年9月22日

第二章 系统架构浏览2.2-2.4小节

     摘要: v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} Normal 0 7.8 磅 0 2 false false false ...  阅读全文

posted @ 2009-09-22 23:59 ChinaPanda 阅读(758) | 评论 (0)编辑 收藏
仅列出标题  下一页