中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

武漢app開發(fā)靠譜的公司湖北網(wǎng)站seo策劃

武漢app開發(fā)靠譜的公司,湖北網(wǎng)站seo策劃,婚禮策劃網(wǎng)站設計,為什么做獨立站的人都不止一個網(wǎng)站文章目錄 簡介一、altinstructions節(jié)1.1 .altinstructions1.2 .rela.altinstructions 二、內核模塊重定位源碼分析參考資料 簡介 在內核開發(fā)中,有時需要對內核代碼進行修補,以解決bug、優(yōu)化性能或引入新功能。替代指令(altinstructions&…

文章目錄

  • 簡介
  • 一、altinstructions節(jié)
    • 1.1 .altinstructions
    • 1.2 .rela.altinstructions
  • 二、內核模塊重定位源碼分析
  • 參考資料

簡介

在內核開發(fā)中,有時需要對內核代碼進行修補,以解決bug、優(yōu)化性能或引入新功能。替代指令(altinstructions)提供了一種在不修改原始代碼的情況下進行修補的方法。它允許開發(fā)者在原始指令的位置插入替代指令,以實現(xiàn)所需的功能變更。

內核版本:4.19.90
處理器架構:aarch64

以vfat.ko模塊為例:

# readelf -S vfat.ko
There are 35 section headers, starting at offset 0x6c08:節(jié)頭:[] 名稱              類型             地址              偏移量大小              全體大小          旗標   鏈接   信息   對齊......[10] .altinstructions  PROGBITS         0000000000000000  000037680000000000000078  0000000000000000   A       0     0     1[11] .rela.altinstruct RELA             0000000000000000  000037e000000000000001e0  0000000000000018   I      31    10     8......

我目前這臺機器arm64 架構的OS的內核模塊沒有 .altinstr_replacement 節(jié)(還是arm64 架構的 Linux 都沒有.altinstr_replacement 節(jié),這點有待確定)。

一、altinstructions節(jié)

1.1 .altinstructions

.altinstructions 是 Linux 內核中的一個特殊節(jié)(section),用于定義指令替換規(guī)則。它允許內核在運行時替換特定的匯編指令序列,以提高性能或解決特定的問題。

指令替換的規(guī)則包括兩個部分:原始指令序列和替換指令序列。內核在執(zhí)行時會檢查原始指令序列是否匹配,如果匹配則使用替換指令序列來取代它。

.altinstructions 節(jié)主要用于在 Linux 內核中進行指令替換或修補。它提供了在運行時替換特定指令序列的機制,常用于優(yōu)化關鍵代碼路徑或解決硬件問題。

struct alt_instr {s32 orig_offset;	/* offset to original instruction */s32 alt_offset;		/* offset to replacement instruction */u16 cpufeature;		/* cpufeature bit set for replacement */u8  orig_len;		/* size of original instruction(s) */u8  alt_len;		/* size of new instruction(s), <= orig_len */
};

(1)orig_offset(s32類型):原始指令的偏移量。指示原始指令在代碼段中的位置。
(2)alt_offset(s32類型):替換指令的偏移量。指示替換指令在代碼段中的位置。
(3)cpufeature(u16類型):替換指令所需的CPU特性位。這個字段用于在替換指令被應用時檢查CPU是否支持相應的特性。
(4)orig_len(u8類型):原始指令的長度。表示原始指令所占用的字節(jié)數(shù)。
(5)alt_len(u8類型):新指令的長度。表示替換指令所占用的字節(jié)數(shù)。這個值必須小于等于 orig_len,以確保替換后的指令不會超出原始指令的范圍。

.altinstructions 節(jié)中保存了 struct alt_instr 結構體數(shù)組。數(shù)組中的每一個元素代表了一條替換指令記錄,給出了原始指令的位置、長度和用于修補原始指令的新指令的位置、長度。

這個結構體用于在 Linux 內核的 .altinstructions 節(jié)中定義指令替換規(guī)則。每個結構體實例表示一條指令的替換規(guī)則,其中包含原始指令和替換指令的相關信息。通過使用這些結構體,內核可以在運行時根據(jù)需要進行指令替換,以優(yōu)化性能或解決特定的硬件問題。

# readelf -x 10 vfat.ko.altinstructions”節(jié)的十六進制輸出:NOTE: This section has relocations against it, but these have NOT been applied to this dump.0x00000000 00000000 00000000 05000c0c 00000000 ................0x00000010 00000000 05000c0c 00000000 00000000 ................0x00000020 05000c0c 00000000 00000000 05000c0c ................0x00000030 00000000 00000000 05000c0c 00000000 ................0x00000040 00000000 05000c0c 00000000 00000000 ................0x00000050 05000c0c 00000000 00000000 05000c0c ................0x00000060 00000000 00000000 05000c0c 00000000 ................0x00000070 00000000 05000c0c                   ........
struct alt_instr {s32 orig_offset;	/* offset to original instruction */s32 alt_offset;		/* offset to replacement instruction */u16 cpufeature;		/* cpufeature bit set for replacement */u8  orig_len;		/* size of original instruction(s) */u8  alt_len;		/* size of new instruction(s), <= orig_len */
};
sizeof(struct alt_instr) = 12

“.altinstructions”節(jié) 都是存放struct alt_instr結構體數(shù)據(jù)

因此這個節(jié)存放了 10 個struct alt_instr結構體。

1.2 .rela.altinstructions

.rela.altinstructions節(jié)是一個重定位節(jié),用于存儲.altinstructions節(jié)中數(shù)據(jù)結構的重定位信息。

在Linux ELF(Executable and Linkable Format)文件中,重定位節(jié)(Relocation Section)是用于存儲鏈接器在鏈接過程中需要進行地址修正的信息。重定位節(jié)包含了需要修改的符號引用和相關的重定位類型。

重定位節(jié)中都是未定義的符號,即不是本模塊定義的符號,因此這些符號的地址在內核模塊加載時需要進行重新處理。

重定位節(jié)的名稱通常以 “.rel” 或 “.rela” 開頭,后面跟隨符號表中相關的節(jié)名稱。例如,“.rel.text” 表示與代碼段(.text)相關的重定位信息。

在鏈接過程中,鏈接器會根據(jù)符號引用和重定位類型,將重定位節(jié)中的每個重定位項應用于對應的位置,修正地址或符號引用。

重定位節(jié)的結構和格式可以因不同的體系結構和文件格式而有所不同,但通常包含以下信息:
(1)Offset(偏移量):指定需要修正的位置在節(jié)中的偏移量。
(2)Symbol Index(符號索引):指定需要修正的符號引用在符號表中的索引。
(3)Type(類型):指定重定位的類型,如絕對重定位、PC相對重定位等。
(4)Addend(增量):一些重定位類型需要額外的增量值,用于計算最終的修正值。

/* Relocation table entry with addend (in section of type SHT_RELA).  */typedef struct
{Elf64_Addr	r_offset;		/* Address */Elf64_Xword	r_info;			/* Relocation type and symbol index */Elf64_Sxword	r_addend;		/* Addend */
} Elf64_Rela;
sizeof(Elf64_Rela) = 24
# readelf -x 11 vfat.ko.rela.altinstructions”節(jié)的十六進制輸出:0x00000000 00000000 00000000 05010000 02000000 ................0x00000010 e0040000 00000000 04000000 00000000 ................0x00000020 05010000 02000000 28210000 00000000 ........(!......0x00000030 0c000000 00000000 05010000 02000000 ................0x00000040 5c070000 00000000 10000000 00000000 \...............0x00000050 05010000 02000000 34210000 00000000 ........4!......0x00000060 18000000 00000000 05010000 02000000 ................0x00000070 54080000 00000000 1c000000 00000000 T...............0x00000080 05010000 02000000 40210000 00000000 ........@!......0x00000090 24000000 00000000 05010000 02000000 $...............0x000000a0 24090000 00000000 28000000 00000000 $.......(.......0x000000b0 05010000 02000000 4c210000 00000000 ........L!......0x000000c0 30000000 00000000 05010000 02000000 0...............0x000000d0 f41a0000 00000000 34000000 00000000 ........4.......0x000000e0 05010000 02000000 58210000 00000000 ........X!......0x000000f0 3c000000 00000000 05010000 02000000 <...............0x00000100 b41c0000 00000000 40000000 00000000 ........@.......0x00000110 05010000 02000000 64210000 00000000 ........d!......0x00000120 48000000 00000000 05010000 02000000 H...............0x00000130 e01e0000 00000000 4c000000 00000000 ........L.......0x00000140 05010000 02000000 70210000 00000000 ........p!......0x00000150 54000000 00000000 05010000 02000000 T...............0x00000160 341f0000 00000000 58000000 00000000 4.......X.......0x00000170 05010000 02000000 7c210000 00000000 ........|!......0x00000180 60000000 00000000 05010000 02000000 `...............0x00000190 28200000 00000000 64000000 00000000 ( ......d.......0x000001a0 05010000 02000000 88210000 00000000 .........!......0x000001b0 6c000000 00000000 05010000 02000000 l...............0x000001c0 84200000 00000000 70000000 00000000 . ......p.......0x000001d0 05010000 02000000 94210000 00000000 .........!......

計算得到該重定位節(jié)中有20個Elf64_Rela結構體數(shù)據(jù)。

# readelf -r vfat.ko | grep -A 25 .rela.altinstructions
重定位節(jié) '.rela.altinstructions' at offset 0x37e0 contains 20 entries:偏移量          信息           類型           符號值        符號名稱 + 加數(shù)
000000000000  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 4e0
000000000004  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2128
00000000000c  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 75c
000000000010  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2134
000000000018  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 854
00000000001c  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2140
000000000024  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 924
000000000028  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 214c
000000000030  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 1af4
000000000034  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2158
00000000003c  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 1cb4
000000000040  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2164
000000000048  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 1ee0
00000000004c  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2170
000000000054  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 1f34
000000000058  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 217c
000000000060  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2028
000000000064  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2188
00000000006c  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2084
000000000070  000200000105 R_AARCH64_PREL32  0000000000000000 .text + 2194

在這里插入圖片描述

其中.text + 4e0、.text + 75、.text + 854等都是對應的 BL 函數(shù)跳轉指令:
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
這里的__ll_sc___cmpxchg_case_acq_4函數(shù)和__ll_sc___cmpxchg_case_mb_8不屬于vfat.ko內核模塊中的函數(shù),因此需要重定位來獲取對應函數(shù)的位置。

# cat /proc/kallsyms | grep '\<__ll_sc___cmpxchg_case_acq_4\>'
ffff3c3a3fa01bf0 T __ll_sc___cmpxchg_case_acq_4
# cat /proc/kallsyms | grep '\<__ll_sc___cmpxchg_case_mb_8\>'
ffff3c3a3fa01d28 T __ll_sc___cmpxchg_case_mb_8

可以看到這兩個函數(shù)都屬于內核鏡像中定義的函數(shù)。

aarch64一條指令四個字節(jié),bl是函數(shù)調用指令,比如一個內核模塊調用內核鏡像或者其他模塊的函數(shù):
vfat.ko內核模塊中函數(shù)調用內核的函數(shù)kmem_cache_alloc_trace:

94000000        bl      0 <kmem_cache_alloc_trace>
941001 0100

在這里插入圖片描述

上面提到.altinstructions有10 個struct alt_instr結構體,.rela.altinstructions節(jié)中有20個Elf64_Rela結構體數(shù)據(jù)。

其中.text + 2128、.text + 2134、.text + 2140等對應的指令如下所示:
在這里插入圖片描述
在這里插入圖片描述
這10個重定位項都是在 .text 代碼段的末尾,對應的指令都是:

MOV             X30, X1

對于 x30 寄存器:

在ARM64體系結構中,寄存器 x30 是通用寄存器之一,也稱為"General Purpose Register"。ARM64體系結構共有31個通用寄存器,編號從x0到x30。

寄存器 x30 在ARM64體系結構中有一個特殊的角色,它被稱為"鏈接寄存器"(Link Register),也經(jīng)常以 “l(fā)r” 的縮寫表示。鏈接寄存器用于存儲函數(shù)的返回地址,在函數(shù)調用過程中起到重要的作用。

當一個函數(shù)被調用時,當前函數(shù)的返回地址(即調用該函數(shù)的指令的下一條指令的地址)會被保存在鏈接寄存器 x30 中。函數(shù)執(zhí)行完畢后,通過將鏈接寄存器中的返回地址裝載到程序計數(shù)器(PC)中,控制流程可以返回到調用函數(shù)的位置。

鏈接寄存器 x30 還可以在函數(shù)中用作通用寄存器,存儲臨時數(shù)據(jù)、地址計算和數(shù)據(jù)傳輸?shù)?。但需要注意的?#xff0c;一旦在函數(shù)中使用鏈接寄存器存儲其他數(shù)據(jù),必須在函數(shù)返回之前將其恢復為正確的返回地址,以確保函數(shù)返回到正確的位置。

