Chinaunix首页 | 论坛 | 博客
  • 博客访问: 75309
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 172
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-19 15:48
个人简介

此时正当修行时

文章分类

全部博文(32)

文章存档

2013年(32)

我的朋友

分类: LINUX

2013-02-25 20:48:30

the asm offsets

This is the structure offsets required by lg.ko's switcher.S.Unfortunately we don't have infrastructure for private asm-offsets creation.

 

/*

 * Generate definitions needed by assembly language modules.

 * This code generates raw asm output which is post-processed

 * to extract and format the required data.

 */

#include 

#include 

#include 

#include 

#include 

#include 

#include "sigframe.h"

#include 

#include 

#include 

#include 

#include 

#include 

#ifdef CONFIG_LGUEST_GUEST

#include 

#include "../../../drivers/lguest/lg.h"

#endif

#define DEFINE(sym, val) \

        asm volatile("\n->" #sym " %0 " #val : : "i" (val))

#define BLANK() asm volatile("\n->" : : )

#define OFFSET(sym, str, mem) \

        DEFINE(sym, offsetof(struct str, mem));

/* workaround for a warning with -Wmissing-prototypes */

void foo(void);

void foo(void)

{

        OFFSET(SIGCONTEXT_eax, sigcontext, eax);

        OFFSET(SIGCONTEXT_ebx, sigcontext, ebx);

        OFFSET(SIGCONTEXT_ecx, sigcontext, ecx);

        OFFSET(SIGCONTEXT_edx, sigcontext, edx);

        OFFSET(SIGCONTEXT_esi, sigcontext, esi);

        OFFSET(SIGCONTEXT_edi, sigcontext, edi);

        OFFSET(SIGCONTEXT_ebp, sigcontext, ebp);

        OFFSET(SIGCONTEXT_esp, sigcontext, esp);

        OFFSET(SIGCONTEXT_eip, sigcontext, eip);

        BLANK();

        OFFSET(CPUINFO_x86, cpuinfo_x86, x86);

        OFFSET(CPUINFO_x86_vendor, cpuinfo_x86, x86_vendor);

        OFFSET(CPUINFO_x86_model, cpuinfo_x86, x86_model);

        OFFSET(CPUINFO_x86_mask, cpuinfo_x86, x86_mask);

        OFFSET(CPUINFO_hard_math, cpuinfo_x86, hard_math);

        OFFSET(CPUINFO_cpuid_level, cpuinfo_x86, cpuid_level);

        OFFSET(CPUINFO_x86_capability, cpuinfo_x86, x86_capability);

        OFFSET(CPUINFO_x86_vendor_id, cpuinfo_x86, x86_vendor_id);

        BLANK();

        OFFSET(TI_task, thread_info, task);

        OFFSET(TI_exec_domain, thread_info, exec_domain);

        OFFSET(TI_flags, thread_info, flags);

        OFFSET(TI_status, thread_info, status);

        OFFSET(TI_preempt_count, thread_info, preempt_count);

        OFFSET(TI_addr_limit, thread_info, addr_limit);

        OFFSET(TI_restart_block, thread_info, restart_block);

        OFFSET(TI_sysenter_return, thread_info, sysenter_return);

        OFFSET(TI_cpu, thread_info, cpu);

        BLANK();

        OFFSET(GDS_size, Xgt_desc_struct, size);

        OFFSET(GDS_address, Xgt_desc_struct, address);

        OFFSET(GDS_pad, Xgt_desc_struct, pad);

        BLANK();

        OFFSET(PT_EBX, pt_regs, ebx);

        OFFSET(PT_ECX, pt_regs, ecx);

        OFFSET(PT_EDX, pt_regs, edx);

        OFFSET(PT_ESI, pt_regs, esi);

        OFFSET(PT_EDI, pt_regs, edi);

        OFFSET(PT_EBP, pt_regs, ebp);

        OFFSET(PT_EAX, pt_regs, eax);

        OFFSET(PT_DS,  pt_regs, xds);

        OFFSET(PT_ES,  pt_regs, xes);

        OFFSET(PT_FS,  pt_regs, xfs);

        OFFSET(PT_ORIG_EAX, pt_regs, orig_eax);

        OFFSET(PT_EIP, pt_regs, eip);

        OFFSET(PT_CS,  pt_regs, xcs);

        OFFSET(PT_EFLAGS, pt_regs, eflags);

        OFFSET(PT_OLDESP, pt_regs, esp);

        OFFSET(PT_OLDSS,  pt_regs, xss);

        BLANK();

        OFFSET(EXEC_DOMAIN_handler, exec_domain, handler);

        OFFSET(RT_SIGFRAME_sigcontext, rt_sigframe, uc.uc_mcontext);

        BLANK();

        OFFSET(pbe_address, pbe, address);

        OFFSET(pbe_orig_address, pbe, orig_address);

        OFFSET(pbe_next, pbe, next);

        /* Offset from the sysenter stack to tss.esp0 */

        DEFINE(TSS_sysenter_esp0, offsetof(struct tss_struct, x86_tss.esp0) -

                 sizeof(struct tss_struct));

        DEFINE(PAGE_SIZE_asm, PAGE_SIZE);

        DEFINE(PAGE_SHIFT_asm, PAGE_SHIFT);

        DEFINE(PTRS_PER_PTE, PTRS_PER_PTE);

        DEFINE(PTRS_PER_PMD, PTRS_PER_PMD);

        DEFINE(PTRS_PER_PGD, PTRS_PER_PGD);

        DEFINE(VDSO_PRELINK_asm, VDSO_PRELINK);

        OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);

#ifdef CONFIG_PARAVIRT

        BLANK();

        OFFSET(PARAVIRT_enabled, paravirt_ops, paravirt_enabled);

        OFFSET(PARAVIRT_irq_disable, paravirt_ops, irq_disable);

        OFFSET(PARAVIRT_irq_enable, paravirt_ops, irq_enable);

        OFFSET(PARAVIRT_irq_enable_sysexit, paravirt_ops, irq_enable_sysexit);

        OFFSET(PARAVIRT_iret, paravirt_ops, iret);

        OFFSET(PARAVIRT_read_cr0, paravirt_ops, read_cr0);

#endif

#ifdef CONFIG_XEN

        BLANK();

        OFFSET(XEN_vcpu_info_mask, vcpu_info, evtchn_upcall_mask);

        OFFSET(XEN_vcpu_info_pending, vcpu_info, evtchn_upcall_pending);

#endif

#ifdef CONFIG_LGUEST_GUEST

        BLANK();

        OFFSET(LGUEST_DATA_irq_enabled, lguest_data, irq_enabled);

        OFFSET(LGUEST_PAGES_host_gdt_desc, lguest_pages, state.host_gdt_desc);

        OFFSET(LGUEST_PAGES_host_idt_desc, lguest_pages, state.host_idt_desc);

        OFFSET(LGUEST_PAGES_host_cr3, lguest_pages, state.host_cr3);

        OFFSET(LGUEST_PAGES_host_sp, lguest_pages, state.host_sp);

        OFFSET(LGUEST_PAGES_guest_gdt_desc, lguest_pages,state.guest_gdt_desc);

        OFFSET(LGUEST_PAGES_guest_idt_desc, lguest_pages,state.guest_idt_desc);

        OFFSET(LGUEST_PAGES_guest_gdt, lguest_pages, state.guest_gdt);

        OFFSET(LGUEST_PAGES_regs_trapnum, lguest_pages, regs.trapnum);

        OFFSET(LGUEST_PAGES_regs_errcode, lguest_pages, regs.errcode);

        OFFSET(LGUEST_PAGES_regs, lguest_pages, regs);

#endif

}

 

