Linux Renesas SuperH

<<<linux-sh kernel sequence>>>
                                Apr. 25, 2001
                                Masahiro ABE<mailto:[email protected]>
                                (C) 2001 A&D; Co., Ltd. <http://www.aandd.co.jp>
--------------------------------------------------------------------------------
0. Table of Contents
  1. Preface
  2. Memory Map at Kernel Boot Time
  3. Decompress Kernel Image - startup(arch/sh/boot/compressed/head.S)
  4. Kernel Startup - _stext(arch/sh/kernel/head.S)
  5. Architecture Independent Startup Function - start_kernel(init/main.c)
  6. init Kernel Thread - init(init/main.c)
  7. Architecture Dependent Initialization - setup_arch(arch/sh/kernel/setup.c)
  8. Initialization of Exception Handling - trap_init(arch/sh/kernel/traps.c)
  9. Initialization of Interrupt Handling - init_IRQ(arch/sh/kernel/irq_ipr.c)
  10. Initialization of Timer - time_init(arch/sh/kernel/time.c)
  11. Exception/Interrupt Handling
    11.1 Exception Handling Entry
    11.2 Dispatcher for Exception/Interrupt Handling - handle_exception
    11.3 Actual Exception Handling(Excluding System Call and Interrupt)
    11.4 Actual System Call Handling
    11.5 Actual Interrupt Handling
--------------------------------------------------------------------------------
1. Preface
 This document is the result of analysis of execution sequence of Linux Kernel
running on Hitachi SuperH Processor by reading source code.
 This is written based on source code of Kernel2.4.2 with following patch files:
-linux-sh-2.4.2-20010323.patch
-linux-sh-2.4.2-20010320-cf.patch
 This document must contain many wrong, misunderstood information because of
lack of knowledge of the author. When you find them, please kindly notify me.
This document is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY. Author assumes no responsibility to the contents of this document.
Anyone can display and/or copy the document for any purpose.
 All explanation is based on the case that SolutionEngine(MS7750SE01) of Hitachi
ULSI Systems Co. Ltd., is used and executed from CompactFlash
 I would like to appreciate everyone on the GNU/Linux on SuperH project.
 Throughout the analysis, "Linux Kernel Internals" guide was very valuable. I
recommend you to read it also. I would like to appreciate the author of it, Mr.
Tigran Aivazian, for preparing such a good explanation.
--------------------------------------------------------------------------------
2. Memory Map at Kernel Boot Time
 This chapter shows the memory map of the system when the kernel started its
execution. This applies to the case that both sh-ipl+g and sh-lilo are used.
external mem.   P1 area         media           contents
0x 0000 0000    0x 8000 0000----------------------------------------------------
                                FlashROM	code of start(entry.S)
0x 0000 0100    0x 8000 0100                    --------------------------------
                                                exception handler(general_exception)
0x 0000 0400    0x 8000 0400                    --------------------------------
                                                exception handler(tlb_miss)
0x 0000 0600    0x 8000 0600                    --------------------------------
                                                exception handler(interrupt)
                                                exception handling table
                                                actual handler code
                                                code of main.c
0x 0400 0000    0x 8400 0000----------------------------------------------------
0x 0c00 0000    0x 8c00 0000----------------------------------------------------
                                RAM             global variable of sh-ipl+g
0x 0c00 0a00    0x 8c00 0a00                    --------------------------------
                                                stack area of sh-ipl+g
0x 0c00 0f00    0x 8c00 0f00                    --------------------------------
0x 0c00 1000    0x 8c00 1000                    --------------------------------
                                                Parameters for the kernel
0x 0c00 1100    0x 8c00 1100                    --------------------------------
                                                Command line for the kernel
0x 0c00 2000    0x 8c00 2000                    --------------------------------
0x 0c20 0000    0x 8c20 0000                    --------------------------------
                                                code of lilo 1st stage loader
0x 0c20 0200    0x 8c20 0200                    --------------------------------
                                                lilo stack area
0x 0c20 1000    0x 8c20 1000                    --------------------------------
                                                code of lilo 2nd stage loader
0x 0c20 2000    0x 8c20 2000                    --------------------------------
                                                lilo reserved area
