
kernel基础
文章平均质量分 92
淡泊的猪
对的事情,坚持做
展开
-
如何启用linux内核异常自动重启机制
内核出现异常,有这么几种情况:1.在内核代码执行路径上会主动的去检查一些执行环境,如果不满足触发panic;2.某段代码一直占着cpu,其他进程都得不到执行,这又分为两种情况:a)某段代码禁止了内核抢占,并且进入了死循环,这种情况下时钟中断可以响应,但是因为禁止了内核抢占,中断处理函数结束后仍然占着cpu,这就是所谓的soft lockupb)某段代码禁止了中断,并且进入了死循环,这种情况...原创 2020-01-29 18:49:36 · 4739 阅读 · 0 评论 -
linux 时间子系统之基本概念(一)
time相关的用户空间api:clock_t clock(void);The clock() function returns an approximation of processor time used by the program.The value returned is the CPU time used so far as a clock_t; to get the numb...原创 2020-01-18 10:17:17 · 628 阅读 · 0 评论 -
linux input输入子系统源码分析
总体软件架构先来个整体的软件层次架构图:从上图输入子系统的框架图,可以看出,输入子系统由Input driver(驱动层)、Input core(输入子系统核心)、Event handler(事件处理层)三部分组成。一个输入事件,如鼠标移动、键盘按下等通过Input driver -> Input core -> Event handler -> userspace的顺序...原创 2020-01-17 19:51:14 · 667 阅读 · 0 评论 -
linux中对/dev/console和/dev/tty的理解
/dev/console是什么如果系统中存在多个tty设备,想象一下,这时内核启动的log应该打印在哪里,这时内核会从tty中选择一个最合适的作为console,当然内核启动参数中也可以明确的去指定那个tty作为内核的console,/dev/console主要是暴露给用户空间使用的,主要用于系统管理员在改终端上登陆用。/dev/tty又是什么The file /dev/tty is a c...原创 2020-01-16 18:30:34 · 4844 阅读 · 0 评论 -
linux 中断与异常---mips基础(一)
MIPS体系结构采用的是精确异常处理模式这是什么意思呢?下面来看从“See MIPS Run”一书中的摘录:“In a precise-exception CPU, on anyexception we get pointed at one instruction(the exception victim). All instructions preceding theexception vict...原创 2018-03-13 09:58:57 · 7231 阅读 · 0 评论 -
Linux中SysRq的使用
1.简介SysRq 键可以直接从内核输出信息。只要不是禁止中断状态,即使死机时也可以获取信息。SysRq 键在确认内核运行、调查内核死机原因等各种情况下都非常有效。2.需要kernel打开CONFIG_MAGIC_SYSRQ选项支持3.内核的源代码位置:drivers/tty/sysrq.c4.kernel中的实现static int __init sysrq_init(void){sys...原创 2018-04-19 16:56:21 · 4228 阅读 · 0 评论 -
linux 中断与异常---基本概念(二)
可编程中断控制器PIC +-------------+ | cpu | +-------------+ ↑ INTR | ↓+------------------------+| PIC |+--------------------...原创 2018-04-24 16:42:11 · 463 阅读 · 0 评论 -
linux中的asm-offsets.h和bounds.h
asm-offsets.h和bounds.h在内核中主要用于定义常量。asm-offsets.h用于定义特定成员相对结构体的偏移量,bounds.h用于定义资源的最大个数。顶层内核目录下kbuild文件定义了它们的生成规则:## Kbuild for top-level directory of the kernel# This file takes care of the following:#...原创 2018-05-02 11:21:00 · 1739 阅读 · 0 评论 -
linux kernel启动代码分析
本文分析基于mips架构,linux-3.3.8一.kernel入口首先通过链接脚本找到kernel的入口1.内核是压缩过的/arch/mips/boot/compressed/Makefile ,将$(obj)/head.o $(obj)/decompress.o $(obj)/dbg.o $(obj)/piggy.o 链接为vmlinuz,链接脚本为ld.scriptvmlinuz: $(...原创 2018-05-02 14:40:56 · 2294 阅读 · 0 评论 -
What does an idle CPU do?
原文出处:https://manybutfinite.com/post/what-does-an-idle-cpu-do/index.htmlIn the last post I said the fundamental axiom of OS behavior is that at any given time, exactly one and only one task is active o...转载 2018-05-02 15:06:57 · 354 阅读 · 0 评论 -
linux 内存管理---分页系统之页表初始化(四)
arch_mem_init()在调用bootmem_init()函数完成bootmem的初始化之后调用paging_init()进行分页系统的初始化。分页系统就是将物理内存按页为单位进行描述,管理和映射。注意这里是mips cpu,不同的arch cpu初始化顺序可能不同, linux-3.3.8[arch/mips/mm/init.c]void __init paging_init(void){...原创 2018-05-03 18:15:20 · 864 阅读 · 0 评论 -
linux SYSCTL的原理分析
1./proc/sys support需要打开内核选项CONFIG_PROC_SYSCTLfs/proc/root.cvoid __init proc_root_init(void){ ... proc_sys_init(); //注册/proc/sys}#ifdef CONFIG_PROC_SYSCTLextern int proc_sys_init(void);ex...原创 2018-04-19 16:18:18 · 2636 阅读 · 0 评论 -
linux 内存管理---页框回收(十)
为什么需要页回收?linux的设计哲学之一:尽可能多的使用内存,比如尽可能的多使用memory cache,disk cache,因为这在系统负载比较小时,能够提升系统性能,但是随着cache越来越多,这迟早会消耗完所有可用的内存,所以只有在系统内存不足时才会触发页回收。触发页回收的时机内存紧缺grow_buffers(),alloc_page_buffers(),_ _alloc_pages()...原创 2018-04-19 15:49:44 · 1321 阅读 · 0 评论 -
linux应用中减少内存碎片
1.使用mem pool一次性malloc一块大的内存,以后内存分配都从这块大内存中去分配,如何去管理这块内存就是mem pool该做的事情,其实它的原理类似于malloc的原理,只是这块大内存是固定的,不像malloc可以系统中去申请,所以管理上要比malloc简单,mem pool的实现有很多种,这里随便列举一两个:https://github.com/silentbicycle/mpoolh...原创 2018-04-03 17:55:13 · 1057 阅读 · 1 评论 -
Linux 内核进程管理之进程PID
原文地址:http://www.cnblogs.com/hazir/p/linux_kernel_pid.htmlLinux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构,Linux 内核所有涉及到进程和程序的所有算法都是围绕该数据结构建立的,是内核中最重要的数据结构之一。该数据结构在内核文件 include/linux/sched.h 中定义,在Linux 3.8 ...转载 2018-04-03 17:46:49 · 1617 阅读 · 0 评论 -
linux一切设备皆文件的实现(二)
首先struct device是设备模型中的概念,这个结构体中保存的是具体板子上设备的信息,比如基寄存器地址,寄存器范围,终端号等等,目的就是使驱动与具体的板子硬件连接剥离,使驱动更具有通用性和移植性,这样不同板子上驱动都不需要改变,struct device注册设备放在架构相关的代码中硬编码或者使用device tree。 struct cdev是一个用户接口相关的结构体,linux上一...原创 2018-04-03 17:18:21 · 1451 阅读 · 0 评论 -
linux一切设备皆文件的实现(一)
fs/inode.cvoid init_special_inode(struct inode *inode, umode_t mode, dev_t rdev){ inode->i_mode = mode; if (S_ISCHR(mode)) { inode->i_fop = &def_chr_fops; inode->i_rde...原创 2018-04-03 17:16:59 · 881 阅读 · 0 评论 -
linux SMP启动代码分析
If one replicates an entire CPU to execute a second thread, then the technique is known as multi-processing.If one replicates only a portion of a CPU to execute a second thread, then the technique is ...原创 2018-03-13 10:19:40 · 2035 阅读 · 0 评论 -
linux cache管理---mips基础(一)
一 Bmips cache 概述magnum/basemodules/mma/bmma.hThere are potentially three CPU caches you should be aware of: 1) L1 cache - this is a write-back cache 2) L2 cache - this is a write-back c...原创 2018-03-13 10:14:56 · 1876 阅读 · 0 评论 -
linux 内存管理---mips基础(一)
本文分析的是4K mips的内存管理,其他mips型号可能会有所区别The MMU in a 4K processor core will translate any virtual address to a physical address before a request is sent to thecache controllers for tag comparison or to the ...原创 2018-03-13 10:07:59 · 3458 阅读 · 0 评论 -
linux 内存管理---分页系统之物理页管理(五)
Linux把物理内存划分为三个层次来管理层次描述节点(Node)为了支持NUMA模型而引入的概念,node的划分主要根据访问速度,一个node中所有内存对于某个cpu来说访问速度相同,对于SMP来说,每个cpu都有本地内存,本地cpu访问速度较快,但是其他cpu却访问较慢,每个node用pg_data_t结构体来描述管理区(Zone)每个节点node根据用途不同被划分为多个zone, 用于表示不同...原创 2018-05-03 18:23:43 · 638 阅读 · 0 评论 -
linux 内存管理---伙伴系统(六)
start_kernel()调用mm_init()进行伙伴系统的初始化。static void __init mm_init(void){ /* * page_cgroup requires countinous pages as memmap * and it's bigger than MAX_ORDER unless SPARSEMEM. */ page_...原创 2018-05-04 14:20:02 · 971 阅读 · 0 评论 -
linux 中断与异常---源码分析(三)
概述冷启动、热重启、非屏蔽中断异常入口始终为0xBFC00000,任何其他寄存器都改变不了一些比较频繁发生的异常为了效率的考虑有专用的异常入口,如TLB refill除此之外所有的其他异常共用一个异常入口,称为通用异常入口,专用异常入口和通用异常入口可通过寄存器改变通用异常发生后, CPU 硬件设置 CAUSE寄存器的 ExcCode位, 就跳转到通用异常入口处, ExcCode 位段用来描述通用...原创 2018-04-26 17:43:27 · 1785 阅读 · 0 评论 -
LSM框架介绍
LSM是Linux Security Module的简写在内核需要安全检查的地方安插了很多钩子://所有的钩子都定义在security/security.c文件中int security_file_open(struct file *file, const struct cred *cred) { int ret; ret = call_int_hook(...原创 2019-04-18 11:17:44 · 6875 阅读 · 1 评论 -
USB OTG学习笔记
本文是查阅相关资料学习了半天的心得,记录下来防止忘记,不正确之处望指正。OTG 是On-The-Go的缩写,是为了USB设备身份主从互换而引入的。为了支持OTG,改动部分包括如下:1.接口修改,增加了另外一条ID线2.增加了额外的协议,ADP,SRP,HNP一个既支持主又支持从的USB设备,软件协议栈应包含如下部分:host controler driverhost side de...原创 2019-03-21 14:58:07 · 484 阅读 · 0 评论 -
linux spi驱动分析
spi的驱动框架主要分为:cpu<—platform bus—>spi master 端 <-----spi bus—>spi slave 端spi master 指的是spi控制器,spi master device是平台设备,spi master driver是平台驱动,它们都挂载在平台总线上spi slave指的是spi设备,spi device和spi dri...原创 2019-03-20 18:40:47 · 1543 阅读 · 0 评论 -
linux平台设备和平台驱动
由module_init宏指定模块的初始化入口下面来看看模块的初始化static struct platform_driver efuse_driver = { .probe = efuse_probe, ...原创 2019-03-16 17:53:07 · 643 阅读 · 0 评论 -
Linux audit详解
什么是auditThe Linux Audit Subsystem is a system to Collect information regarding events occurring on the system(s) ,Kernel events (syscall events), User events (audit-enabled programs)syslog记录的信息有限,主要...原创 2019-02-14 13:29:31 · 33563 阅读 · 1 评论 -
linux kernel keys笔记
keys在内核中的表示为: struct key { atomic_t usage; /* number of references */ key_serial_t serial; /* key serial number */ union { struct list_head graveya...原创 2019-02-14 10:24:17 · 1643 阅读 · 0 评论 -
Linux signal捕获
signal的原理这里不打算多讲,这里主要讲一下应用man 7 signal,可以看到一些关于signal的介绍:每个信号都对应着一个action,默认的有:Term, Ign,core,Stop,Cont, 文档上明确的写着是以进程为修改单位的,所有的线程的action都相同:The signal disposition is a per-process attribute: in...原创 2019-01-14 09:36:33 · 1716 阅读 · 0 评论 -
升级ubifs以支持 fscrypt
The Linux kernel configuration item CONFIG_UBIFS_FS_ENCRYPTION:prompt: UBIFS Encryption type: bool depends on: CONFIG_UBIFS_FS_XATTR && CONFIG_BLOCK defined in fs/ubifs/Kconfig found in ...原创 2019-01-17 20:30:35 · 2413 阅读 · 2 评论 -
ubi & ubifs学习笔记
drivers/mtd/ubi/ubi-media.h EC: Erase Count,记录块的擦除次数struct ubi_ec_hdr { __be32 magic;__u8 version;__u8 padding1[3];__be64 ec; /* Warning: the current limit is 31-bit anyway! */__be32 vid_hdr...原创 2019-01-11 18:39:54 · 24995 阅读 · 0 评论 -
自己实现的简单dmalloc
写了个简单的dlmalloc的实现,这个自然是无法实用的,需要不断完善,通过简单的实例重点在于说明dlmalloc的工作原理。 struct tag_node{ void * addr; int size; char * function; char * line; int count; struct tag_node * next;...原创 2018-07-16 09:25:31 · 1115 阅读 · 0 评论 -
gcc内嵌汇编
6.43 How to Use Inline Assembly Language in C CodeThe asm keyword allows you to embed assembler instructions within C code. GCC provides two forms of inline asm statements. A basic asm statement is on...转载 2018-05-24 15:00:04 · 382 阅读 · 0 评论 -
内存泄露调试经验
使用mallinfo确定是否有内存泄露:static struct mallinfo mi1,mi2;static struct timeval oldTime, currentTime;#define MALLOC_STAT_TIME (60)void sk_factory_test_malloc_stat(void){ gettimeofday (&currentTime, ...原创 2018-05-16 09:34:46 · 5943 阅读 · 0 评论 -
linux 内存管理---bootmem(三)
为什么要使用bootmem分配器,内存管理不是有buddy系统和slab分配器吗?由于在系统初始化的时候需要执行一些内存管理,内存分配的任务,这个时候buddy系统,slab分配器等并没有被初始化好,此时就引入了一种内存管理器bootmem分配器在系统初始化的时候进行内存管理与分配,当buddy系统和slab分配器初始化好后,在mem_init()中对bootmem分配器进行释放,内存管理与分配由...原创 2018-04-28 15:16:29 · 1175 阅读 · 0 评论 -
linux权限检查机制
1.文件权限 static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask) { if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { if (likely(mnt &...原创 2018-11-28 16:01:36 · 2146 阅读 · 0 评论 -
linux 内存管理---物理内存探测(二)
linux内核启动时如何知道系统有多少可用内存呢?我们可以猜猜大概有这么几种方式:1.内核写死;2.通过启动参数传递给内核;3.内核代码自动探测可用内存大小;具体是怎样的呢,下面结合代码具体分析一下。start_kernel()启动后调用setup_arch()对平台相关的代码进行初始化。[arch/mips/kernel/setup.c]void __init setup_arch(char *...原创 2018-04-27 17:55:58 · 1086 阅读 · 0 评论 -
uclibc中dlmalloc实现分析
Doug Lea malloc是一个用C语言实现的非常流行的内存分配器,由纽约州立大学Oswego分校计算机系教授Doug Lea于1987年撰写,许多人将其称为Doug Lea的malloc,或者简称dlmalloc。由于具备高效且占用空间较小等特点,dlmalloc被广泛使用,用Doug Lea自己的话说,就是“它在一些linux版本里面作为默认的malloc被使用,被编译到一些公共的软件包里...原创 2018-03-13 09:28:33 · 1134 阅读 · 0 评论 -
关于Linux线程的线程栈以及TLS
原文出处:http://blog.youkuaiyun.com/dog250/article/details/7704898[-]说明一线程栈二线程本地存储-TLS注意关于TLS另外需要说的说明:a.本文描述Linux NPTL的线程栈简要实现以及线程本地存储的原理,实验环境中Linux内核版本为2.6.32,glibc版本是2.12.1,Linux发行版为转载 2016-12-08 19:52:24 · 1026 阅读 · 0 评论