注释:

List:       
Subject:    
From:       
Date:       
 

Almost every architecture generates Assembler values to map the offsets of fields in C structures, about the only exception is i386 and that is because its offsets are hard coded into entry.S.  Every arch has done it differently, none of them have got it exactly right.

 

As part of kbuild 2.5 I am standardizing on one method for generating Assembler offsets.  This change is required for kbuild 2.5 but it can be added to 2.4 without disturbing the current kbuild, I want to do this gradually now instead of a single massive change in kernel 2.5. I will be issuing per architecture changes for generating Assembler offsets against 2.4.

 

The kbuild 2.5 method for generating Assembler offsets satisfies these requirements:

 

* No manual intervention required.Many architectures rely on users running make dep after changing config options that affect the Assembler offsets.  If the user forgets to run make dep then the C and Assembler code is out of sync - totally unacceptable.  This is completely fixed in kbuild 2.5; I cannot do a complete fix in kbuild 2.4 but it is still better than the existing manual system.

 

* Standard name for the related files.  There are 6+ different names for the files used to generate Assembler offsets, kbuild 2.5 uses asm-offsets.[csh] on all architectures.

 

* Allows for multiple parallel compiles from the same source tree. Writing the generated asm-offsets.h to include/asm is not an option, it prevents concurrent compiles.

 

* The method must work in native and cross compile mode and give exactly the same results.  Some 2.4 code only works in native mode, some architectures have different methods for native and cross compile with different output formats.  Yeuch!

 

