Wednesday, December 29, 2010

/dev/mem Read Physical Memory

This is the physical memory address, if you want to read/write physical memory, you can open this file using fopen() for open(), and read/write it. For example, you can read/write SMRAM at 0xa0000

However, it cannot read /dev/mem mmap after 0x101000, but you could use mmap to solve this problem. please see article. 

http://my.opera.com/yangguangxiang/blog/2008/02/17/dev-mem

在2.4,可以直接打开/dev/mem,然后读取。

在2.6,直接打开/dev/mem后,只可以读取前0x101000部分的内容(ubuntu)。大约是1MB加4KB。读取后面的内容将出现"open not permitted"错误。

解决的方法是使用mmap()。routine如下:

f = open("/dev/mem", O_RDONLY);

my_mem = mmap (0, 0x1000, PROT_READ, MAP_SHARED, f, 0x34f000);
if (my_mem == MAP_FAILED)
printf("map failed %s\n",strerror(errno));
通过my_mem就可以得到0x101000之后的内存内容了。

How to write kernel model

1. install kernel-devel on CentOS 5.5 (kernel-dev on Ubuntu)
$ yum update // upgrade your kernel to latest kernel version
$ yum install kernel-devel // install kernel development library at /lib/module/
This is the source code header for current Linux kernel. You could check if they match each other.
$ uname -r
$ ls /lib/moudle/

2. write hello.c and Makefile 
hello.c:

#include <linux/module.h>
#include <linux/init.h>

static int hello_init(void)
{
    printk("\nhello, world\n");
    return 0;
}
static void hello_exit(void)
{
    printk("Goodbye, crule world\n");
}
module_init(hello_init);
module_exit(hello_exit);

Makefile:
# Comment/uncomment the following line to disable/enable debugging
#DEBUG = y
# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
  DEBFLAGS = -O -g # "-O" is needed to expand inlines
else
  DEBFLAGS = -O2
endif
CFLAGS += $(DEBFLAGS) -I$(LDDINCDIR)
ifneq ($(KERNELRELEASE),)
# call from kernel build system, change to you filename
obj-m    := hello.o
else
# remember to set right kernel directory
#KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELDIR ?= /lib/modules/2.6.18-194.26.1.el5/build
PWD       := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINCDIR=$(PWD)/../include modules
endif
clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
depend .depend dep:
    $(CC) $(CFLAGS) -M *.c > .depend
ifeq (.depend,$(wildcard .depend))
include .depend
endif

3.  install module, test, remove module
$tail -f /var/log/message // you will see message "hello word!" when install module
$insmod hello.ko // install module
$lsmod | grep hello // you will see module has been installed
$rmmod hello // remove the module

4. Addition info
LDD book: http://lwn.net/Kernel/LDD3/
Example source code: http://examples.oreilly.com/9780596005900/
BIOSren pdf tutorial: http://linux.chinaunix.net/bbs/viewthread.php?tid=1042600&extra=page%3D1%26amp%3Bfilter%3Ddigest

In example source code:  examples/sample/ directory is the sample example.

understanding mmap()

#include <sys/mman.h>

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

The basic idea is to map the content of a file to memory.

void *addr: preferable addr as the start address in memory, usually set as 0, becuase the actually start address is returned by mmap().

size_t length: the number of bytes you want to map

off_t offset:  the start address of file you want to map. or you could say the offset of the current file you are going to map it to memory.

int fd:  the file descriptor you the file.

int prot: memory protection of the memory just mapped.
       The prot argument describes the desired memory protection of  the  map‐
       ping  (and  must  not  conflict with the open mode of the file).  It is
       either PROT_NONE or the bitwise OR of one  or  more  of  the  following
       flags:
       PROT_EXEC  Pages may be executed.
       PROT_READ  Pages may be read.
       PROT_WRITE Pages may be written.
       PROT_NONE  Pages may not be accessed.

int flags: mapped memory is shared with other process or private.

here is the example of mmap usage, but this is not working:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

#define STARTADDR 0x400000
#define ENDADDR 0x620de6
#define SIZE ENDADDR - STARTADDR

int main(int argc, char *argv[])
{
    int fd1 = open("/dev/mem", O_RDONLY);
    if(fd1 == -1)
    {
    fprintf(stderr, "open error\n");
    exit(1);
    } 
    int fd2 = open("staticMem", O_CREAT | O_RDWR);
    if(fd1 == -1)
    {
    fprintf(stderr, "open error\n");
    exit(1);
    }
    void* my_mem = mmap(0, SIZE, PROT_READ, MAP_SHARED, fd1, STARTADDR);
    if(my_mem == MAP_FAILED)
    {
    fprintf(stderr, "map failed\n");
    exit(1);
    } 
    if(write(fd2, my_mem, SIZE)!= SIZE)
    {
    fprintf(stderr, "write error\n");
    exit(1);
    }
    return 1;
}

logical address, linear address(virtual address), physical address

logical address: before segementation unit translate, in projected mode, it is same value as linear or virtual address. because linux does not use segmentation.

linear address or virtual address: Before paging unit

physical address: not necessary map to RAM, to could be mapped to other device(ROM, PCI device)

Friday, December 24, 2010

how to get the physicall address of kernel code

$ cat /boot/System.map-2.6.32-27-generic | grep text
This command will give you virtual address. The first one is the start of kernel code, For example, on my Ubuntu:
$ cat /boot/System.map-2.6.32-27-generic | grep text 
c0100000 T _text
If you change the c to 0, you changed the virtual address to physical address on Linux. 
Why? please see: http://jianggmulab.blogspot.com/2009/09/blog-post.html

$ cat /boot/System.map-2.6.32-27-generic | grep etext
This one will give you the end virtual address of kernel code.

$ cat /proc/iomem
This command will give your general picture of current memory, and all the address are physical address. For example, on my Ubuntu:
  00100000-00592a36 : Kernel code

Monday, December 20, 2010

print out ebp, esp, eax by gcc inline assembly

Here is the code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    unsigned esp, ebp, eax, ebx, ecx, edx;

    asm(
        "movl %%esp, %0;"
        "movl %%ebp, %1;"
        "movl %%eax, %2;"
        "movl %%ebx, %3;"
        "movl %%ecx, %4;"
        "movl %%edx, %5;"
        :"=r"(esp), "=r"(ebp), "=r"(eax), "=r"(ebx), "=r"(ecx), "=r"(edx)
        );
    printf("esp is: %x\n", esp);
    printf("ebp is: %x\n", ebp);   
    printf("eax is: %x\n", eax);
    printf("ebx is: %x\n", ebx);
    printf("ecx is: %x\n", ecx);   
    printf("edx is: %x\n", edx); 

    return 1;
}

GCC inline assembly syntax:
asm(
//assembly code;
: output operand
: input operand
: registers

"=r" I could use any registers for caculation. "=" means write only
"a": I could use %eax for caculation.

Here is the list:
please see Register operand constraint
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html



)

Friday, December 17, 2010

rough training

"... an old maxim from General Suvorov, who beat Napoleon at Borodino in 1812 (beat him by not losing): 'Battle is easy, it's training that's hard.' Or as a Chinese general preached 2,000 years ago: 'The more you sweat in peace, the less you bleed in war.'"

Thursday, December 16, 2010

login as root on Ubuntu

First of all. you cannot login as root after you installed Ubuntu.

But you could do: $sudo su, which will give you root permission. but you are not root user.

you also could login as root by setting the root password
$sudo passwd
you could enter your root password. Then you could login as root.

Remember, there is a difference between
$su
and
$sudo su

$su has more privilege than $sudo su

menu.lst in Grub 2

For Ubunut 10.4 version. there is not /boot/grub/menu.lst file.
Because 10.4 use grub 2.
use /boot/grub/grub.cfg instead

Detailed information about setting windows boot entry:
http://www.linuxquestions.org/questions/linux-newbie-8/there-is-no-menu-lst-in-my-ubuntu-boot-grub-folder-785767/page2.html

Wednesday, December 15, 2010

