📄 proting-uclinux-to-mips-e.txt
字号:
Porting ucLinux to MIPS
This document describes how to porting ucLinux to MIPS platform. And also describes how
to porting GNU toolchain and uclibc on that platform. The last version of this document can
always be found at www.xiptech.com.
1.About this document
1.1 What this document tells and doesn't tell
1.2 Copyright notice
1.3 The last version of this document
2. General information of uclinux
2.1 Introduction of uclinux
2.2 How about the GNU toolchain and c lib
3. MIPS
3.1 Introduction of MIPS
3.2 MIPS without MMU
4. Porting ucLinux
4.1 ucLinux kernel structure
4.2 Kernel mode and user mode check
4.3 Memory accessing check
4.4 Fork and Vfork
4.5 binfmt_flat.c
4.6 Reserve instruction and linux
4.7 Memory page fault handle
5. Porting GNU toolchain
5.1 GNU toolchain and uclinux
5.2 Porting ELF2FLT
5.3 Reserve instructions and toolchain
5.4 Soft floating
6. Porting uclibc
6.1 Introduction of uclibc
6.2 Stack align to 8 bytes
6.3 Pthread
6.4 float-point instruction
7. JIT Simulator
7.1 Introduction
7.2 How to use it?
7.3 Where can I get it?
8. Other issue
8.1 Share lib on uclinux
8.2 PIC or NO-PIC ?
9. Get and build packages
_______________________________________________________________________________
1.About this document
1.1 What this document tells and doesn't tell
This document descript how to porting uclinux, uclibc and GNU toolchain to MIPS platform. And
it tells people where to get the patches and tools.
We didn't list every place we modify in the original code, because reader can see all the changes
in our patch file. Also this document doesn't give details about MIPS and uclinux. We assume the
readers have already known about these.
We will use the special version of hardware MIPSR3000 as samples. Hope this document helps.
1.2 Copyright notice
Copyright (c) 2002-2003
Qiu Liming (qiulm@xiptech.com )
Chang Xinlin (zxl@xiptech.com)
Xipos Tech. ( www.xiptech.com )
Personal user can get and read this document freely. Please don't use it in business without the
permit.
1.3 The last version of this document
www.xiptech.com
2. General information of uclinux
2.1 Introduction of uclinux
uclinux is the most popular OS used in no MMU device. Though uclinux works the same with
linux in many ways, they have some differences, such as fork, mmap, stack size, memory
fragment. We should pay attention to these problems when we porting AP from linux to uclinux.
However, uclinux is still one of the best choice in no MMU system.
The newest version of uclinux kernel can be obtained at www.uclinux.org. The official release
supports two platform, arm and m68k. uclinux has been ported to many other platforms. As we
know, MIPS version of uclinux 2.4.1x is done by some companies. But there is no patch is
published. We do the porting job and now release it to the public.
2.2 How about the GNU toolchain and uclibc
uclibc is the standard C lib for uclinux. It supports MIPS already. But to MIPSR3000, there are
some place had to be changed.
GNU toolchain can be used to build linux or uclinux, no matter kernel and AP. But a tool named
elf2flt, which is used to generate the flat format file for uclinux , need to be added to toolchain.
The original version of elf2flt doesn't support MIPS.
3. MIPS
3.1 Introduction of MIPS
We can get all information about MIPS at www.mips.com. Here we only list some features of
MIPS,
TLB can be seen by software
Delay slot
Access unaligned address will cause exception
Div zero will NOT cause exception
3.2 MIPS without MMU
(figure1.jpg)
Figure 1 is the memory mapping between virtual address and physical address of no MMU
version MIPS. Only kuseg can be accessed by user mode program. When the physical RAM is
located at 0x00000000, some thing special will happen. No user mode program can run because
they cannot access any RAM ! Unfortunately many devices arrange their RAM to 0x0000000.
We must solve this problem. Later section you will see how.
4. Porting ucLinux
4.1 ucLinux kernel structure
This section descript the structure of uclinux kernel. Uclinux kernel can be generated as following,
1. Get linux kernel source code
2. Get the corresponding uclinux patch
3. Patch it to linux source code tree, then we get uclinux kernel
There are 5 directories are added into the linux tree
mmnommu
arch/armnommu
arch/m68knommu
include/asm-armnommu
include/asm-m68knommu
For supporting flat file format, fs/binfmt_flat.c is also added. To start our tour of porting uclinux
to MIPS, let's copy include/asm-mips to include/asm-mips, and then copy arch/mips to
arch/mipsnommu. After these steps, change the Makefile to disable arch/mips and enable
arch/mipsnommu.
4.2 Kernel mode and user mode
In uclinux/linux , OS kernel run in kernel mode, AP and lib run in user mode. But as we discuss in
section 3.2, user mode program cannot be executed on some platforms. What can we do to let
uclinux run on them? Our solution is maintain MIPS CPU running at kernel mode, and cheats the
uclinux kernel when it tries to do the switch between user mode and kernel mode.
The first step is change start_thread (include/asm-mipsnommu/processor.h)
#define start_thread(regs, new_pc, new_sp) do { \
/* New thread loses kernel and FPU privileges. */ \
regs->cp0_status = (regs->cp0_status & ~(ST0_CU0|ST0_KSU|ST0_CU1));\
regs->cp0_status = (regs->cp0_status & ~(ST0_CU0|ST0_KSU|ST0_CU1)) | KU_USER;\
regs->cp0_epc = new_pc; \
regs->regs[29] = new_sp; \
current->thread.current_ds = USER_DS; \
} while (0)
This macro will be executed when kernel creates user mode program. It will set KU_USER bit to
force MIPS CPU into user mode. Once we remoce this bit, MIPS CPU won't go into user mode
anymore.
The second step is to cheat uclinux. When exception happens, uclinux check if it is running at user
mode. If the answer is yes, the stack needs to be switched to kernel mode stack. After exception is
handled, uclinux check this again to decide if the stack needs to be switched back. MIPS linux
uses two methods to do the check, bit CU0 of CP0_STATUS and bit KU_USER of CP0_STATUS.
We cannot use KU_USER in our uclinux. Then we use CU0 for all checks. This step will change
the following files,
arch/mipsnommu/kernel/entry.S
arch/mipsnommu/kernel/scall_o32.S
include/arch/mipnommu/stackframe.h
include/arch/mipsnommu/ptrace.h
At the last of this section, we will discuss about scall_o32.S. This file handles system call. When
system call is done, the CPU should return to the place where the call is made. But if the call is
come from user mode, signal and schedule will be handled first. Code in scall_o32.S works fine
when the call is really come from user mode. The problem is there is no check here, if the call is
come from kernel mode, will this code work fine too?
4.3 Memory accessing check
To enhance the security of linux, kernel will check if a pointer is legal before using it. Because it's
hard to distinguish kernel mode address and user mode address in MIPS uclinux., the memory
check should be removed.
At first we need to change __access_ok (include/asm-mipsnommu/uaccess.h), most memory
check is accomplished by it. Then we need to modify some functions written in assembly, such as
__strncpy_from_user_asm. These functions will do the pointer check by itself.
4.4 Fork and vfork
Uclinux doesn't support fork, it only supports vfork. To avoid user mode program call fork, we
should change sys_fork in MIPS uclinux. We just need to change the parameters sys_fork pass to
do_fork, let sys_fork work as vfork.
4.5 binfmt_flat.c
This file handles the flat format for uclinux. It only supports arm and m68k. We will make it
support MIPS as well. Details is discuss in 5.2.
4.6 Reserve instruction and linux
MIPS I has some instructions to handle unaligned address access. (lwl/lwr,swl/swr) But they are
not implemented in every MIPS CPU product. Such CPU will cause a Reserve Instruction
exception when executing them. The normal way to solve this is handle them in do_ri. But this is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -