May 10 2010

做一个操作系统的准备工作

Category: OSDevwuxicn @ 1:10 PM
« Previous TOP » Next

======

工欲善其事,必先利其器!

想要做一个操作系统,好用的工具必不可少,下面介绍所需要工具。
Continue reading “做一个操作系统的准备工作”

Tags:


May 05 2010

Install GDB cross debugger for i386 ELF on Mac OS X

Category: Uncategorizedwuxicn @ 9:31 AM

1. Download and install gettext: http://www.gnu.org/software/gettext/

2. Install i386-elf-gcc from MacPorts:

sudo port install i386-elf-gcc
sudo port -f activate i386-elf-gcc

You may need to create some symbolic links:

cd /opt/local/bin
sudo ln -s i386-elf-gcc-4.3.2 i386-elf-gcc
sudo ln -s i386-elf-g++-4.3.2 i386-elf-g++

3. Download GDB: http://www.gnu.org/software/gdb/download/

4. Configure and install GDB for i386-ELF:

./configure --prefix=/opt/local \
            --program-prefix=i386-elf- \
            --target=i386-elf \
            --with-gmp=/opt/local \
            --with-libelf=/opt/local \
            --with-build-libsubdir=/opt/local
make
sudo make install

Then the i386-ELF GDB will be installed in /opt/local with program name i386-elf-gdb.

Note: option ‘–target=i386-elf’ specifies guest(target) type ‘i386 ELF’.

Tags: , , , , ,


Apr 25 2010

[zz]Guide: Function Calling Conventions

Category: Uncategorizedwuxicn @ 9:55 PM

Guide: Function Calling Conventions

GCC follows certain rules in generating and calling its functions. If you are writing portable C or C++ code, you never need to know about these rules. However, if you are writing assembly language or nonportable code that depends on these rules, you need to know what they are. This document attemps to describe them, and gives some examples.

Notes

This document assumes a familiarity with assembly language. The assembler code used here is written in the AT&T syntax, as used by GNU as. If you’re using an Intel-syntax assembler, like nasm, you’ll have to translate appropriately.

What’s described here are GCC’s standard calling conventions. Many can be changed by using options like -mregparm, but that’s outside the scope of this document.

These conventions apply to C. C++ introduces several additional complications (such as class pointers and name mangling), some of which can change between compiler versions. Thus, I suggest that asm functions called from C++ code be declared as extern "C". This will cause C calling conventions to be used.

Writing Assembly-Language Functions

Naming

In DJGPP, a function’s assembly-language name is the same as its C name, with an underscore (“_“) prepended. Thus, the C function foo would be named _foo in assembly language. (This is in fact true for all symbol names, such as variables.) C++ has some much more complicated rules.

Registers

GCC requires that some registers not change across a function call. If you want to use these registers in an assembly function, you must save and restore their values. They are:

  • %ebx
  • %esi
  • %edi
  • %ebp
  • The segment registers %ds%es and %ss

Other registers are available for your use (though some have other special uses; read on).

  • Integers (of any size up to 32 bits) and pointers are returned in the %eax register.
  • Floating point values are returned in the 387 top-of-stack register, st(0).
  • Return values of type long long int are returned in %edx:%eax (the most significant word in %edx and the least significant in %eax).
  • Returning a structure is complicated and rarely useful; try to avoid it. (Note that this is different from returning a pointer to a structure.)

If your function returns void (e.g. no value), the contents of these registers are not used.

          Last argument
	  ...
4(%esp)	  First argument
(%esp)    Return address

  • Integers up to 32 bits and pointers are pushed as a single longword.
  • long long int is pushed as two longwords; the least significant is pushed last (and so is located first in memory).
  • float and double are pushed as a double-precision value, occupying 8 bytes.
  • long double is pushed as an extended-precision value followed by 2 bytes of padding, totalling 12 bytes.
  • As before, structures are more complicated and best avoided.

These rules also apply to functions which take a variable number of arguments (like printf). As with any variadic function, the function must find its own way of determining how many arguments were actually passed (usually based on one of the required args).

The stack below the return address is available for temporary storage, but be sure to decrement %esp appropriately. Memory below %esp may be overwritten asynchronously, by interrupt handlers and such. Restore its value when exiting, so that the return works correctly. You may also push and pop at will.

You may modify your arguments in place if you wish; they will not be reused by the caller. Do not, however, attempt to pop them; the caller handles this.

Calling C Functions From Assembly Language

An assembly language function may wish to call a function written in C, either your own or one from the standard library. The same rules already explained apply; you just see them from the other side.

First, you push the function’s arguments (if any) onto the stack, last argument first. See above for the formats used. (Floating point values are usually most easily handled by making space on the stack and then executing a store instruction; i.e. subl $8,%esp; fstpl (%esp).)

Use a simple call instruction to call the function.

You are responsible for removing the arguments you have pushed. They may have changed, so you may not reuse them. You need not, however, discard them at once; it may be more convenient when calling several functions to leave the arguments on the stack and pop them all together at the end. addl n,%esp is an efficient way to do this. It may also be convenient in this case to use %ebp as a frame pointer, since it need not change all the time. (The C compiler does this.)

The return value may be found as detailed above.

Expect the registers %eax%ecx, and %edx, as well as the floating-point stack, to have changed. Standard library functions may modify the %gs register, and the_far* functions may modify %fs. Other registers will be preserved.

Conclusion

These are the basic calling conventions used by GCC; however, there are special cases, optional modifications, etc. that can apply in situations not covered here. In this case, gcc -S is your best friend – from assembly output, you can usually figure out the rules. Also helpful is the GCC source: see i386.h and i386.md inconfig/i386. They are well commented.

Examples

(略,见原文)

原文:http://www.delorie.com/djgpp/doc/ug/asm/calling.html

Tags: , ,


Apr 21 2010

使用SSH隧道翻墙!

Category: Uncategorizedwuxicn @ 4:38 PM
使用SSH隧道翻墙你必须有一个国外可以通过SSH登录的空间。

做法很简单(感谢Sprayfly):
1. 将下面内容存成文件tunnel.sh:

#!/bin/bash
# --------------------------------------
#
#     Title: SSH HTTP Proxy Script
#    Author: Jonathan Lumb
#     Email: jonolumb (at) gmail (dot) com
#  Homepage: http://sprayfly.com
#      File: tunnel.sh
#   Created: July 05, 2009
#
#   Purpose: Establishes/closes a secure HTTP proxy
#
# --------------------------------------
########### Setup SSH Proxy #############
# You will need to have your server setup to run with public and private keys
export SSH_HOST=username@host
############ End of Setup #############

if [ ! -f /tmp/.tunnel ]
then
    echo "创建 SSH 隧道"
    ssh -f -D 9999 $SSH_HOST "if [ -f ~/.tunnel ]; then rm ~/.tunnel; fi; while [ ! -f ~/.tunnel ]; do echo > /dev/null; done" &
    touch /tmp/.tunnel
else
    echo "关闭 SSH 隧道"
    ssh $SSH_HOST "touch ~/.tunnel"
    rm /tmp/.tunnel
fi

exit

然后给这个文件增加执行权限:chmod +x tunnel.sh

2. 建立SSH的RSA授权文件:ssh-keygen -t rsa注意:密码留空。
这样在本地的 ~/.ssh/ 下就会产生两个文件 id_rsaid_rsa.pub,将这两个文件的访问权限设置为0700:chmod 0700 id_rsa id_rsa.pub
然后将密钥授权文件id_rsa.pub文件传到远程的~/.ssh/ 下,命名为:authorized_keys,操作:
scp id_rsa.pub username@host.com:~/.ssh/authorized_keys

3. 做完上面两步后就可以开启你的SSH tunnel了:
开启/关闭SSH隧道:./tunnel.sh
然后在浏览器中设置socks代理(http代理必须留空):127.0.0.1端口:9999

补充说明一点:要关闭ssh隧道还可以在远程的 $HOME 目录下建立一个 .tunnel 文件,这样本地ssh隧道就会自动关闭了,建立方法:touch .tunnel

Tags: , , ,


Apr 15 2010

What does CALL instruction do in assembly?

Category: Uncategorizedwuxicn @ 8:52 AM

CALL instruction do nothing except push IP into the stack (doesn’t like function call in C).
More specific, there’re 2 kinds of CALL:

1. Near call: if it’s a near call, the assembly does those two things:
1) push EIP into the stack,
2) set EIP to the address of beginning of your function.

2. Far call: if it’s a far call, the assembly does the following FOUR things:
1) push CS into the stack (save old code segment),
2) push EIP into the stack,
3) set CS to the new code segment which your function addressed,
4) set EIP to the beginning address of your function.

BTW, the RET instruction does the reverse operations.

Tags: ,


Apr 13 2010

ANSI Escape Sequences (ANSI Escape codes)

Category: Uncategorizedwuxicn @ 1:03 PM

ANSI Escape Sequences 定义了控制终端(Terminal)中的颜色显示、光标位置、键盘重映射的功能。直接向终端发送特定的ASCII字符串就能对特定控制功能进行修改。

所有控制序列都是以 ESC键 的ASCII码字符 和 [ 字符开始的,即: ESC [
ESC键 的 ASCII 码是 0x1b (oct 033),并且 [ 字符的ASCII是 0x5b (oct 0133),所以所有控制序列开头2字节都是: 0x1b 0x5b
我们可以用 echo -e 命令来进行测试(注意,必须带 -e 参数,否则echo不会将字符进行转义),ESC 字符可以写成 \x1b 或者 \033,
如: echo -e "\x1b[1;31mHello world\x1b[0m" 将会输出 红色粗体的:Hello world

控制序列的格式是这样的:

ESC [ ... Char

其中:
ESC是:ESC键 的 ASCII 字符:\x1b 或者 \033.
... Char是:以一个字符结尾的控制串,结尾字符区分大小写,可以是 H、f、m等,具体字符所表示的控制意义与格式见表1. [1]

Further reading: [3] [4]

 

References:

[1] ASCII Table: ANSI Escape sequences. http://ascii-table.com/ansi-escape-sequences.php
[2] The 256 color mode of xterm. http://www.frexx.de/xterm-256-notes/
[3] Wikipedia: ANSI escape cod. http://en.wikipedia.org/wiki/ANSI_escape_code
[4] Xterm Control Sequences. http://www.xfree86.org/current/ctlseqs.html
Continue reading “ANSI Escape Sequences (ANSI Escape codes)”

Tags: ,


Apr 12 2010

用defaults命令修改Mac的一些默认设置

Category: Uncategorizedwuxicn @ 10:55 PM

1: defaults write com.apple.dashboard devmode YES
This allows you to drag widgets out of Dashboard onto the desktop. Requires the dock to be relaunched to take effect, so type “killall Dock” and press enter. Now, if you click and hold onto a widget in the dashboard and press F12 to return to the desktop, the widget won’t disappear with the rest. Put NO at the end to reverse.

2: defaults write com.apple.frameworks.diskimages skip-verify TRUE
Skip disk image verification. Potentially risky, use with disk images from trusted sources. Replace TRUE with FALSE to reverse.

3: defaults write com.apple.CrashReporter DialogType none
Disables the unexpectedly quit dialog that normally appears when an application crashes. Replace “none” with “prompt” to enable again.

4: defaults write com.apple.finder AppleShowAllFiles TRUE
Shows hidden files in the finder. Replace TRUE with FALSE to hide hidden files again.

5: defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
Changes the title of a window to its full path. Repeat with NO to reverse.

to find out more, see: http://www.macosxtips.co.uk/

Tags:


Apr 10 2010

Win32 内存中的栈与函数调用约定(

Category: Uncategorizedwuxicn @ 12:56 AM

注意:本文是针对Win32的,对linux下的gcc不适用!

1. 栈 Stack

在内存中,栈(Stack)是从高地址往低地址增长的[1](从上到下布局的),例如:

1052h |------| < -- sp
1051h |------|                /\
1050h |------| push ||    pop ||
104Fh |------|      \/
104Eh |------|

即当前sp在1052h位置,push了1 Byte以后,sp = sp - 1 即:1051h。

并且,数据存放时按照我们正常的逻辑存放,高地址存放数据的高字节、低地址存放数据的低字节,例如:
当前sp=1052h, push word 0x1234 时,先将高字节0x12存入sp-1的位置(1051h),再将0x34存入sp-2位置(1050h),然后sp = sp - 2 (sp变为1050h) [1].

2. 函数调用约定 Calling Convention

Continue reading "Win32 内存中的栈与函数调用约定("

Tags: , , , ,


Apr 02 2010

x86寄存器

Category: Uncategorizedwuxicn @ 4:38 PM

INTEL 80×86 CPU有下面这些寄存器:

通用寄存器 (General Purpose Registers)
1. EAX (AX/AL/AH) Accumulator.
2. EBX (BX/BL/BH) Base
3. ECX (CX/CL/CH) Counter
4. EDX (DX/DL/DH) Data

注: Exx 是32位寄存器,在80386+的CPU才有。
Continue reading “x86寄存器”

Tags: ,


Mar 31 2010

Make your own boot-up img with “dd”

Category: Uncategorizedwuxicn @ 10:29 PM

Assume that you have a boot file ‘boot.bin’, then you can use it to create your own boot-up img(disk) with “dd” command under linux(or BSD).

the only thing you need to do is to open a terminal and type:

dd if=boot.bin of=boot.img bs=512 count=1

done!

or if you want a boot-up floppy disk img:

dd if=boot.bin of=boot.img bs=512 count=1
dd if=/dev/zero of=boot.img seek=1 bs=512 count=2879

then you have a 1.44M disk img.

Tags:


« Previous PageNext Page »