0x 0c20 3000    0x 8c20 3000                    --------------------------------
                                                lilo map information
0x 0c20 3200    0x 8c20 3200                    --------------------------------
                                                lilo 1st descriptor table
0x 0c20 3400    0x 8c20 3400                    --------------------------------
                                                lilo 2nd descriptor table
0x 0c20 3600    0x 8c20 3600                    --------------------------------
                                                lilo Default command line
0x 0c20 3800    0x 8c20 3800                    --------------------------------
                                                lilo Keyboard translation table
0x 0c20 3a00    0x 8c20 3a00                    --------------------------------
                                                lilo Message table
0x 0c20 3c00    0x 8c20 3c00                    --------------------------------
0x 0c21 0000    0x 8c21 0000                    --------------------------------
                                                Kernel will be loaded here
0x 1000 0000    0x 9000 0000----------------------------------------------------
 Kernel image is loaded at 0x8c210000 as shown above, and it is the starting
address of execution.
--------------------------------------------------------------------------------
3. Decompress Kernel Image - startup(arch/sh/boot/compressed/head.S)
 By reading Makefile, zImage file is constructed like this figure. (Please refer
to (Makefile, arch/sh/Makefile, arch/sh/boot/Makefile, arch/sh/boot/compressed/
Makefile.)
+--------------------------------+
| arch/sh/boot/compressed/head.S |
+--------------------------------+
| arch/sh/boot/compressed/misc.c |
+--------------------------------+
| arch/sh/kernel/sh_bios.c       |
+--------------------------------+
| gzip-compressed vmlinux        |
| +----------------------------+ |
| | arch/sh/kernel/head.S      | |
| +----------------------------+ |
| | arch/sh/kernel/init_task.c | |
| +----------------------------+ |
| | init/main.c                | |
| +----------------------------+ |
| | init/version.c             | |
| +----------------------------+ |
| | ......                     | |
| +----------------------------+ |
+--------------------------------+
 Kernel image is gzip-compressed except the first portion of codes. Kernel code
first decompress the compressed image into certain address of memory. This is
done in "startup" of arch/sh/boot/compressed/head.S.
 startup does the following: (_text is set as 0x8c001000 at link time.)
-initialize Status Register
-clean BSS area
-initialize Stack Pointer, uses user_stack in arch/sh/boot/compressed/misc.c
-decompress main code - decompress_kernel(arch/sh/boot/compressed/misc.c)
 decompressed to 0x8c001000 + 0x20001000 = 0xac002000(P2 area)
-jump to the first address of decompressed kernel main code
 address is 0x8c001000 + 0x1000 = 0x8c002000 (P1 area)
 Destination of decompression and destination of jump differs, but both are same
in terms of external memory space. Decompressed kernel is written to P2 area
because it is non-cachable area. (???Is this true???)
--------------------------------------------------------------------------------
4. Kernel Startup - _stext(arch/sh/kernel/head.S)
 _stext is the code first executed in kernel main code. This code does only the
required operation before jumping to start_kernel(init/main.c), and then jumps
to it.
 Here is what _stext does:
-initialize Status Register, mask interrupt
-set 0 to r6_bank (this is for global_imask)
-set up Stack Pointer using the stack area defined in vmlinux.lds.S
-set (address of stack) - 0x2000 to r7_bank (this is for current)
-initialize FPU - fpu_init(arch/sh/kernel/fpu.c)
-enable cache - cache_init(arch/sh/mm/cache.c)
-clear BSS area
-jump to start_kernel
--------------------------------------------------------------------------------
5. Architecture Independent Startup Function - start_kernel(init/main.c)
 start_kernel is executed on every architecture while booting linux kernel. This
does most of initialization and house-keeping tasks to make fundamental
functionalities of kernel work.
 Here I briefly follow the sequence, excluding some those I thought these may
not be so important.
-lock_kernel()                 ---does nothing because this is not SMP system
-display banner message
-setup_arch()                  (arch/sh/kernel/setup.c)
-display passed command line
-parse_options()               (init/main.c)
-trap_init()                   (arch/sh/kernel/traps.c)
-init_IRQ()                    (arch/sh/kernel/irq_ipr.c)
-sched_init()                  (kernel/sched.c)
-time_init()                   (arch/sh/kernel/time.c)
-softirq_init()                (kernel/softirq.c)
-console_init()                (drivers/char/tty_io.c)
-init_modules()                (kernel/module.c)
-kmem_cache_init()             (mm/slab.c)
-sti() -> __sti()              (include/asm-sh/system.h)
-calibrate_delay()             (init/main.c)
-mem_init()                    (init/main.c)
-kmem_cache_sizes_init()       (mm/slab.c)
-proc_root_init()              (fs/proc/root.c)
-fork_init()                   (kernel/fork.c)
-proc_caches_init()            (kernel/fork.c)
-vfs_caches_init()             (fs/dcache.c)
-buffer_init()                 (fs/buffer.c)
-page_cache_init()             (mm/filemap.c)
-kiobuf_setup()                (fs/iobuf.c)
-signals_init()                (kernel/signal.c)
-bdev_init()                   (fs/block_dev.c)
-inode_init()                  (fs/inode.c)
-ipc_init()                    (ipc/util.c)
-check_bugs()                  (include/asm-sh/bugs.h)
-display POSIX conformance test message
-smp_init()                    ---does nothing because this is not SMP system
-kernel_thread()               (arch/sh/kernel/process.c)
-unlock_kernel()               ---does nothing because this is not SMP system
-set need_resched flag of current task (kernel itself)
-cpu_idle()                    (arch/sh/kernel/process.c)
 Functions until smp_init do each initialization. After these operation, kernel
does infinite loop inside cpu_idle. Kernel startup process can be treated as a
process with pid=0.
 I will add some explanation of some of those initialization functions later.
 kernel_thread starts init() as a thread. init is explained in next chapter.
--------------------------------------------------------------------------------
6. init Kernel Thread - init(init/main.c)
 init kernel thread, started by start_kernel(), does initialization of several
device drivers, and starts first user process at last.
-lock_kernel()                 ---does nothing because this is not SMP system
-do_basic_setup()              (init/main.c)
 does initialization of several device driver, and calls special initialization
 functions
-free_initmem()                (arch/sh/mm/init.c)
 free memory area occupied by initialization function and data
-unlock_kernel()               ---does nothing because this is not SMP system
-create console for standard input/output and error output
-start user process
 do_basic_setup is explained in more detail here.
-set current process to child_reaper
-mtrr_init()
  | initialize fundamental drivers, and create context_thread kernel thread
-start_context_thread()
-do_initcalls()                (init/main.c)
 calls all functions defined with __initcall or module_init macro, one by one
-initialize file system, irda, pcmcia
-mount root file system
-initialize devfs file system
 init tries to launch programs specified by startup option, then /sbin/init,
/etc/init, /bin/init, /bin/sh, if previous file is not found. Once it succeeds,
execution is transferred to that program, and the rest of init code is not
executed.
 If no one can be launched, then panic() is called. Error message is displayed
and system halts then.
 You can see the "Kernel Panic" message when you put only kernel into root file
system and boot.
--------------------------------------------------------------------------------
7. Architecture Dependent Initialization - setup_arch(arch/sh/kernel/setup.c)
 Before doing some initialization, there are things those must be initialized in
archtecture-dependent way. This is the one that is executed.
 Contents of this differs based on configuration. This explanation is based on
the case that SolutionEngine MS7750SE01 be used.
-read root device info from 0x8c001008, which should have been set by sh-ipl+g
-if it is not specified that root be mounted read only, clear the read-only flag
 of root_mountflags
-set up start_code/end_code/end_data/brk field of init_mm (virtual address)
-set up kernel code and data field of ram_resources[] structure(physical
 address)
-analyze command line by parse_cmdline() and determine RAM size and machine type
-initialize variable for paging operation (???not sure on this???)
-execute machine-dependent initialization code, registered in machine vector
 ->setup_se(), this initialize SMSC37C935A Super-IO chip
-set up flags related to FPU in init_task_union
-initialize page table and enable MMU in paging_init()(arch/sh/mm/init.c)
--------------------------------------------------------------------------------
8. Initialization of Exception Handling - trap_init(arch/sh/kernel/traps.c)
 Until here, VBR points to the exception handler provided by sh-ipl+g. This
function sets up so kernel-defined exception handler will be used from then on.
 This function does the following (vbr_base and exception_handling_table[] are
defined in arch/sh/kernel/entry.S)
-set address of functions defined by DO_ERROR macro to exception_handling_table
 [12] and [13]
-store address of current VBR + 0x100 to gdb_vbr_vector
-set the address of vbr_base to VBR
 Explanations are given later about how exception and interrupt handled.
--------------------------------------------------------------------------------
9. Initialization of Interrupt Handling - init_IRQ(arch/sh/kernel/irq_ipr.c)
 Before explaining the contents of this function, let me show several structures
and variables used when interrupts get handled.
 There are four structures defined for interrupt handling definition.
-struct hw_interrupt_type
-irq_desc_t
-struct irqaction
-struct ipr_data
 Those are defined as follows:
<include/linux/irq.h>
-----
struct hw_interrupt_type {
        const char * typename;
        unsigned int (*startup)(unsigned int irq);
        void (*shutdown)(unsigned int irq);
        void (*enable)(unsigned int irq);
        void (*disable)(unsigned int irq);
        void (*ack)(unsigned int irq);
        void (*end)(unsigned int irq);
        void (*set_affinity)(unsigned int irq, unsigned long mask);
};
typedef struct hw_interrupt_type  hw_irq_controller;
typedef struct {
        unsigned int status;            /* IRQ status */
        hw_irq_controller *handler;
        struct irqaction *action;       /* IRQ action list */
        unsigned int depth;             /* nested irq disables */
        spinlock_t lock;
} ____cacheline_aligned irq_desc_t;
-----
<include/linux/interrupt.h>
-----
struct irqaction {
        void (*handler)(int, void *, struct pt_regs *);
        unsigned long flags;
        unsigned long mask;
        const char *name;
        void *dev_id;
        struct irqaction *next;
};
-----
<arch/sh/kernel/irq_ipr.c>
-----
struct ipr_data {
        unsigned int addr;      /* Address of Interrupt Priority Register */
        int shift;              /* Shifts of the 16-bit data */
        int priority;           /* The priority */
};
-----
 struct hw_interrupt_type abstracts interrupt controller.
 typename stores the name of interrupt controller. startup, shutdown, enable,
disable, ack, end, set_affinity store the address of functions to enable/disable
interrupts, or other tasks.
 irq_desc_t is for defining interrupt handling description table. One instance
of this is related to one interrupt number.
 status stores the status of interrupt handling. handler has the pointer to the
instance of hw_irq_controller. action has the pointer to the instance of
irqaction.
 struct irqaction defines the action which should be invoked when interrupt
occur.
 handler contains the address of function to handle interrupt. name stores
the name of interrupt source. dev_id is for Device ID, and actually not pointer.
next is set when IRQ is shared by other device and contains address of next
instance of irqaction.
 struct iprdata is to store information required to control interrupt priority
level register (IPR) of SH interrupt controller.
 Instances using those definitions are defined like this:
<arch/sh/kernel/irq.c>
-----
irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
                                { [0 ... NR_IRQS-1] = { 0, &no;_irq_type, }};
struct hw_interrupt_type no_irq_type = {
        "none",
        startup_none,
        shutdown_none,
        enable_none,
        disable_none,
        ack_none,
        end_none
};
-----
<arch/sh/kernel/irq_ipr.c>
-----
static struct ipr_data ipr_data[NR_IRQS];
static struct hw_interrupt_type ipr_irq_type = {
        "IPR-based-IRQ",
        startup_ipr_irq,
        shutdown_ipr_irq,
        enable_ipr_irq,
        disable_ipr_irq,
        mask_and_ack_ipr,
        end_ipr_irq
};
-----
<arch/sh/kernel/time.c>
-----
static struct irqaction irq0  = { timer_interrupt, SA_INTERRUPT, 0, "timer", NULL, NULL};
-----
 irq_desc is the instance of irq_desc_t, array for each interrupt number. This
is statically allocated at boot time, with no_irq_type and NULL are set to
handler and action fields, respectively.
 no_irq_type is the instance of hw_interrupt_type for the interrupt number not
used specifically. Registered functions does nothing.
 ipr_data[] is the instance of ipr_data, array for each interrupt number.
 ipr_irq_type is the instance of hw_interrupt_type for the interrupt number
registered to use by make_ipr_irq() function. This has the address of functions
those controls interrupt controller of SH processor.
 irq0 is the instance of irqaction, for handling timer interrupt.
 With those information provided, I will explain this function.
 This function first sets up the interrupt priority level register(IPRA-D) of SH
processor. make_ipr_irq()(arch/sh/kernel/irq_ipr.c) sets up the register, and
sets the address of ipr_irq_type to the irq_desc[].handler for the given
interrupt number.
 Here is the summary of operation.
Source          #     used IPR     position in IPR     priority
----------------------------------------------------------------------
TMU0            16      IPRA            3               2
RTC-CUI         22      IPRA            0               2
SCI-ERI         23      IPRB            1               3
SCI-RXI         24      IPRB            1               3
SCI-TXI         25      IPRB            1               3
SCIF-ERI        40      IPRC            1               3
SCIF-RXI        41      IPRC            1               3
SCIF-BRI        42      IPRC            1               3
SCIF-TXI        43      IPRC            1               3
 "position in IPR" corresponds to bits like this
0       3-0
1       7-4
2       11-8
3       15-12
 After those set up, machine-dependent initialization of interrupt is executed.
For the case of SolutionEngine, interrupts for external I/O connected to IRL
line are set up. Here is the summary, similar to the above.
Source          #     used ILCR     position in ILCR    priority
----------------------------------------------------------------------
IDE0            14      ILCRA           2               15-14
Mouse           12      ILCRA           1               15-12
RTC             8       ILCRB           1               15-8
Floppy          6       ILCRC           3               15-6
Printer         5       ILCRC           2               15-5
Serial 1        4       ILCRC           1               15-4
Serial 2        3       ILCRC           0               15-3
Keyboard        1       ILCRD           3               15-1
LAN             10      ILCRD           1               15-10
PCIRQ3          0       ILCRE           3               15-0
PCIRQ2          11      ILCRE           2               15-11
PCIRQ1          9       ILCRE           1               15-9
PCIRQ0          7       ILCRE           0               15-7
SLOTIRQ2        13      ILCRG           1               15-13
SLOTIRQ1        2       ILCRG           0               15-2
 It seems that this part of code sets up the contents of FPGA of SolutionEngine
so that it resembles how IPR works for each internal modules. ILCR seems like
those are located at 0xb1400000 and above, are 16 bit, and works like IPR.
 Since this memory area is not described other than as "Test area, don't touch"
in SolutionEngine Manual, This explanation is derived from my imagination.
(If you have more information and can provide me, I'm happy to add further
explanation here)
 Now all irq_desc[].handler for interrupts from internal module and external I/O
are set up.
--------------------------------------------------------------------------------
10. Initialization of Timer - time_init(arch/sh/kernel/time.c)
 This function sets up handler for timer interrupt, and several time-related
setting.
 First it acquire current date from RealTimeClock(RTC). If the contents of RTC
is invalid, it sets RTC as Jan 1st of 2000.
 Next, irq0 structure described in previous chapter is set up so that registered
function, timer_interrupt(), is used when timer interrupt occur.
 Then, it measures the frequency of decrements of timer counter. Module clock
frequency can be obtained to multiply this by 4.
 Next, ratio of master/CPU/external bus/module clock from FRQCR register, and
frequency of those clocks is set to current_cpu_data structure.
 At last, it programs timer module to generate interrupt every 10mSec (100Hz),
and starts the timer.
 At this point still interrupt is disabled, so no interrupt is detected. It can
be after sti() is called in start_kernel().
--------------------------------------------------------------------------------
11. Exception/Interrupt Handling
 This chapter illustrates the actual exception/interrupt handling sequence.
11.1 Exception Handling Entry
 As explained at trap_init(), VBR is set up to point to vbr_base defined in
arch/sh/kernel/entry.S. There are three entry, general_exception/tlb_miss/
interrupt, each located so that these have offset 0x100/0x400/0x600 from
vbr_base. One of those entry gets executed at the time of exception/interrupt,
according to the type of it.
 Those three entry stores required information in r2 and r3, and jumps to
handle_exception. r2 and r3 contains those.
                        r2(R2_BANK1)            r3(R3_BANK1)
-------------------------------------------------------------
general_exception       contents of EXPEVT      address of ret_from_exception
tlb_miss                contents of EXPEVT      address of ret_from_exception
interrupt               contents of INTEVT      address of ret_from_irq
11.2 Dispatcher for Exception/Interrupt Handling - handle_exception
 This function dispatches to the actual handler, based on the contents of r2.
Here is the summary of operation.
+-[store Stack Pointer to r0]
+-[if it was in kernel mode before interrupt or exception]
| +-[set up r1(R1_BANK1) so it resembles SR as FD bit 1, IMASK F]
| +-[if FPU disable bit is not set]
|   +-[push FPU registers to the stack]
|   +-[set FPSCR.SZ to 0, .PR to 1]
|   +-[set up r1(R1_BANK1) so it resembles SR as FD bit 0, IMASK F]
+-[if it was in user mode before interrupt or exception]
| +-[change stack pointer as it is for the kernel]
| +-[set up r1(R1_BANK1) so it resembles SR as FD bit 1, IMASK F]
+-[push -1L to the stack]
+-[push MACL,MACH,GBR,SSR,PR,SPC to the stack]
+-[set PR with the contents of r3(R3_BANK1)]
+-[push contents of r0 (has the original stack pointer) to the stack]
+-[push r14-r8 to the stack]
+-[set SR.FD and .IMASK using the contents of r1(R1_BANK1), clear .RB and .BL]
+-[push r7(R7_BANK0) to r0(R0_BANK0) to the stack]
+-[jump to the address stored in exception_handling_table[], using r2(R2_BANK1)]
 Here is the illustration of stack at this point.
Address  -------------------------------------------
Low      |original R0_BANK0                        |<--current r15 points here
 |       |original R1_BANK0                        |
 |       |original R2_BANK0                        |
 |       |original R3_BANK0                        |
 |       |original R4_BANK0                        |
 |       |original R5_BANK0                        |
 |       |original R6_BANK0                        |
 |       |original R7_BANK0                        |
 |       |original R8                              |
 |       |original R9                              |
 |       |original R10                             |
 |       |original R11                             |
 |       |original R12                             |
 |       |original R13                             |
 |       |original R14                             |
 |       |original R15(stack pointer)              |
 |       |original PC                              |
 |       |original PR                              |
 |       |original SR                              |
 |       |original GBR                             |
 |       |original MACH                            |
 |       |original MACL                            |
 |       |-1(placeholder for syscall_nr)           |
 |       |original FR0-15(if FPU is used in kernel)|
High     |                                         |<--orig. r15 points here
 PR contains the address of ret_from_exception or ret_from_irq at this point,
so when the actual handler finishes operation, control transfers to either of
those two entry.
 Register bank is changed to BANK0 as SR.RB bit is cleared.
 SR.BL bit is cleared so that interrupt is enabled, but SR.IMASK has F set,
so all interrupts are masked.
 Here I add some explanation about calling convention of function in LinuxSH.
As you can verify by objdump-ing the object file created by GCC, arguments are
passed to function with this convention.
order of arg    using
-----------------------
        0       r4
        1       r5
        2       r6
        3       r7
        4       @r15
        5       @(4,r15)
        6       @(8,r15)
        ...     ...
return val      r0
 Actual handler routine (explained in later section) written in C is defined
like this style.
-----
asmlinkage void handle_specific_exception(
        unsingned long r4,
        unsingned long r5,
        unsingned long r6,
        unsingned long r7,
        struct pt_regs reg)
-----
 "reg" represents the contents of stack from r0 to -1(in place of syscall_nr).
Register r4 to r7 are not set in handle_exception, so the callee can't use first
four arguments. Those are here otherwise callee C function can't receive
arguments properly.
 I explained that those first four arguments may be useless, but those are used
for usual arguments passing between C caller and C callee. If there are 4
arguments or less, these are passed through register, and it's much faster
than passed through stack.
 And System call ABI defines that argument 0 - 3 are passed with r4 to r7,
4 - 6 with r0 to r2. r3 must contain system call number. results are contained
in r0. This style matches the above discussion, because top of stack has
original r0, and it is interpreted as argument 4, and so on.
(BTW, asmlinkage macro does nothing, as include/linux/linkage.h shows.)
 System call ABI gets more explanation later.
11.3 Actual Exception Handling(Excluding System Call and Interrupt)
 handle_exception dispatches to following functions.
Exception Type                   Exception Code Function Name
------------------------------------------------------------
Instruction TLB miss/
Data TLB miss(load)                     0x040   tlb_miss_load
Data TLB miss(store)                    0x060   tlb_miss_store
Initial Page Write                      0x080   initial_page_write
Instruction TLB protection violation/
Data TLB protection violation(load)     0x0a0   tlb_protection_violation_load
Data TLB protection violation(store)    0x0c0   tlb_protection_violation_store
Instruction address error/
Data address error(load)                0x0e0   address_error_load
Data address error(store)               0x100   address_error_store
FPU                                     0x120   do_fpu_error
Unconditional trap(system call)         0x160   system_call
Illegal instruction                     0x180   do_reserved_inst(*)
Illegal slot instruction                0x1a0   do_illegal_slot_inst(*)
break after instruction                 0x1e0   break_point_trap(*)
Illegal fpu instruction                 0x800   do_fpu_state_restore(*)
Illegal slot fpu instruction            0x820   do_fpu_state_restore(*)
 Functions without (*) are defined in arch/sh/kernel/entry.S.
 Functions with (*) are C functions. do_fpu_state_restore() is in arch/sh/kernel
/fpu.c. break_point_trap is in arch/sh/kernel/process.c. Other two are defined
in arch/sh/kernel/traps.c, as explained at chapter 8.
 Unconditional trap occurs when user-mode program makes system-call. This is
explained in next section.
 When those handler finishes, control moves to ret_from_exception entry, because
PR is set to there beforehand. ret_from_exception does the following.
-check the mode when exception occur, and if it was kernel mode then jump to
 restore_all
-if if was user mode then clear the IMASK and jump to ret_from syscall
 After this control goes like the figure and explanation in 11.5, but reaches
restore_all at last. it sets SR.BL and .RB bit, restores registers from stack,
issues rte instruction and exit from exception handling.
11.4 Actual System Call Handling
 system_call handles the case if unconditional trap occurred with trapa
instruction.
 Before I explain the contents of system_call, I'll give summary of system-call
Application Binary Interface(ABI)
 As I have shown above, ABI is defined like this (refer to include/asm-sh/
unistd.h and arch/sh/kernel/entry.S).
argument 0                      r4
argument 1                      r5
argument 2                      r6
argument 3                      r7
argument 4                      r0
argument 5                      r1
argument 6                      r2
system call number              r3
(number of arguments+0x10) * 4  TRA
 One can determine if old ABI is used or new ABI, with the contents of TRA.
This document only describes new ABI.
 For the case of BIOS/gdb-call, TRA contains 0x20*4 or more.
 Caller must set up arguments as above.
 At the entry of system_call, SR.RB is cleared, so those registers above contain
values those set by the caller. Those are kept and passed to system call handler
function, except the case if ptrace is used.
 Here is what system_call does. (I can't give explanation related to ptrace,
since I don't know about it)
-store contents of TRA to r8
-jump to debug_trap if trap number is 0x20 or more
-calculate number of arguments from r8, and left-shift it by 16 bits
-store higher 16 bits of r8 and lower 16 bits of r3 to r10
-store contents of r10 in place of system call number(syscall_nr) in the stack
-clear IMASK
-if ptrace of user task is set, call syscall_trace()(arch/sh/kernel/ptrace.c),
 restore contents of r3 to r7 from stack, set address of syscall_ret_trace in PR
-if ptrace is not set, set address of syscall_ret to PR
-if system call number is greater than NR_syscalls, issue rts instruction and
 move to syscall_ret_trace or syscall_ret
-jump to the function registered in sys_call_table[system call number]
 If system call number is valid, one of system call handler function is executed
here. System call handler functions are written in C and follow the calling
convention as I described earlier. You can see that at this point register and
stack are set up correctly, from explanation above.
 When handler function return, control moves to syscall_ret_trace or syscall_ret
as one of those address is set to PR.
 syscall_ret stores the contents of r0 to the stack area where contain original
value of r0, so that return value from handler gets returned to system call
caller. Then it continues execution to ret_from_syscall.
 After this control goes like the figure and explanation in 11.5, but reaches
restore_all at last. it sets SR.BL and .RB bit, restores registers from stack,
issues rte instruction and exit from exception handling.
11.5 Actual Interrupt Handling
 There are three kinds of interrupts: non-maskable interrupt, external device
interrupt and internal module interrupt.
 Non-maskable interrupt is serviced as not doing anything but just return, as
defined in entry.S (there is a comment that this is not implemented yet).
 External device and internal module interrupt are set up to jump to do_IRQ(arch
/sh/kernel/irq.c) function. As described above, C function is assembled as first
four arguments are passed by registers, and those are not used in that routine.
Next argument, regs, is used as it is the contents of stack and original
registers.
 do_IRQ() does the following.
-obtain IRQ number from the content of R2_BANK1
-increment number of interrupts field of kstat structure
-get pointer of irq_desc_t for the given IRQ number
-ack interrupt by calling ack() of hw_interrupt_type structure registered in
 irq_desc_t structure
-clear IRQ_REPLAY and IRQ_WAITING bit and set IRQ_PENDING bit of status field
 of irq_desc_t structure
-if both IRQ_DISABLED and IRQ_INPROGRESS are not set in the status field, clear
 IRQ_PENDING bit, set IRQ_INPROGRESS bit, get pointer to irqaction structure
 from action field of irq_desc_t
-return if action is NULL
-in handle_IRQ_event(), call function registered to handler field of irqaction
 to service interrupt, for all chained irqaction structure
-clear IRQ_PENDING and IRQ_INPROGRESS bit of status field of irq_desc_t
-if IRQ_DISABLED is not set, notify interrupt controller about the end of
 servicing interrupt by calling end() of hw_interrupt_type structure
-if softirqactive flag is set and not masked, call do_softirq()
 As an example, timer_interrupt() is set as handler of irqaction structure, so
each timer interrupt causes call to timer_interrupt().
 Control transfers to ret_from_irq since it is set to PR before calling do_IRQ.
ret_from_irq does the following.
-If it was kernel mode when interrupt occur, jump to restore_all
-if it was user mode, restore SR.IMASK and jump to ret_with_reschedule
-in ret_with_reschedule, if user mode task needs rescheduled, set address of
 ret_from_syscall to PR and calls schedule(). schedule() may not return if
 task switching occur until this task gets switched in.
-At ret_from_syscall, softirq is handled by do_softirq() if it is active and not
 masked, and goes back to ret_with_reschedule
-if rescheduling is not needed, jump to the restore_all if no pending signal.
 if there are pending signal, handle it with do_signal and then jump to the
 restore_all
-restore_all sets SR.BL and .RB bit, restores registers from stack, issues rte
 instruction and exit from interrupt handling.
 Following figure may explain this operation more clearly.
#ret_from_irq#
 |Kernel   |User mode
 |Mode     |
 |         +->#ret_with_reschedule#<----------------------+
 |            |re-     |                                  |
 |         not|schedule|required                          |
 |    required|        +->#reschedule#                    |
 |            |             |                             |
 |            |             +->#schedule#(kernel/sched.c) |
 |            |                 |                         |
 |            |                 +->#ret_from_syscall#     |
 |            |                  none|softirq|exist       |
 |            |                      |       |            |
 |            |                      |       +------------+
 |            |                      |                    |
 |            | exist                +->#handle_softirq#  |
 |      signal+--->#signal_return#         |              |
 |        none|        |                   +->#do_softirq#(kernel/softirq.c)
 |            |        |
 |            |        +->#do_signal#(arch/sh/kernel/signal.c)
 |            |             |
 +------------+-------------+------------------>#restore_all#
 Thus the interrupt handling finishes like this.


printer friendly Linux is a trademark of Linus Torvalds. 
SuperH and Cool Engine are registered trademarks of Hitachi, Ltd.

Last update: 28/08/2026

This page ©2003 A&D; Co., Ltd.. Permission granted to copy and display.
$Id: prtemplate.php3,v 1.4 2026/08/28 16:30:56 lethal Exp $