Disk parition and mount

show all the partitions on hard disk
sudo fdisk -l


Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000db6bb

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       18249   146585061   83  Linux
/dev/sda2           18250       36485   146480670   83  Linux
/dev/sda3           36486       54721   146480670   83  Linux
/dev/sda4           54722       60802    48839073    5  Extended
/dev/sda5           54722       55207     3903763+  82  Linux swap / Solaris
/dev/sda6           55208       60802    44935168   83  Linux

This is an example

For this hard disk. I only installed Debian on sda1. installed Ubuntu on sda6.
sda5 is the swap patition. I setted as logically.

I also partitioned sda2 and sda3. I set ext3 journaling format.
my previous blog has more detailed installation information.
I don't understand why sda4 came out. 

sda1 installed Debian
You could check OS version.

$ cd /mnt
$ sudo mkdir sda1
$ sudo mount /dev/sda1 sda1
$ cd sda1
$ cat /etc/lsb-release or /etc/redhat-release
e

Install multiple OS on single machine

Disk Space: 500G. You want to install Debian 5.0, Ubuntu 10.4, Cent OS 5.5, Fedora 8 on single machine

First: install Debian (it doesn't matter, choose any one of them)
1. create a partition table of current device(current hard disk), type MSDOS
2. start to partition the current device
3. get first partition: size 150G; format ext3 journaling; /dev/sda1. Primary
4. create a swap partition,
5. size usually is same as memory. /dev/sda5 Logically. first four are Primary
6. leave 350G as free space
7. You could choose expert installing settings.
8. remember to install Grub. choose grub 2 or grub legacy


Second install Ubuntu 10.4
1. put Ubuntu CD in CD-ROM
2. Ubuntu live CD should detect the Debian already has been installed on Disk
3. choose advanced partition.
4. partition half of the free space. size: 150; format: ext3 journaling
5. install ubuntu on the partition./dev/sda2
6. Ubuntu will use the same swap partition as debian.
7. Remember to install Grub at /dev/sda.but this grub will not trash previous one. If you boot, this grub will also show previous debian booting option.

Third install Cent OS 5.5
1. same steps. use all the free space.
2. every time you have to mount current partition (you will install OS on it) to /
3. Don't choose to install grub for CentOS. If you installed grub at /dev/sda. Your previous grub will be trashed, then you cannot boot in previous Debian and Ubuntu.

Fourth Install Fedora 8
1. Fedora, Cent OS are red-hat release. The Installation GUI are almost same.
2. Don't Install Grub again. Fedora grub will trash all previous grub. 

Fifth Install Unbutu 10.4 again.
1. Since previous CentOS 5.5 and Fedora 8 grub trashed previous grub.
2. I need to Install Ubuntu again because Ubuntu grub could detect all the Linux on the harddisk.
3. Ubuntu grub is version 1.98. Fedora and CentOS grub are same.
4. Ubunut grub is better than others.
5. red-hat grub will try to find hard disk by itself. For example: if I disable one disk at BIOS. red-hat grub still could possible find that disk, and boot from that one.

Tuesday, December 14, 2010

Moving hard drive from one computer to another

Tested:
move windows 2008 server, no problem
move centOS 5.5. kernel panic
move openSuse. cannot find drive.

 Or more detailed discussion:
https://bbs.archlinux.org/viewtopic.php?id=108472

human and patch

Every individual human being is a patch.
The world is a multi-thread program
Each patch implement some functions of the program.
We can calculate the value of each human being by using diff command

some patch is efficient, critical, has a lot dependency

Grub configuration

In grub configuration, you could have a lot settings for OS.

here is an example of /boot/grub/menu.lst file
#boot=/dev/sda  // comments
default=0  // default loading OS
timeout=5  // timeout 5 seconds before getting into default OS
splashimage=(hd0,0)/grub/splash.xpm.gz 
hiddenmenu
title CentOS (2.6.18-194.17.1.el5)
    root (hd0,0)
    kernel /vmlinuz-2.6.18-194.17.1.el5 ro root=/dev/VolGroup00/LogVol00 rhgb acpi_sleep=s3_bios quiet
    initrd /initrd-2.6.18-194.17.1.el5.img
title CentOS (2.6.18-194.el5)
    root (hd0,0)
    kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
    initrd /initrd-2.6.18-194.el5.img

For example, you could set the serial port setting.
You could add it at the kernel section.
You also can add something like acpi_sleep=s3_bios at the kernel line.

more detailed about serial port: 
http://tldp.org/HOWTO/Remote-Serial-Console-HOWTO/configure-kernel-grub.html

Tuesday, December 7, 2010

subversion + Latex

subversion repository setup:
Howto: http://fengweizhang.blogspot.com/2011/10/how-to-create-svn-repository.html

remember, you need to set up the ACL permission correctly for your svn repository. so other people could checkout, commit

$svn co svn+ssh://username@servername.com/home/username/svnrepos/file

The idea is setup a projectfile in svn repositary. so memeber could check out the files and write the papers. Then he could commit it.


Latex How-To
Ubuntu: install texlive
$sudo apt-get installl texlive. 

you could use whatever editor you want: Vim
Then find two things:
a tutorail for latex grammer: lshort.pdf
a latex template:

Mac: install latex
sudo port install texlive
OR
install MacTex
http://guides.macrumors.com/Installing_LaTeX_on_a_Mac
or directly download from:
http://www.tug.org/mactex/2011/
But this file is very large.

Updated March 30 2014: 
Install Latex on Mac
1. Go to http://guides.macrumors.com/Installing_LaTeX_on_a_Mac
2. Download MacTeX at http://www.tug.org/mactex/
3. Be Patient, and it is a large file
4. Install it. If you have previous version installed, you can uninstall them by delete /usr/local/texlive
See http://tug.org/mactex/uninstalling.html


CentOS: install tetex
yum install tetex tetex-fonts tetex-dvips tetex-latex ghostscript


EDITOR: You could use VIM, texmaker is good. 

Monday, December 6, 2010

surspend to RAM S3

Suspend to RAM command:

$ echo mem > /sys/power/state
or
$ echo mem > /proc/acpi/sleep

$ echo cmd > /sys/power/state

        S1,                 "standby"         很低的延迟就能回到工作状态

        S3,                 "mem",                 suspend-to-RAM, STR

        S4,                 "disk"                 suspend-to-disk, STD,Hibernation

CPU

command to get cpu infomation:
cat /proc/cpuinfo
In Ubuntu, you could install command cpuid, and run:$cpuid

AMD Athlon 64 3500+
CPUID example: 40ff2
Model stepping:DH-F2

Friday, December 3, 2010

video card

output: Video Graphic Array (VGA), Digital Visual Interface(DVI).

Motherboard Interface:
ISA (Industry Standard Architecture)
PCI (Peripheral Component Interconnect)
AGP (Accelerated Graphics Port)
PCIe (PCI Express)

If you add a video card in your board, It will boot.
But if you remove a video card from your board, and you want to go back to your integrated video card. sometimes X server will not work. You need to reconfigure it, and your machine will have GUI


lsmod | grep radeon

radeon is an open source driver.

Wednesday, December 1, 2010

serial port

Get which serial port device is active
$ dmesg | grep tty

Get the bard rate of serial port device sttyS0
$stty -F /dev/sttyS0

Put some data in serial port
$echo hello > /dev/sttyS0

receive data from serial port of other machine (if use USB to receive)
$cat /dev/ttyUSB0

usually, you will see binary data from terminal. You could use minicom to receive data
Install minicom
$sudo apt-get install minicom

Start minicom and save output
$minicom -C output

If you didn't set up the serial port, you need set up first, run
$minicom -s

Setup the device and baud rate. you could run following cmd to see the device
$ls /dev/tty*

I assume you use USB, the you could see /dev/ttyUSB0. set the device to /dev/USB0 in the minicom. and You need to set the bard rate. Usually, it is 9600, some times it would be 115200(like coreboot set the serial port rate)

Finally, you will see data from terminal, you could also save the output.