2011年1月27日 星期四

Linux boot over arm

boot start

traps.c in arch\arm\kernel
early_trap_init(void)
{
    memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
    memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);
    memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
}



entry-armv.S in arch\arm\kernel

    .equ    stubs_offset, __vectors_start + 0x200 - __stubs_start

    .globl    __vectors_start
__vectors_start:
 ARM(    swi    SYS_ERROR0    )
 THUMB(    svc    #0        )
 THUMB(    nop            )
    W(b)    vector_und + stubs_offset
    W(ldr)    pc, .LCvswi + stubs_offset
    W(b)    vector_pabt + stubs_offset
    W(b)    vector_dabt + stubs_offset
    W(b)    vector_addrexcptn + stubs_offset
    W(b)    vector_irq + stubs_offset
    W(b)    vector_fiq + stubs_offset

    .globl    __vectors_end
__vectors_end:

Linux boot over arm

entry-armv.S in arch\arm\kernel

2011年1月25日 星期二

RCU

http://en.wikipedia.org/wiki/Read-copy-update

Linux barrier

#define barrier() __asm__ __volatile__("": : :"memory")
The major of work is to enforce refresh the cache in cpu


CPU越过内存屏障后,将刷新自己对存储器的缓冲状态。这条语句实际上不生成任何代码,但可使gcc在barrier()之后刷新寄存器对变量的分配。    
             例1:
                1        int a = 5, b = 6;
                2        barrier();
                3        a = b;
     
            在line 3,GCC不会用存放b的寄存器给a赋值,而是重新读内存中的b值,赋值给a。

The other major purpose of barrier() is to synchronize two(or more) CPUs caches, it often used in SMP enviroment. 

2011年1月6日 星期四

English

screw it (rude)(管他的)

I do not care what happens or what someone does She didn't want to see the movie, but I thought, screw it, I'm going anyway.
Usage notes: usually used when the speaker is annoyed

With that in mind(說到這一點)

Softirq - Software Interrupt Context: Bottom Halves, Tasklets, softirq

top-half=hardirq/bottom-half=sotfirq
Threaded interrupt handlers: reduce that work/latency even further
http://people.netfilter.org/rusty/unreliable-guides/kernel-hacking/basics-softirqs.html
http://lwn.net/Articles/302043/

Softirq
/* PLEASE, avoid to allocate new softirqs, if you need not _really_ high
   frequency threaded job scheduling. For almost all the purposes
   tasklets are more than enough. F.e. all serial device BHs et
   al. should be converted to tasklets, not to softirqs.
 */


 Tasklets --- multithreaded analogue of BHs.

Main feature differing them of generic softirqs: tasklet
is running only on one CPU simultaneously.

2011年1月5日 星期三

linux_random

int request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *), unsigned long irqflags, const char * devname, void *dev_id)
中的irqflags加入SA_SAMPLE_RANDOM
會在setup_irq(unsigned int irq, struct irqaction* act);
if request_irq() with SA_SAMPLE_RANDOM flag, the setup_irq() will call rand_initialize_irq()


If irqaction has SA_SAMPLE_RANDOM flag, handle_IRQ_event() will call add_interrupt_randomness


The disk and keyboard/mouse is more appropriate, the periodic interrupt like timer interrupt is not suitable.

Linux device and driver : USB example

arch\arm\mach-pxa\devices.c
struct platform_device pxa27x_device_ohci = {
    .name        = "pxa27x-ohci",
    .id        = -1,
    .dev        = {
        .dma_mask = &pxa27x_ohci_dma_mask,
        .coherent_dma_mask = DMA_BIT_MASK(32),
    },
    .num_resources  = ARRAY_SIZE(pxa27x_resource_ohci),
    .resource       = pxa27x_resource_ohci,
};
static struct platform_driver ohci_hcd_pxa27x_driver = {
    .probe        = ohci_hcd_pxa27x_drv_probe,
    .remove        = ohci_hcd_pxa27x_drv_remove,
    .shutdown    = usb_hcd_platform_shutdown,
    .driver        = {
        .name    = "pxa27x-ohci",
        .owner    = THIS_MODULE,
#ifdef CONFIG_PM
        .pm    = &ohci_hcd_pxa27x_pm_ops,
#endif
    },
};


static const struct hc_driver ohci_pxa27x_hc_driver = {
    .description =        hcd_name,
    .product_desc =        "PXA27x OHCI",
    .hcd_priv_size =    sizeof(struct pxa27x_ohci),

    /*
     * generic hardware linkage
     */
    .irq =            ohci_irq,
    .flags =        HCD_USB11 | HCD_MEMORY,

    /*
     * basic lifecycle operations
     */
    .start =        ohci_pxa27x_start,
    .stop =            ohci_stop,
    .shutdown =        ohci_shutdown,

    /*
     * managing i/o requests and associated device resources
     */
    .urb_enqueue =        ohci_urb_enqueue,
    .urb_dequeue =        ohci_urb_dequeue,
    .endpoint_disable =    ohci_endpoint_disable,

    /*
     * scheduling support
     */
    .get_frame_number =    ohci_get_frame,

    /*
     * root hub support
     */
    .hub_status_data =    ohci_hub_status_data,
    .hub_control =        ohci_hub_control,
#ifdef  CONFIG_PM
    .bus_suspend =        ohci_bus_suspend,
    .bus_resume =        ohci_bus_resume,
#endif
    .start_port_reset =    ohci_start_port_reset,
};

drivers\usb\host\ohci-pxa27x.c
static int ohci_hcd_pxa27x_drv_probe(struct platform_device *pdev);
/**
 * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
 * Context: !in_interrupt()
 *
 * Allocates basic resources for this USB host controller, and
 * then invokes the start() method for the HCD associated with it
 * through the hotplug entry's driver_data.
 *
 */
int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device *pdev);
drivers\usb\core\hcd.c
usb_add_hcd




2010年12月29日 星期三

Linux interrupt flow and data structure overview

__irq_usr or __irq_svc in arcn\arm\kernl\entry-armv.S
->
irq_handler in .macro    irq_handler in arcn\arm\kernl\entry-armv.S
->asm_do_IRQ()->
generic_handle_irq()->generic_handle_irq_desc()->
desc->handle_irq(irq, desc)->handle_IRQ_event()->
action->handler(irq, action->dev_id);

struct irqaction {
    irq_handler_t handler;
    unsigned long flags;
    const char *name;
    void *dev_id;
    struct irqaction *next;
    int irq;
    struct proc_dir_entry *dir;
    irq_handler_t thread_fn;
    struct task_struct *thread;
    unsigned long thread_flags;
};

data structure

interrupt flow

2010年12月27日 星期一

modify irq_desc

\include\linux\irq.h
## provide extern IRQ handler interface of irq_desc
## provide extern Handling irq action chains of irq_desc
struct irq_desc {};
extern struct irq_desc irq_desc[NR_IRQS];
extern void handle_simple_irq(unsigned int irq, struct irq_desc *desc);
static inline void set_irq_handler(unsigned int irq, irq_flow_handler_t handle);
set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip, irq_flow_handler_t handle);
set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip, irq_flow_handler_t handle, const char *name);
extern int set_irq_chip(unsigned int irq, struct irq_chip *chip);
extern int set_irq_data(unsigned int irq, void *data);
extern int set_irq_chip_data(unsigned int irq, void *data);
extern int setup_irq(unsigned int irq, struct irqaction *new);
extern void remove_irq(unsigned int irq, struct irqaction *act);


\include\linux\irqnr.h
extern struct irq_desc *irq_to_desc(unsigned int irq);
 
\kernel\irq\chip.c

