2012年9月11日 星期二

Add virtual address mapping to current process's page table

Object:
Using hardware MMU to speedup the translation of gva to hpa for QEMU system mode.

Note: pgd , pud, pmd are the table entry, not the table

Reference:

Sample kernel code:

//#include <asm/tlb.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/sched.h>

#include <asm/pgalloc.h>
//#include <asm/pgtable_types.h>  //for struct page
#include <asm/tlbflush.h>

//vaddr_h: vaddr above 4G
long sys_cr3(unsigned long vaddr_h)
{
    unsigned long eax;
    unsigned long vaddr=0x10000000 | (vaddr_h & 0xfff);
    pgd_t *pgd;
    pud_t *pud;
    pmd_t *pmd, pmd_n;
    pte_t *pte, *pte_h;
    struct page *page;
    pgprot_t pgprot;// = __pgprot(0x67);
    //pgprot.pgprot=0x67;
    //set_pte_vaddr(vaddr, ptep);
    //pte_alloc();
    
    //get origen page above 4G
    pgd = pgd_offset(current->mm, vaddr_h);
    pud = pud_offset(pgd, vaddr_h);
    pmd = pmd_offset(pud, vaddr_h);
    pgprot = __pgprot(pmd_val(*pmd) & 0xfff);
    pte_h = pte_offset_map(pmd, vaddr_h);
    
    //mapping vaddr_h above 4G to vaddr below 4G
    //alloc page entry
    pgd = pgd_offset(current->mm, vaddr);
    if(pgd_none(*pgd)) {
        printk("pgd entry not found, alloc new pud and set pgd entry\n");
        /* FIXME */
        //pud = pud_alloc(current->mm, current->mm->pgd, vaddr);
        //set_pud(pgd, *pud);
        pgd = pgd_alloc(current->mm);
    }
    pud = pud_offset(pgd, vaddr);
    if(pud_none(*pud)) {
        printk("pud entry not found, alloc new pmd and set pud entry\n");
        /* FIXME */
        pud = pud_alloc(current->mm, pgd, vaddr);
    }
    pmd = pmd_offset(pud, vaddr);
    if(pmd_none(*pmd)) {
        printk("pmd entry not found, alloc new pte and set pmd entry\n");
        //pmd = pmd_alloc(current->mm, pud, vaddr);
        page = pte_alloc_one(current->mm, vaddr);
        pmd_n = mk_pmd(page, pgprot);
        set_pmd(pmd, pmd_n);
    }
    /*
    pte = pte_offset_map(pmd, vaddr);
    if(!pte) {
        printk("pte_alloc\n");
        //page table entry is not availble, alloc one page table
        //Note: pte is page table pointer, not page table entry
        ptep = pte_alloc_map(current->mm, current->mm->mmap, pmd, vaddr);
        if(!ptep)
            printk("pte_alloc_map fail\n");
    }
    */
    
    //pte = page table entry
    pte = pte_offset_map(pmd, vaddr);
    
    //printk("vaddr_h: pmd_index:%lu, pte_index:%lu\n", 
    //        pmd_index(vaddr_h), pte_index(vaddr_h));
    
    //set new pte as origin pte_h
    set_pte(pte, *pte_h);
    
    //printk("__pa(%lx)=%lx, __pa(12345678)=%lx\n", 
    //        vaddr_h, __pa(vaddr_h), __pa(0x12345678));
    //update_mmu_cache(current->mm->mmap, vaddr, pte);
    //flush_tlb_mm(current->mm);



    __asm__ __volatile__ (
#ifdef CONFIG_X86_64
        "mov %%cr3, %%rax"
#else
        "mov %%cr3, %%eax"
#endif
        : "=a" (eax)
        : /* no input */
        :
        );

    return eax;
    //flush_tlb_page(current->mm->mmap, 0x12345);
}

2012年8月17日 星期五

Adding a new system call in Linux kernel 3.3

How to add a new Linux kernel API in 3.3 version? -- for 64 bits OS
  • Upzip it with command ‘tar xvfj XXX” to a folder For example : /root/kernel tar xvfj linux-3.3.1.tar.bz2 
  • Edit file “/root/kernel/linux-3.3.1/arch/x86/syscalls/syscall_64.tbl” Add new line
    312 64 husky1 sys_husky1 
  • Eidt file “/root/kernel/linux-3.3.1/include/linux/syscalls.h” Add new function declaration
    asmlinkage long sys_husky1(int fd);
    before the line “#endif” 
  • Add a new c file under “/root/kernel/linux-3.3.1/arch/x86/kernel”
    long sys_husky1(int fd) { ... }
  • Edit “/root/kernel/linux-3.3.1/arch/x86/kernel/Makefile” Add a new line “obj-y += husky.o” 
  • goto /root/kernel/linux-3.3.1 folder and run command “make –j8” 
User space code for calling the new system call:
int ret = syscall(312);
Due to standard kernel header doesn't know our new system call,
we need to use system call number here.

Ref: http://stackoverflow.com/questions/9977968/adding-a-new-system-call-in-linux-kernel-3-3

2012年7月4日 星期三

讓Apache得知透過nginx reverse-server的訪客真實IP

由於大家都只對revere proxy連線,實際存取apache的就只有reverse proxy而已,若是不透過reverse proxy,apache的log可以清楚的紀錄每一個連線的ip,但是透過了reverse proxy之後,所有瀏覽紀錄會變成只剩下一個,所有的瀏覽紀錄都來自127.0.0.1,這樣的情況是不被允許的,雖然說reverse proxy也可以留下紀錄,但是遇到有問題要比對時就相當麻煩了,所以還是希望apache可以記錄到原始的ip。


安裝: emerge mod_rpaf
設定:
RPAFenable On
RPAFproxy_ips 127.0.0.1 2.2.2.2(reverse proxy ip)
RPAFsethostname On
#RPAFheader X-Forwarded-For (This is default)


Reference:
Show IP in Apache logs from nginx reverse proxy
Passing IPs to apache with nginx proxy

2012年4月27日 星期五

Creating symbolic link on NTFS

Windows 底下有一個 Installer 的資料夾,裡面主要是放 安裝程式的 MSI 檔案
如果任意移除會倒置很多應用程式無法正常運作
但是這個資料夾又非常佔用空間,勢必得想個方法把他搬到 D: 或其他地方

在 Linux 系統中有 ln 指令可以做 link 的動作
慶幸 Windows 中也有類似的工具 Junction,只要是 NTFS 的系統就有支援
下載點: http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx

安裝方式:
把解壓縮後的 junction.exe 複製到 \Windows\system32

使用方式:
junction dir1 dir2
這樣 dir1 就會成回 dir2 的 symbolic link 了

所以要把 Installer 移到其他硬碟的步驟為:
  1. 搬移 C:\Windows\Installer 到 D:\Installer
  2. junction C:\Windows\Installer D:\Installer
這樣當 Windows 存取 C:\Windows\Installer 的時候,就會自動轉向 D:\Installer 了

2012年4月20日 星期五

Windows 無法安裝列表機

今天幫朋友安裝列表機一直無法成功
安裝最後給的錯誤訊息是 "無可用的連接埠"
到 "列表機與傳真" 那邊嘗試新增列表機的時候
發現上面完全沒有可用的連接埠可以選
正常情況應該如下圖會列出很多選項
新增列表機的畫面(正常)

經過一翻 google, 最後在微軟[1]的網頁上找到類似問題的解法
根據微軟的說法, 是管理列表機連接埠的驅動可能被其他列表機取代
而導致與系統不相容
只要將驅動改回原本的 Windows 提供的即可
  1. 啟動 「 登錄編輯程式 」 (Regedt32.exe)。
  2. 找出下列機碼:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Local Port
  3. 編輯 "Driver", 把字串值改回 "localspl.dll"
  4. 按一下 [確定],然後再結束「登錄編輯程式」
  5. 重新啟動「多工緩衝處理器」(Print Spool)服務。
不過我遇到的問題卻是這個登錄表的值 存取被拒絕!!
這時需要把 Local Port 下面的所有子資料夾權限改回來才可
正常的權限應該如下
權限設定
但我這邊卻是一片空白,完全無法存取
修復方法是修改這些子資料夾的擁有者
改回 Administrators 就可以存取了
取回擁有權

參考資料:
[1] 沒有本地埠可供印表機: http://support.microsoft.com/kb/255507

2012年3月29日 星期四

解決 Java SecureRandom 過久的問題

【問題原因】
Java 的 SecureRandom 為了取得足夠亂、無法預測的 random data
會依據當時系統的 中斷、網路傳輸以及處理器負載 等資訊來產生
當系統處於閒置狀態時,便無法取得足夠多變異的資料來產生 random data
導致需要更多時間去完成這個動作

【解決方法】
啟動 Java 時,增加以下參數
-Djava.security.egd=file:/dev/./urandom
P.S 雖然他產生的亂度相對較低
但...除非是需要【極高/無法預測】的 key
不然還是愈快愈好 XDD

【可能會遇到此問題的軟體】
  1. Hadoop system: HDFS namenode
  2. Web server: Jetty
【參考文獻】
How to solve performance problem with Java SecureRandom?
Jetty: Connectors are slow to startup

2011年10月16日 星期日

Git through proxy authentication

How to git clone from https://github.com/FreeRDP/FreeRDP.git through proxy authentication.

Add git proxy configuration:
# git config --global http.proxy http://user:pass@proxy.server:port/

Troubleshooting:
add GIT_CURL_VERBOSE=1 environment variable for git
ex: GIT_CURL_VERBOSE=1 git clone https://xxx
Related Posts Plugin for WordPress, Blogger...