* Standard scripts for generating the output.  Every arch does it differently in 2.4, standards are good!

 

* Correct dependency trees.  Because 2.4 make dep does not scan .S files, there is little or no dependency information.  Even if the offsets are regenerated, the affected Assembler code does not always get rebuilt. kbuild 2.5 handles dependencies for Assembler as well as C; I cannot get kbuild 2.4 perfect but I can improve on the existing (or non-existent) 2.4 dependencies.

 

All architectures will define arch/$(ARCH)/asm-offsets.c.  This has a standard prologue for the macros that convert offsets to Assembler, followed by arch specific field references.

 

arch/$(ARCH)/asm-offsets.s is generated from arch/$(ARCH)/asm-offsets.c using standard rules, although kbuild 2.4 needs some tweaking.

 

arch/$(ARCH)/asm-offsets.h is generated from arch/$(ARCH)/asm-offsets.s by a semi-standard script.  Most of the script is common to all architectures but the precise format of the Assembler output is arch specific.

 

The final result is included in *only* the Assembler programs that need it, as #include "asm-offsets.h" with -I arch/$(ARCH) in the relevant Makefiles.  Hard coding relative paths in source files is a pet hate, use #include "localname.h" and -I instead.  Including the generated file in C code is not allowed, it severly pollutes the dependency chain, to the extent that any config change can force a complete recompile, unacceptable.

 

 

Example from i386:

 

arch/i386/asm-offsets.c

 

/*

 * Generate definitions needed by assembly language modules.

 * This code generates raw asm output which is post-processed to extract

 * and format the required data.

 */

 

#include

#include

#include

 

/* Use marker if you need to separate the values later */

 

#define DEFINE(sym, val, marker) \

  asm volatile("\n-> " #sym " %0 " #val " " #marker : : "i" (val))

 

#define BLANK() asm volatile("\n->" : : )

 

int

main(void)

{

  DEFINE(state,        offsetof(struct task_struct, state),);

  DEFINE(flags,        offsetof(struct task_struct, flags),);

  DEFINE(sigpending,   offsetof(struct task_struct, sigpending),);

  DEFINE(addr_limit,   offsetof(struct task_struct, addr_limit),);

  DEFINE(exec_domain,  offsetof(struct task_struct, exec_domain),);

  DEFINE(need_resched, offsetof(struct task_struct, need_resched),);

  DEFINE(tsk_ptrace,   offsetof(struct task_struct, ptrace),);

  DEFINE(processor,    offsetof(struct task_struct, processor),);

  BLANK();

  DEFINE(ENOSYS,       ENOSYS,);

  return 0;

}

 

asm-offsets.s to asm-offsets.h.

 

# Convert raw asm offsets into something that can be included as

# assembler definitions.  It converts

#   -> symbol $value source

# into

#   symbol = value /* 0xvalue source */

 

echo '#ifndef __ASM_OFFSETS_H__'

echo '#define __ASM_OFFSETS_H__'

echo '/*'

echo ' * DO NOT MODIFY'

echo ' *'

echo " * This file was generated by arch/$(ARCH)/Makefile.in."

echo ' *'

echo ' */'

echo ''

awk '

  /^->$/{printf("\n")}

  /^-> /{

    sym = $2;

    val = $3;

    sub(/^\$/, "", val);

    $1 = "";

    $2 = "";

    $3 = "";

    printf("%-20s = %3d\t/* 0x%x\t%s */\n", sym, val, val, $0)

  }

'

echo '#endif'

 

Generated arch/i386/asm-offsets.h

 

#ifndef __ASM_OFFSETS_H__

#define __ASM_OFFSETS_H__

/*

 * DO NOT MODIFY

 *

 * This file was generated by arch/i386/Makefile.in.

 *

 */

 

state              =   0      /* 0x0     offsetof(struct task_struct, state) */

flags              =   4      /* 0x4     offsetof(struct task_struct, flags) */

sigpending         =   8      /* 0x8     offsetof(struct task_struct, sigpending) */

addr_limit       =  12      /* 0xc     offsetof(struct task_struct, addr_limit) */

exec_domain      =  16      /* 0x10    offsetof(struct task_struct, exec_domain) */

need_resched     =  20      /* 0x14    offsetof(struct task_struct, need_resched) */

tsk_ptrace       =  24      /* 0x18    offsetof(struct task_struct, ptrace) */

processor        =  52      /* 0x34    offsetof(struct task_struct, processor) */

ENOSYS           =  38      /* 0x26    ENOSYS */

#endif
阅读(1162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~