1. 原因
static void rk3288_clk_init(struct device_node *np) { rk3288_cru_base = of_iomap(np, 0);, r% N8 h+ x5 F; n/ V% T( r9 W0 ? [...] } 2. ioremap() 实验 实验环境:
实验代码: #include <linux/init.h> ~% p, D; ]$ y) @: D#include <linux/module.h>& C& v6 P) g( {* ^% h5 p, o. j #include <linux/sched.h>9 f8 v8 B8 W( A" K+ q% ~: ?# K5 ^% l #include <asm/io.h>! g) R- t( p+ [/ c $ I& a; q& F1 L( F% o/ }/ V #define USE_IOREMAP % U' Z* L0 Q& x) { #define H3_GPIO_BASE (0x01C20800) static volatile unsigned long *gpio_regs = NULL;! g: m5 r. Z# F0 @' J P ( }! _# U* Q4 A" x" }) k static int __init ioremap_mod_init(void) {9 d/ |2 m' e' e# K7 i: j( d: l int i = 0; printk(KERN_INFO "ioremap_mod init\n");4 d' i" ~$ J$ G ' D! f, k9 m8 j, o$ S8 M #ifdef USE_IOREMAP0 J3 R- F f! R, [ gpio_regs = (volatile unsigned long *)ioremap(H3_GPIO_BASE, 1024);1 f, E4 C' j: @& l5 K #else7 F5 G( i# d/ x4 d0 _ gpio_regs = (volatile unsigned long *)H3_GPIO_BASE; #endif ) l. c- n! L5 {" y- L& W for (i=0; i<3; i++) printk(KERN_INFO "reg[%d] = %lx\n", i, gpio_regs); return 0;! Q, H; s1 {3 v) f0 @. V9 ~ }- w# o1 a6 V, f6 { module_init(ioremap_mod_init); static void __exit ioremap_mod_exit(void) { printk(KERN_INFO "ioremap_mod exit\n ");! L( k) i# W! E4 ~8 G #ifdef USE_IOREMAP iounmap(gpio_regs);# u3 ?, i) D5 Y$ G. i( M #endif } module_exit(ioremap_mod_exit);1 k( u8 y! @4 g" Y# |) ` MODULE_AUTHOR("es-hacker");* g b* G# r/ o! {% m MODULE_LICENSE("GPL v2");* x( i. X# @7 J2 N |7 e5 X1 Q% d! e 实验结果: 使用了 ioremap() $ insmod ioremap* c- ?2 Z6 w* m" Z# Oioremap_mod init, _/ p+ Z; X+ m) r) K0 c9 S% x reg[0] = 71227722 J6 j3 {$ d# P$ M5 Z K5 ` K reg[1] = 33322177 g D& {) m( f4 T' } reg[2] = 773373$ q; z0 a: j, @, ? 未使用 ioremap(): $ insmod ioremap_mod.koUnable to handle kernel paging request at virtual address 01c208006 C: G5 l( s- a6 | pgd = c9ece7c0* z9 T9 n# V+ l9 g5 j: a [01c20800] *pgd=6ddd7003, *pmd=00000000 Internal error: Oops: 206 [#1] SMP ARM2 P6 Y2 A/ F3 }* J1 _+ V+ C) x, [; d CPU: 1 PID: 1253 Comm: insmod Tainted: G O 4.14.111 #116% g% S7 m R9 I' ^$ a Hardware name: sun8i task: ef15d140 task.stack: edc50000 PC is at ioremap_mod_init+0x3c/0x1000 [ioremap_mod]9 `/ Z! J: P _7 H LR is at ioremap_mod_init+0x14/0x1000 [ioremap_mod] pc : [<bf5d903c>] lr : [<bf5d9014>] psr: 600e0013 sp : edc51df8 ip : 00000007 fp : 118fa95c r10: 00000001 r9 : ee7056c0 r8 : bf5d6048- I7 L9 Z2 k% f, v r7 : bf5d6000 r6 : 00000000 r5 : bf5d6200 r4 : 00000000, z8 a4 F* ^7 W r3 : 01c20800 r2 : 01c20800 r1 : 00000000 r0 : bf5d5048 Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user% c3 m4 [) w4 L( F. k( Q' _ Control: 30c5387d Table: 49ece7c0 DAC: b106c794 Process insmod (pid: 1253, stack limit = 0xedc50210) 0 ^% \. e# S8 Z8 G9 { [<bf5d903c>] (ioremap_mod_init [ioremap_mod]) from [<c0201a70>] (do_one_initcall+0x40/0x16c), s9 h. y0 F; F [<c0201a70>] (do_one_initcall) from [<c02b20c8>] (do_init_module+0x60/0x1f0)5 |; h* E: i$ i' H. m( P [<c02b20c8>] (do_init_module) from [<c02b1214>] (load_module+0x1b48/0x2250)& R- c1 p% V: H, m# E [<c02b1214>] (load_module) from [<c02b1ad8>] (SyS_finit_module+0x8c/0x9c)5 o" U1 D- t5 c# z% O; e$ w [<c02b1ad8>] (SyS_finit_module) from [<c0221f80>] (ret_fast_syscall+0x0/0x4c) Code: e5953000 e3050048 e1a01004 e34b0f5d (e7932104) ---[ end trace 928c64a33a054308 ]---* a3 W3 u* ^% B; h% j% ]5 Z Segmentation fault 3. ioremap() 的实现内幕 ioremap() 的实现内幕会涉及到比较多的内存管理的知识,这里我们抛开代码细节简单了解一下原理就好。
函数调用流程:
总结一下:
|
微信公众号
手机版