聚會時間公告: 因應COSCUP 2011, Kalug 8月份休會一次

六月 26, 2011

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» How to write Platform Devices and Drivers with FPGA via GPMC

View more presentations from Wu Bo-Yi

這投影片是我在接手公司其中一個專案,所做的 Slide,當然最主要是深入了解 GPMC (General Purpose Memory Control),GPMC 本來是ARM 用來跟 Memory 溝通的 interface,現在用來跟 FPGA 溝通,目前我只有看到 TI 的線上文件有看到相關說明,以及解釋 GPMC 的 Program Model,在寫 GPMC 之前請先注意 Platform Device 跟 Platform Driver 的關係,之後才會開始設定 GPMC Config(1~7) 的設定檔,這樣拿示波器就可以看到 GPMC Chip Select 訊號,每個 ARM 只能接 8 個 Chip Select,這點大家必須注意,Flash 會用掉一個,在這專案學到蠻多東西,畢竟 Driver 這塊非常大,之前寫 G-Sensor 的 i2c Driver 也是如此。此 Slide 只是初步介紹,更詳細的就要實際撰寫程式碼了。

Random View

三月 14, 2011

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [Linux Kernel] 讀取 /proc 底下資料最佳方法: seq_file interface

前言

最近在整合公司內部 Atheros(被高通買下) 晶片的 Router,從原本 2.6.15 升級到 2.6.34.7,升級過程遇到很多困難,其中一項升級 Wireless Driver 部份,發現在 Kernel Socket 與 User Space 溝通之間出了問題,利用 Ioctl 來取得目前在 AP 上面所有 Client 資料(包含 mac address, 處於 N or G mode…等),在 User Space 上會掉資料,後來利用 /proc 底下檔案來跟 User 之間溝通,才沒有發生任何問題,由於輸出的檔案比較多,就偏向用 2.6 Kernel 提供的 seq_file 介面( interface )建立虛擬檔案 (virtual file) 與 User Space 溝通(此方法為 Alexander Viro 所設計),此功能其實在 2.4.15 已經實做了,只是在 2.6 版本才被大量使用。

程式設計師可以透過引入 seq_file.h> 來實做 seq_file interface,seq_file 最大優勢就是讀取完全沒有4k boundry 的限制,也就是不用管會不會超出 output buffer。

The iterator interface

為了能夠讓 iterator 正常運作,我們必須實做 4 個 function (start, next, stop, show),跑得過程為 start -> show -> next -> show -> next -> stop,為了方便講解,參考 Linux Kernel(4)- seq_file 裡面範例如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h> /* Necessary because we use proc fs */
#include <linux/seq_file.h> /* for seq_file */
#include <linux/uaccess.h>

MODULE_LICENSE("GPL");

#define MAX_LINE 1000
static uint32_t *lines;

/**
 * seq_start() takes a position as an argument and returns an iterator which
 * will start reading at that position.
 */

static void* seq_start(struct seq_file *s, loff_t *pos)
{
    uint32_t *lines;

    if (*pos >= MAX_LINE) {
        return NULL; // no more data to read
    }

    lines = kzalloc(sizeof(uint32_t), GFP_KERNEL);
    if (!lines) {
        return NULL;
    }

    *lines = *pos + 1;

    return lines;
}

/**
 * move the iterator forward to the next position in the sequence
 */

static void* seq_next(struct seq_file *s, void *v, loff_t *pos)
{
    uint32_t *lines = v;
    *pos = ++(*lines);
    if (*pos >= MAX_LINE) {
        return NULL; // no more data to read
    }
    return lines;
}

/**
 * stop() is called when iteration is complete (clean up)
 */

static void seq_stop(struct seq_file *s, void *v)
{
    kfree(v);
}

/**
 * success return 0, otherwise return error code
 */

static int seq_show(struct seq_file *s, void *v)
{
    seq_printf(s, "Line #%d: This is Brook's demo\n", *((uint32_t*)v));
    return 0;
}

static struct seq_operations seq_ops = {
    .start = seq_start,
    .next  = seq_next,
    .stop  = seq_stop,
    .show  = seq_show
};

static int proc_open(struct inode *inode, struct file *file)
{
    return seq_open(file, &seq_ops);
}

static struct file_operations proc_ops = {
    .owner   = THIS_MODULE, // system
    .open    = proc_open,
    .read    = seq_read,    // system
    .llseek  = seq_lseek,   // system
    .release = seq_release  // system
};

static int __init init_modules(void)
{
    struct proc_dir_entry *ent;

    ent = create_proc_entry("brook", 0, NULL);
    if (ent) {
        ent->proc_fops = &proc_ops;
    }
    return 0;
}

static void __exit exit_modules(void)
{
    if (lines) {
        kfree(lines);
    }
    remove_proc_entry("brook", NULL);
}

module_init(init_modules);
module_exit(exit_modules);


裡面可以發現實做了 seq_start, seq_next, seq_stop, seq_show,首先,我們要實作 start() 函式,其主要功能就是回傳 iterator 要讀取的單一位置(position),position 從 0 開始,而我們打算從第一行開始讀取
void * (*start) (struct seq_file *m, loff_t *pos);

接下來實做 next() 功能,其主要是將 position 移動到下一個位置,所以移動到下一個,我們必須將 iterator 加一
void * (*next) (struct seq_file *m, void *v, loff_t *pos);

最後要實作 stop(),當 iterator 執行完畢,我們必須將 start() 所用到的記憶體釋放
void (*stop) (struct seq_file *m, void *v);

Formatted Output

顯示的 callback function 為 show,seq_file 提供了幾個 function 讓大家使用

int seq_printf(struct seq_file *m, const char *f, ...): 類似 printf 或 printk
int seq_puts(struct seq_file *m, const char *s): 印字串
int seq_putc(struct seq_file *m, char c): 印字元

最後說明一下 module 啟動流程,首先定義您要的 /proc/file,給接下來在 module 初始化的時候宣告

static int __init init_modules(void)
{
    struct proc_dir_entry *ent;

    ent = create_proc_entry("brook", 0, NULL);
    if (ent) {
        ent->proc_fops = &proc_ops;
    }
    return 0;
}

我們可以看到當建立 proc file 時綁定特定 proc_ops operation

static struct file_operations proc_ops = {
    .owner   = THIS_MODULE, // system
    .open    = proc_open,
    .read    = seq_read,    // system
    .llseek  = seq_lseek,   // system
    .release = seq_release  // system
};

接著打開檔案時呼叫 proc_open:

static int proc_open(struct inode *inode, struct file *file)
{
    return seq_open(file, &seq_ops);
}

proc_open 則是回傳 seq_file 四個 seq_ops operation

static struct seq_operations seq_ops = {
    .start = seq_start,
    .next  = seq_next,
    .stop  = seq_stop,
    .show  = seq_show
};

就是上面所列的四個最主要的 function。

Related View

十二月 27, 2010

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [Linux] 釋放虛擬記憶體 (cache)

Linux Kernel 2.6.16 之後加入了 drop caches 的機制,可以讓系統清出多餘的記憶體,這對於搞嵌入式系統相當重要阿,Memory 不夠就不能 upgrade firmware,我們只要利用讀寫 proc 檔案就可以清除 cache 記憶體檔案,底下是操作步驟: 1. 釋放 pagecache:捨棄一般沒使用的 cache echo 1 > /proc/sys/vm/drop_caches 2. 釋放 dentries and inodes: echo 2 > /proc/sys/vm/drop_caches 3. 釋放 pagecache, dentries and inodes:(效果等同於1+2) echo 3 > /proc/sys/vm/drop_caches Reference: Drop Caches 觀察 Linux 的虛擬記憶體 Related View切換 Ubuntu apt 的 mirror site (0)[Linux] [...]

七月 30, 2010

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [網站] 好站連結 (七) Android, javascript, Css, PHP, Perl, FreeBSD, Linux

Windows C# C# 比較字串 MSDN 比較字串 Request.Form Collection Request Query String / Form Parametrs ...詳全文(共1347字)

七月 27, 2010

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [Kernel Driver] 撰寫簡易 Timer 機制

在底層 Linux Kernel 提供了時序(timing)機制,方便驅動程式設計者所使用,核心是依據硬體發出的『計時器中斷』來追蹤時間的流動狀況。我們可以依據 HZ 的值來設計 Delay 機制,讓驅動程式可以每隔固定一段時間啟動或者是發出訊號,也可以利用 Timer 來讓 LED 閃爍變化,在介紹 Timer API 之前,可以先參考 Linux Kernel: 簡介HZ, tick and jiffies 這篇文章,瞭解一些相關名詞,舉例:如果想知道一秒後的 jiffies 時間,可以寫成底下: ...詳全文(共1659字)

七月 25, 2010

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [Linux Kernel] 簡單 hello world: License and Module 介紹(part 3)

在 Kernel 2.4 或以上版本,在編譯模組完成,要進行 load module 之前,你會發現底下訊息: # insmod hello-3.o Warning: loading hello-3.o will taint the kernel: no license ...詳全文(共2142字)

七月 20, 2010

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [Linux Kernel] 撰寫 Hello, World module: The __init and __exit Macros (part 2).

再看此篇之前,可以先閱讀作者先前寫的:『[Linux Kernel Driver] 撰寫簡單 Hello, World module (part 1).』,今天要介紹 Driver 的 init module 區別,在 Kernel 2.4 版本,您可以自行定義 init 跟 cleanup 函式,他們不再被個別稱為 init_module()cleanup_module(),現在都使用 module_init()module_exit() 兩大巨集,這兩函式被定義在 linux/init.h 檔案裡面,所以在寫程式務必將其 include 喔,另外一個核心模組(MODULE_LICENSE),用於讓核心知道此模組遵守自由授權條款,若沒這項宣告,核心會跟您抱怨的喔,底下為範例:

#include <linux/kernel.h> /* pr_info所需 include 檔案*/
#include <linux/init.h>
#include <linux/module.h> /* 所有 module 需要檔案*/
#include <linux/version.h>

MODULE_DESCRIPTION("Hello World !!");
MODULE_AUTHOR("Bo-Yi Wu <appleboy.tw AT gmail.com>");
MODULE_LICENSE("GPL");

static int __init hello_init(void)
{
    pr_info("Hello, world appleboy\n");
    pr_info("The process is \"%s\" (pid %i)\n", current->comm, current->pid);
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_INFO "Goodbye\n");
}

module_init(hello_init);
module_exit(hello_exit);

編譯過程,可以自行修改 Makefile,可以打開 kernel/android-2.6.29/drivers/i2c/chips/Makefile 參考範例,您會發現很多類似底下寫法:

obj-$(CONFIG_TWL4030_POWEROFF)  += twl4030-poweroff.o
obj-$(CONFIG_TWL4030_MADC)  += twl4030-madc.o
obj-$(CONFIG_RTC_X1205_I2C) += x1205.o
obj-$(CONFIG_SENSORS_BOSCH_BMA150)  += bma150.o

如果要編譯成 module 可以設定成 obj-m += bma150.o,編譯到 Kernel image,則會寫成 obj-y += bma150.o,然而 $(CONFIG_SENSORS_BOSCH_BMA150) 是從 make menuconfig 設定,當然為什麼 menuconfig 會出現此設定,那就要從 kernel/android-2.6.29/drivers/i2c/chips/Kconfig 裡面加入 CONFIG_SENSORS_BOSCH_BMA150 設定, 選好之後,觀看 Kernel 資料夾底下的 .config 內容,看到 CONFIG_SENSORS_BOSCH_BMA150=y,這樣就正確了。

What is different of __init and __exit?

在寫 G-Senser Driver 時候,您會發現 static int __init BMA150_init(void) 跟 static void __exit BMA150_exit(void),跟平常寫 C 語言宣告函式不一樣吧,這兩個巨集分別有不同意義喔,當然也可以將 span style=”color:red”>__init 跟 __exit 拿掉,這不會影響 Driver 的編譯,但是會影響記憶體的優化,Driver 啟動時會呼叫 BMA150_init 函式,如果有加上 __init,當此 init function 執行完,會將記憶體 Release 給系統,這是針對 built-in 的方式才適用,如果是編譯成模組方式,則不會有此功能,然而 __exit 是 Driver 結束後會呼叫的 function,但是跟 __init 剛好功能相反,在 built-in 的 Kernel 映像檔並不會執行到 __exit,編譯成模組才會有釋放記憶體效果,這兩巨集可以參考 linux/init.h 檔案。

Related View

六月 21, 2010

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [Linux Kernel] 撰寫簡單 Hello, World module (part 1).

來筆記如何在 Kernel 撰寫 hello world! module,在 Ubuntu Kernel 2.6.31-14 環境下撰寫,其實不難啦,首先先進入 Kernel 目錄,請在 /usr/src 底下看自己的系統版本,或者是利用 uname -r 來知道 Kernel 版本,底下是在 Ubuntu Kernel 2.6.31-14 Kernel 實做:

進入 Kernel 目錄

#
# cd Kernel directory
#
cd /usr/src/linux-headers-2.6.31-14-generic-pae

建立 hello 目錄

#
# mkdir directory
#
mkdir hello

建立 Makfile 以及 hello.c

hello.c:

#include <linux/kernel.h> /* pr_info 所需 include 檔案*/
#include <linux/init.h>
#include <linux/module.h> /* 所有 module 需要檔案*/
#include <linux/version.h>

MODULE_DESCRIPTION("Hello World !!");
MODULE_AUTHOR("Bo-Yi Wu <appleboy.tw AT gmail.com>");
MODULE_LICENSE("GPL");

static int __init hello_init(void)
{
    pr_info("Hello, world\n");
    pr_info("The process is \"%s\" (pid %i)\n", current->comm, current->pid);
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_INFO "Goodbye\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile:

#
# Makefile by appleboy <appleboy.tw AT gmail.com>
#
obj-m       += hello.o
KVERSION := $(shell uname -r)

all:
    $(MAKE) -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

clean:
    $(MAKE) -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

之後只要切換到 hello 目錄,直接打 make 就可以產生出 hello.ko 檔案,直接載入 hello.ko 方式:

insmod ./hello.ko

移除 hello.ko

rmmod ./hello.ko

之後到 /var/log/message 底下就可以看到訊息:
Kernel Hello World

Related View

五月 28, 2010

小惡魔AppleBOY
AppleBOY
小惡魔 - 電腦技術 - 工作筆記 - AppleBOY is about »

tag cloud

» [Linux Kernel] built-in vs. module

在編譯 Android Linux Kernel 2.6.29 Driver,常常遇到該把 Driver 用 built-in 或者是編譯成 module 呢?這其實看人習慣,就跟問你編輯器是用 Vim 或者是 emacs 是同樣意思,這兩者是有很大的差異,built-in 用在開機自動讀取載入,所以直接編譯成 uImage 檔案給嵌入式系統,像是 SCSI 或者是 SATA Driver 都建議編譯成 built-in 的方式,反而是一些音效驅動程式,可以編譯成 module,NTFS 就是可以編譯成 module,等您需要的時候在動態載入就可以,這樣可以減少 Kernel Image 的使用空間。

如果不想用 built-in 編譯,開機又需要驅動程式,那就需要透過 initrd 方式來啟動。底下整理兩者差異:

built-in:

開機自動載入,不可移除
Linux Kernel Image 大
需要重新 Compile

module:

可動態載入
Linux Kernel Image 小
不需要重新 Compile

reference:
[gentoo-user] kernel: built-in vs. module

Related View

五月 14, 2008
» [tips] 如何讓你的ext2/ext3在神出鬼沒的地雷戰場上存活.

喜歡用自由軟體的人其實應該都滿常遇到地雷,
通常也練就了一身人間即時掃雷機的本事,
但有些時候實在是地雷太小顆 (但是倒炸的很大力),
又發生在想都想不到的地方, 要讓人不嗚呼哀哉也難.
就像開車時你不超車會有別人超車,
你不想用新版會有別人用新版,
軟體相容性的問題往往是會不請自來的.

lloyd大大今天跟我說了一個最近踩到地雷的故事,
他拿了一顆用ext2格式化過的400g硬碟,
拿到他弟弟灌了ext2 driver的windows上執行,
之前好一陣子都能讀取寫入, 操作上都沒問題,
最近卻怎麼格式化都不能用.
(在windows上會問你要不要重新格式化)
換了小一點的硬碟也不行. 最後他深入追查才發現es2fprogs這個最近更新的套件更新了mkfs.ext2這個程式, 預設的inode改變成256 bytes. 所以要用
mkfs.ext2 -I 128 讓預設的inode設成原本的128 bytes.

ok問題解決了, 聽起來只是windows ext2 driver跟e2fsprogs相容性的問題對不對?
但仔細一想問題可能就很大了, 今天你在debian lenny格式化了一顆ext2硬碟, 要放到穩定的重要server上(恰巧是debian sarge),卻不能讀了.
今天如果你沒有"恰巧"讀到這段,


E2fsprogs 1.40.5 (January 27, 2008)

Fix a potential overflow big in e2image if the device name is too long.

Mke2fs will now create new filesystems with 256 byte inodes and the ext_attr feature flag by default.
This allows for much better future compatibity with ext4 and speeds up extended attributes even on ext3 filesystems.

並把他放在心上的話, 你很可能就炸掉了.
(不過事實上可能就算你讀到這段也還是會被炸掉...)

此外/boot通常有人會用ext2而非格式化成xfs或raiser3什麼的(甚至連ext3都不用, 因為穩定),也免不了會踩到這個雷,
這裡"恰巧"就有個血淋淋的例子. (GRUB vs. the Inodes: Who Needs a Bootable System, Anyway? ) 喔, 只是不能開機而已嘛...orz

備註:
e2fsprogs version:
Gentoo-stable: 1.40.8
lenny (next debian stable): 1.40.8
etch (debian stable): 1.39+1.40

重要指令:

mkfs.ext2 -I 128 /dev/???
mkfs.ext3 -I 128 /dev/???

如果你還要向前相容性的話, 從現在開始別忘了mkfs.ext3時加上-I 128 , 否則... 就歡樂的炸吧... XD

感謝lloyd大大更正: 在debian etch (kernel 2.6.18) 上應該還是可以讀取256 bytes inode的格式, sarge是2.4 kernel可能就不行了. (根據mkfs.ext2的man page說法是2.4 kernel會沒辦法mount)

update: fix link.

七月 22, 2007
» Xen Merged into Linux 2.6.23

http://kerneltrap.org/node/13917, 來自kernel trap的最新消息.

Xen是一個開放原始碼虛擬機監視器,
簡單的說, Xen可以讓一台機器同時跑好幾個作業系統,
而且模擬的速度接近原本cpu速度.
(目前就我所知執行速度是比User mode linux/KVM/kqemu都快的多,
跟qemu/virtualbox/vmware當然更不是同一個檔次的)

前陣子才剛在一台debian etch(dom0)上安裝netbsd跟freebsd的domU,
(DomU 其實就是Xen對guest OS的術語. 而Dom0就是host os.
netbsd是*bsd裡對xen支援最完善的,
目前3.1 release版本就直接支援, 既可以當host OS也可以當guest OS.
如果有人想安裝FreeBSD作guest OS的話, 我建議可以直接參考FreeBSD handbook,21.2.2.1 Setup Xen 3 on Linux dom0,有個不怎麼新的7-current(N個月前的)可以玩,有VT支援的cpu的話應該就比較簡單一點,
也因為我身邊沒半台可以支援VT的cpu(啥怪cpu都有就是沒intel core2duo XD),不然應該可以裝個windows當guest os)
但是說真的, 目前安裝xen還是有點麻煩,
不過我想xen merge進i386的linux之後,
xen應該會越來越得到關愛的眼神才對.
以後搞不好哪天apt-get install xen-netbsd-domU-image就裝好了. (希望啦 XD)

(ps: 差點忘了提最大的重點/亮點:
除去kernel部份 xen大部分的程式碼都是用python寫的!! XDXDXD)

  • Page 1 of 1 ( 11 posts )
  • kernel
A Feedjack powered Planet
A Django site.