## irq_handler (handle_irq of irq_desc)
## irq chip (chip of irq_desc)
## irq handler flow function (handle_irq of irq_desc)
int set_irq_chip(unsigned int irq, struct irq_chip *chip);
__set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,const char *name);
void set_irq_probe(unsigned int irq);
int set_irq_type(unsigned int irq, unsigned int type);
int set_irq_data(unsigned int irq, void *data);
static inline void mask_ack_irq(struct irq_desc *desc, int irq);
static inline void mask_irq(struct irq_desc *desc, int irq);
static inline void unmask_irq(struct irq_desc *desc, int irq);


void handle_simple_irq(unsigned int irq, struct irq_desc *desc);
void handle_level_irq(unsigned int irq, struct irq_desc *desc);



\kernel\irq\handle.c
## interrupt action flow
## __do_IRQ flow
irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action);
unsigned int __do_IRQ(unsigned int irq);


\kernel\irq\irqdesc.c in v2.6.37
struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {initial data};
// do action->handler(irq, action->dev_id);
struct irq_desc *irq_to_desc(unsigned int irq);
struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node);

kernel\irq\manage.c
## provide operation interface irq_desc
## add/remove irqaction list of irq_desc table
setup_irq(unsigned int irq, struct irqaction* act);
void remove_irq(unsigned int irq, struct irqaction *act);
void disable_irq(unsigned int irq);
void enable_irq(unsigned int irq);

include\linux\interrupt.h
static inline int __must_check
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
        const char *name, void *dev)
{
    return request_threaded_irq(irq, handler, NULL, flags, name, dev);
}

2010年12月23日 星期四

USB knowledge

http://stenlyho.blogspot.com/2008/08/uhciohciehci.html

ADD/REMOVE IRQaction list of irq_desc

kernel/irq/manage.c
## add/remove irqaction list of irq_desc table
setup_irq(unsigned int irq, struct irqaction* act);
__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new);

 void remove_irq(unsigned int irq, struct irqaction *act);
static struct irqaction *__free_irq(unsigned int irq, void *dev_id);

spinlock

#if defined(CONFIG_SMP)
//spinlock_types.h (D:\linux-2.6.36.2\arch\arm\include\asm)
# include <asm/spinlock_types.h>
typedef struct {
    volatile unsigned int lock;
} arch_spinlock_t;
#else
# include <linux/spinlock_types_up.h>
#ifdef CONFIG_DEBUG_SPINLOCK

typedef struct {
    volatile unsigned int slock;
} arch_spinlock_t;

#define __ARCH_SPIN_LOCK_UNLOCKED { 1 }

#else

typedef struct { } arch_spinlock_t;

#define __ARCH_SPIN_LOCK_UNLOCKED { }

#endif

#endif

ARM CONFIG_SPARSE_IRQ: Support sparse irq numbering

arch/arm/Kconfig

The configuration item CONFIG_SPARSE_IRQ:
  • prompt:
  • type: tristate
  • depends on: (none)
  • defined in arch/arm/Kconfig
  • found in Linux Kernels: from 2.6.29 release still available on 2.6.36 release

Help text

This enables support for sparse irqs. This is useful in general as most CPUs have a fairly sparse array of IRQ vectors, which the irq_desc then maps directly on to. Systems with a high number of off-chip IRQs will want to treat this as experimental until they have been independently verified.

linux PIC

http://lxr.linux.no/#linux+v2.6.36/kernel/irq/handle.c#L138 
/*
  59 * Linux has a controller-independent interrupt architecture.
  60 * Every controller has a 'controller-template', that is used
  61 * by the main code to do the right thing. Each driver-visible
  62 * interrupt source is transparently wired to the appropriate
  63 * controller. Thus drivers need not be aware of the
  64 * interrupt-controller.
  65 *
  66 * The code is designed to be easily extended with new/different
  67 * interrupt controllers, without having to do assembly magic or
  68 * having to touch the generic code.
  69 *
  70 * Controller mappings for all interrupt sources:
  71 */

2010年12月22日 星期三

