2009年3月26日 星期四

在linux kernel裡建立debug訊息

#define DEBUG_LINE printk("JeffPM: <%s:%s:%d> enter_now\n", __FILE__, __func__, __LINE__)

2009年3月18日 星期三

malloc的實作方式

关于第一个问题, 为什么malloc()返回地址的问题, 还是上面说的那样, malloc的实现有一个优化,
1. 当你malloc()一块很小的内存是, glibc调用brk(), 只需要在heap中移动一下指针, 即可获得可用虚存, 这样分配得到的地址较小.
2. 当你malloc()一块较大内存时, glibc调用mmap(), 需要在内核中重新分配vma结构等, 他会在靠近栈的地方分配虚存, 这样返回的地址大.
3. 这个较小和较小的临界值是一个通过mallopt()调整的.
4. 当然, 除了上面的规则, malloc()还有自己的算法, 来判断到底采用mmap()还是brk(), 一般和虚存碎片有关.

/* Allocate the new heap block. */

//两种方式,一种是sbrk() 另一种是mmap()。后面做为一个专题介绍

#ifdef MALLOC_USE_SBRK

__malloc_lock_sbrk ();

/* Use sbrk we can, as it's faster than mmap, and guarantees

contiguous allocation. */

block = sbrk (block_size);

if (likely (block != (void *)-1))

{

/* Because sbrk can return results of arbitrary

alignment, align the result to a MALLOC_ALIGNMENT boundary. */

long aligned_block = MALLOC_ROUND_UP ((long)block, MALLOC_ALIGNMENT);

if (block != (void *)aligned_block)

/* Have to adjust. We should only have to actually do this

the first time (after which we will have aligned the brk

correctly). */

{

/* Move the brk to reflect the alignment; our next allocation

should start on exactly the right alignment. */

sbrk (aligned_block - (long)block);

block = (void *)aligned_block;

}

}

__malloc_unlock_sbrk ();

#else /* !MALLOC_USE_SBRK */

/* Otherwise, use mmap. */

block = mmap (0, block_size, PROT_READ | PROT_WRITE,

MAP_SHARED | MAP_ANONYMOUS, 0, 0);

#endif /* MALLOC_USE_SBRK */

2009年3月13日 星期五

SPD EEPROM

http://en.wikipedia.org/wiki/Serial_Presence_Detect
http://www.simmtester.com/page/news/showpubnews.asp?num=101

$ sudo /usr/bin/decode-dimms

# decode-dimms version 5164 (2008-03-26 14:48:21 +0100)

Memory Serial Presence Detect Decoder
By Philip Edelbrock, Christian Zuckschwerdt, Burkart Lingner,
Jean Delvare, Trent Piepho and others


Decoding EEPROM: /sys/bus/i2c/drivers/eeprom/0-0050
Guessing DIMM is in bank 1

---=== SPD EEPROM Information ===---
EEPROM Checksum of bytes 0-62 OK (0xC1)
# of bytes written to SDRAM EEPROM 128
Total number of bytes in EEPROM 256
Fundamental Memory type DDR SDRAM
SPD Revision 0.0

---=== Memory Characteristics ===---
Maximum module speed 400MHz (PC3200)
Size 512 MB
tCL-tRCD-tRP-tRAS 2.5-3-3-8
Supported CAS Latencies 2.5T
Supported CS Latencies 0
Supported WE Latencies 1
Minimum Cycle Time at CAS 2.5 5 ns
Maximum Access Time at CAS 2.5 0.6 ns

---=== Manufacturing Information ===---
Manufacturer Undefined
Part Number M2S9JA8APS9F081KA-
Manufacturing Date 2007-W40
Assembly Serial Number 0x5295048B


Decoding EEPROM: /sys/bus/i2c/drivers/eeprom/0-0051
Guessing DIMM is in bank 2

---=== SPD EEPROM Information ===---
EEPROM Checksum of bytes 0-62 OK (0xC1)
# of bytes written to SDRAM EEPROM 128
Total number of bytes in EEPROM 256
Fundamental Memory type DDR SDRAM
SPD Revision 0.0

---=== Memory Characteristics ===---
Maximum module speed 400MHz (PC3200)
Size 512 MB
tCL-tRCD-tRP-tRAS 2.5-3-3-8
Supported CAS Latencies 2.5T
Supported CS Latencies 0
Supported WE Latencies 1
Minimum Cycle Time at CAS 2.5 5 ns
Maximum Access Time at CAS 2.5 0.6 ns

---=== Manufacturing Information ===---
Manufacturer Undefined
Part Number M2S9JA8APS9F081KA-
Manufacturing Date 2007-W40
Assembly Serial Number 0x2A93048F


Number of SDRAM DIMMs detected and decoded: 2

2009年3月11日 星期三

i2C EEPROM

http://72.14.235.132/search?q=cache:KZXXNi_62JMJ:blog.chinaunix.net/u1/38038/showart_686041.html+normal_i2c&hl=zh-TW&ct=clnk&cd=1&gl=tw&client=firefox-a


http://209.85.175.132/search?q=cache:jv_z4Z0xdmEJ:blog.chinaunix.net/u3/90065/showart_1849895.html+eeprom_attach_adapter&hl=zh-TW&ct=clnk&cd=1&gl=tw&client=firefox-a

http://72.14.235.132/search?q=cache:skAn7sQ2c1oJ:linux.chinaunix.net/bbs/thread-1055398-1-5.html+i2c_board_info+EEPROM&hl=zh-TW&ct=clnk&cd=1&gl=tw&client=firefox-a

http://clc168.blogspot.com/2007/07/linux-i2c-driver.html

2009年3月9日 星期一

使用mmap()存取MMIO registers

#include stdio.h
#include fcntl.h
#include string.h
#include sys/vfs.h
#include unistd.h
#include sys/mman.h
#include stdlib.h
#include errno.h

#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int

#define MSM_MDP_BASE 0xE0010000
#define MSM_MDP_PHYS 0xAA200000

#define MSM_MDP_SIZE 0x000F0000
#define MSM_MDP_MASK (MSM_MDP_SIZE-1)
int main(){


void *vaddr1,*vaddr2;

int errors = 0;
int fd;
fd = open("/dev/mem", O_RDWR | O_SYNC);
printf("/dev/mem opened.\n");
fflush(stdout);

vaddr1 = mmap(0, MSM_MDP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,\
fd,MSM_MDP_PHYS & ~MSM_MDP_MASK);

if(vaddr1 == (void *) -1) {
printf("mapping error!!\n");
return -1;
}


vaddr2 = vaddr1 + (MSM_MDP_PHYS & MSM_MDP_MASK);

printf("vaddr1 %p, vaddr2 %p\n", vaddr1, vaddr2);
fflush(stdout);
printf("vsync period=%08X\n",*(volatile u32 *)(vaddr2+0xE0008));



munmap(vaddr1, MSM_MDP_SIZE);

close(fd);
return errors;

}