因此我們可以知道 .altinstructions 節(jié)中有10 個struct alt_instr結構體,也就是10處指令要替換,且都是BL函數(shù)調用替換,因此會有相應的 RET 函數(shù)返回,因此.rela.altinstructions節(jié)中有20個Elf64_Rela結構體數(shù)據(jù)。每一個 .altinstructions 節(jié)中的struct alt_instr結構體對應一個 BL和一個RET。

二、內核模塊重定位源碼分析

// linux-4.19.90/kernel/module.cSYSCALL_DEFINE3(init_module......)-->load_module()-->post_relocation()/* Arch-specific module finalizing. */-->module_finalize()

其中module_finalize是一個與體系架構有關的函數(shù),這里我們主要關注 aach64位架構:

typedef struct elf64_shdr {Elf64_Word sh_name;		/* Section name, index in string tbl */Elf64_Word sh_type;		/* Type of section */Elf64_Xword sh_flags;		/* Miscellaneous section attributes */Elf64_Addr sh_addr;		/* Section virtual addr at execution */Elf64_Off sh_offset;		/* Section file offset */Elf64_Xword sh_size;		/* Size of section in bytes */Elf64_Word sh_link;		/* Index of another section */Elf64_Word sh_info;		/* Additional section information */Elf64_Xword sh_addralign;	/* Section alignment */Elf64_Xword sh_entsize;	/* Entry size if section holds table */
} Elf64_Shdr;
// linux-4.19.90/arch/arm64/kernel/module.cint module_finalize(const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs,struct module *me)
{const Elf_Shdr *s, *se;const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {if (strcmp(".altinstructions", secstrs + s->sh_name) == 0)apply_alternatives_module((void *)s->sh_addr, s->sh_size);}return 0;
}
// linux-4.19.90/arch/arm64/kernel/alternative.cvoid apply_alternatives_module(void *start, size_t length)
{struct alt_region region = {.begin	= start,.end	= start + length,};__apply_alternatives(&region, true);
}
struct alt_instr {s32 orig_offset;	/* offset to original instruction */s32 alt_offset;		/* offset to replacement instruction */u16 cpufeature;		/* cpufeature bit set for replacement */u8  orig_len;		/* size of original instruction(s) */u8  alt_len;		/* size of new instruction(s), <= orig_len */
};struct alt_region {struct alt_instr *begin;struct alt_instr *end;
};typedef void (*alternative_cb_t)(struct alt_instr *alt,__le32 *origptr, __le32 *updptr, int nr_inst);static void __apply_alternatives(void *alt_region, bool is_module)
{struct alt_instr *alt;struct alt_region *region = alt_region;__le32 *origptr, *updptr;alternative_cb_t alt_cb;for (alt = region->begin; alt < region->end; alt++) {int nr_inst;/* Use ARM64_CB_PATCH as an unconditional patch */if (alt->cpufeature < ARM64_CB_PATCH &&!cpus_have_cap(alt->cpufeature))continue;if (alt->cpufeature == ARM64_CB_PATCH)BUG_ON(alt->alt_len != 0);elseBUG_ON(alt->alt_len != alt->orig_len);pr_info_once("patching kernel code\n");origptr = ALT_ORIG_PTR(alt);updptr = is_module ? origptr : lm_alias(origptr);nr_inst = alt->orig_len / AARCH64_INSN_SIZE;if (alt->cpufeature < ARM64_CB_PATCH)alt_cb = patch_alternative;elsealt_cb  = ALT_REPL_PTR(alt);alt_cb(alt, origptr, updptr, nr_inst);......}......}

函數(shù)的參數(shù) alt_region 是一個指向替代指令區(qū)域的指針,is_module 是一個布爾值,表示是否為模塊代碼,這里傳入的是 true ,表示是模塊代碼。

函數(shù)通過循環(huán)遍歷 alt_region (.altinstructions)中的每個替代指令,每個替代指令存儲在結構體 alt_instr 中,包含原始指令和替代指令的信息。

函數(shù)首先檢查替代指令的 cpufeature 字段。如果該字段小于 ARM64_CB_PATCH 并且當前 CPU 不支持該特性,則跳過該替代指令。

如果替代指令的 cpufeature 等于 ARM64_CB_PATCH,則需要確保 alt_len(替代指令長度)為零。否則,需要確保 alt_len 和 orig_len(原始指令長度)相等。如果上述條件不滿足,則會觸發(fā)錯誤(使用 BUG_ON)。

在確定替代指令有效后,函數(shù)使用 pr_info_once 記錄一條消息,指示正在對內核代碼進行修補。

根據(jù) is_module 的值,函數(shù)設置 origptr 和 updptr 變量。如果是模塊代碼,則 origptr 指向原始指令,否則通過 lm_alias 函數(shù)獲取一個別名。這里是模塊代碼,origptr 指向原始指令。

通過將替代指令的長度 alt->orig_len 除以 ARM64 指令的大小 AARCH64_INSN_SIZE,計算出指令的數(shù)量 nr_inst。

/* A64 instructions are always 32 bits. */
#define	AARCH64_INSN_SIZE		4

根據(jù)替代指令的 cpufeature 值,選擇相應的回調函數(shù) alt_cb。如果 cpufeature 小于 ARM64_CB_PATCH,則選擇 patch_alternative 函數(shù)作為回調函數(shù);否則使用 ALT_REPL_PTR(alt) 來確定回調函數(shù)。

調用回調函數(shù) alt_cb,并將 alt、origptr、updptr 和 nr_inst 作為參數(shù),來應用替代指令。

這段代碼目的是應用替代指令(alternative instructions),用新的指令替換原始指令,根據(jù)替代指令的條件和特性選擇性地應用替代。它通過調用相應的回調函數(shù)來執(zhí)行替代指令操作。從而修復或改進內核的行為。

參考資料

Linux 4.19.90

http://www.risenshineclean.com/news/27148.html

相關文章:

  • 網(wǎng)站被收錄要怎么做電腦培訓網(wǎng)上課程
  • 網(wǎng)站建設叁金手指花總1百度河南代理商
  • 建設摩托車站長seo查詢工具
  • 弱電網(wǎng)站源碼小視頻網(wǎng)站哪個可以推廣
  • 黃村網(wǎng)站建設費用線上渠道推廣有哪些方式
  • 網(wǎng)站登錄頁模板微商營銷技巧
  • dedecms 如何關閉網(wǎng)站搜狗站長
  • 微信公眾號h5商城網(wǎng)站開發(fā)網(wǎng)站建設及網(wǎng)站推廣
  • 惡意網(wǎng)站是怎么實現(xiàn)的seo優(yōu)化員
  • 網(wǎng)站維護主要是做哪些百度教育app
  • 網(wǎng)站建設基本流程圖片網(wǎng)站頁面分析作業(yè)
  • python網(wǎng)站開發(fā)招聘崇左網(wǎng)站建設
  • 個體戶可以做網(wǎng)站建設線上推廣工作內容
  • 公司網(wǎng)站建設與維護能讓網(wǎng)絡非常流暢的軟件
  • 山西住房建設廳網(wǎng)站福建網(wǎng)絡seo關鍵詞優(yōu)化教程
  • 做網(wǎng)站的產(chǎn)品圖片搜索排行榜
  • 怎么做詐騙網(wǎng)站嗎青島seo網(wǎng)站排名
  • 邦策網(wǎng)站建設免費站長統(tǒng)計工具
  • 做品牌的人常用的網(wǎng)站百度查詢
  • 山東做網(wǎng)站的軟文廣告范例大全
  • 做業(yè)務網(wǎng)站友鏈交易交易平臺
  • 建設微信商城網(wǎng)站寧波seo搜索引擎優(yōu)化公司
  • 張店做網(wǎng)站公司培訓機構排名全國十大教育機構排名
  • 抖音logo在線設計生成器免費石景山區(qū)百科seo
  • 武威百度做網(wǎng)站多少錢百度怎么轉人工客服
  • 免費空間凡科連云港seo
  • 品質好的深圳裝修優(yōu)化營商環(huán)境發(fā)言稿
  • 做網(wǎng)站需要一些什么東西seo關鍵詞優(yōu)化推廣外包
  • 如何用c 做網(wǎng)站背景外貿公司一般怎么找客戶
  • wordpress 音頻seo網(wǎng)站分析報告