Linux boot over arm

1./arch/arm/kernel/entry-armv.S
(general HAL of linux kernel)
## 存放vector_table
## include dependent platform specific
...
#include <mach/entry-macro.S>
...
// get_irqnr_preamble and get_irqnr_and_base def in enrty-macro.S
get_irqnr_preamble
//取得irq number from HAL level 
get_irqnr_and_base
... 
//進入os level之處裡中斷 
asm_do_IRQ
 
2./arch/arm/mach-aaec2000/include/mach/entry-macro.S
(platform HAL for linux kernel)
## 取得IRQ NR的MACRO 
## 取得IRQ NR的MACRO
include <mach/irqs.h>...
.macro  get_irqnr_and_base, irqnr, irqstat, base, tmp
...
 
3./arch/arm/mach-aaec2000/include/mach/irqs.h
## IRQ table
 
4. arch/arm/mach-aaec2000/include/mach/aaec2000.h
## Device Memory Mapping
## especially Interrupt controller
5.arch/arm/kernel/irq.c
...
asmlinkage void __exception asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
...

linux over arm - entry-armv.S

http://lxr.linux.no/#linux+v2.6.35.9/arch/arm/kernel/entry-armv.S#L1224

2010年12月21日 星期二

bss section

The computer program memory is organized into the following:

    * Data Segment (Data + BSS + Heap)
    * Stack
    * Code segment
Typically, the program loader initializes the memory allocated
for the bss section when it loads the program. Operating systems
may use a technique called zero-fill-on-demand to efficiently
implement the bss segment (McKusick & Karels 1986).
In embedded software, the bss segment is mapped into memory that
is initialized to zero by the C run-time system before main()
is entered.
 
#include <stdio.h>

extern int __bss_start, _end;

int a, b, c, d;  /* un-initialized */

int main()
{
        int *ptr;
        a = 1, b = 2, c = 3, d = 4;

        for (ptr = &__bss_start; ptr != &_end; ptr++) {
                printf("%d\n", *ptr);
        }
        return 0;
}

Semaphore

function V(semaphore S):
    Atomically increment S
    [S ← S + 1]

function P(semaphore S):
    repeat:
        Between repetitions of the loop other processes may operate on the semaphore
        [if S > 0:
            Atomically decrement S - note that S cannot become negative
            S ← S - 1
            break]

SeqLock - writers-preference

It is a reader-writer consistent mechanism which
avoids the problem of writer starvation. 
 
The reader never blocks, but it may have to retry
if a write is in progress; this speeds up the readers
in the case where the data was not modified, since they
do not have to acquire the lock as they would with a
traditional read-write lock. Also, writers do not wait
for readers, whereas with traditional read-write locks
they do, leading to potential resource starvation in a
situation where there are a number of readers (because
the writer must wait for there to be no readers). 
 
Because of these two factors, seqlocks are more efficient than
traditional read-write locks for the situation where there are
many readers and few writers. The drawback is that if there is
too much write activity or the reader is too slow, they might
livelock (and the readers may starve). 
 
 
 
 
int readcount, writecount; (initial value = 0)
semaphore mutex 1, mutex 2, mutex 3, w, r ; (initial value = 1)
 
READER
  P(mutex 3);
    P(r);
      P(mutex 1);
        readcount := readcount + 1;
        if readcount = 1 then P(w);
      V(mutex 1);
    V(r);
  V(mutex 3);
 
  reading is done
 
  P(mutex 1);
    readcount := readcount - 1;
    if readcount = 0 then V(w);
  V(mutex 1);
 
WRITER
    P(mutex 2);
      writecount := writecount + 1;
      if writecount = 1 then P(r);
    V(mutex 2);
 
  P(w);
    writing is performed
  V(w);
 
  P(mutex 2);
    writecount := writecount - 1;
    if writecount = 0 then V(r);
  V(mutex 2);
reference:http://en.wikipedia.org/wiki/Readers-writer_lock