自己动手写操作系统二(编写内核Hello World 教程)

linux内核编程(hello world示例程序) linux内核编程(hello world) Linux可加载内核模块是 Linux 内核的最重要创新之一。它们提供了可伸缩的、动态的内核。其它开发者可以不用重新编译整个内核便可以开发内核层的程序,极大方便了驱动程序等的开发速度。 本系列文章主要是记录个人从hello world开始,通过学习linux内核基本编程操作,再对ipvs负载均衡器源码进行初步学习分析;最后,基于netfil 阅读详情

   By EvilBinary 小E 

   本文用到的boot.s 和setup.s 两个文件见本blog MyOs  分类相关文章。

    我们编写start.s 来启动初始化信息,然后进入main.c内核代码,然后显示Evilbinary os字样

//start.s 文件

global _start
extern old_pgdt
extern pgdt
extern gdt
extern pidt
extern setup_gdt
extern setup_idt
extern setup_ldt
extern init_8259a
extern main

section .text
jmp _start	;0x8:0x400

	stack times 128 db 0
_start:
	mov ax,ds
	mov ds,ax
	mov ss,ax
	mov es,ax
	mov fs,ax
	mov gs,ax
	mov eax,stack
	mov esp,eax
	xor eax,eax

	sgdt [old_pgdt]
	call init_8259a
	call setup_gdt
	call setup_idt
	lgdt [pgdt]
	lidt [pidt]
	jmp 0x08:kernel
kernel:
	jmp main
	jmp $
	halt
	
//main.c 文件

#include <type.h>
#include <io.h>
void welcome(){
	clrscr();
	e8 welcome[]={"Welcome to EvilBinary OS."};
	e32 i;
	setxy(18,0);
	for(i=0;i<24;i++){
		e8 c=welcome[i];
		write_char(c,LIGHTGREEN);
	}
	printf("\n");
}

int main()
{	
	welcome();
	for(;;)
	return 0; 
}	
用到的相关函数及头文件

//type.h

#ifndef	_TYPE_H
#define _TYPE_H

typedef char   e8;
typedef short  e16;
typedef int    e32;
typedef double e64;
typedef int  ebool;
typedef unsigned char   u8;
typedef unsigned short  u16;
typedef unsigned int    u32;
typedef double u64;

typedef void (*function_handl)();

#endif


//io.c

void write_char(e8 ch,e8 color){
	e8* pVideo=(e8*)0xb8000; 
	u16 offset;
	outb(0x3d4,14);
	offset=inb(0x3d5)<<8;
	outb(0x3d4,15);
	offset|=inb(0x3d5);
	pVideo+=offset*2;
	*pVideo=ch;
	pVideo++;
	*pVideo= color;
	offset++;
	outb(0x3d5,(u8)offset);
	outb(0x3d4,14);
	outb(0x3d5,(u8)(offset>>8));
}
//kernellib.s

;extern outb(u16 port,u8 data)
_outb:	
	push ebp
	mov ebp,esp
	mov al,[esp+12]
	mov dx,[esp+8]
	out dx,al
	mov esp,ebp
	pop ebp
	ret 

;u8 inb(u16 port)
_inb:

	push ebp
	mov ebp,esp
	mov dx,[esp+8]
	in al,dx
	mov esp,ebp
	pop ebp

	ret

;extern outw(u16 port,u16 data)
_outw:	
	push ebp
	mov ebp,esp
	mov ax,[esp+12]
	mov dx,[esp+8]
	out dx,ax
	mov esp,ebp
	pop ebp
	ret 

;u16 inw(u16 port)
_inw:

	push ebp
	mov ebp,esp
	mov dx,[esp+8]
	in ax,dx
	mov esp,ebp
	pop ebp
	ret
//结果







Linux:Hello World 模块 前言Linux 系统为应用程序提供了功能强大且容易扩展的 API,但在某些情况下,这还远远不够。与硬件交互或进行需要访问系统中特权信息的操作时,就需要一个内核模块。下面从 Hello World 模块来说明模块的建立与运行。 环境搭建 开始编写Linux内核模块之前,我们首先要准备一些工具。最重要的是,你需要有一台Linux机器,尽管可以使用任何Linux发行版,但本文中,我使用的是Ubuntu 16.04 LTS,如果你使用的其他发行版,可能需要稍微调整安装命令。 建立 1、创建目录 mkdir -p 阅读详情

相关推荐

自己动手写操作系统(于渊著)

自己动手写操作系统 于渊著 pdf格式

《自己动手写操作系统》学习笔记 之 hello world

《自己动手写操作系统》学习笔记  之 hello world 计算机加电启动后,从引导扇区把代码加载到内存的0x07c00位置,然后运行机器码。 这里使用nasm汇编将显示hello world 字符串的代码编译成二进制代码,然后使用dd制作虚拟软盘,通过VirualBox

jingle 的技术博客 732

orange's一个操作系统的实现-【自己动手写操作系统第二版】

本书内容侧重于逐步指导构建一个迷你操作系统,可作为操作系统DIY者参考。 本pdf清晰、完整,带有书签。

自制一个能显示helloworld的最简单OS

《自己动手写操作系统》 org 07c00h mov ax,cs mov ds,ax mov es,ax call DispStr jmp $ DispStr: mov ax,BootMessage mov bp,ax mov cx,16 mov ax,01301h mov bx,000ch mov dl,0 in...

weixin_30404405的博客 149

自己动手写操作系统.pdf

自己动手写操作系统.pdf

自己动手写嵌入式操作系统+教程+pdf

自己动手写嵌入式操作系统+教程+pdf

《自己动手写操作系统》第2版:第一、二章(1)

    上一篇是对基本情况的介绍,现在开始就进入正题了。     本人使用的开发环境如下(只基于linux下的开发,需要windows下开发的同学自行阅读书籍): 宿主操作系统:win10 虚拟机:VMware Workstation 12 Pro linux版本:centos 7(安装centos7时要顺带安装图形界面,而且还不能使用XShell等工具连接你的centos进行开发,要在ce...

asd150127的博客 1137

系统-等保三级-CentOS Linux 7合规基线检查 shell脚本

系统-等保三级-CentOS Linux 7合规基线检查 shell脚本

swindy博客 2407

实战 - 编写一个最简单的 Hello World 内核模块

让读者编写并运行第一个 Linux 内核模块

qq_34954047的博客 1191

通过ssh进行宇树g1机器人进行连接

原因:就是g1机器人输出接口损坏,无法连接外部显示器。前期准备:本文电脑是联想Y7000p(windows)2.进入页面后:3.出现是一个压缩包,解压后:4.安装时:要注意,需要在文件夹中找到这个文件目录,然后在此鼠标右键开启终端进行安装:接着按照提示next下去即可,可以自定义安装路径。

m0_72207463的博客 1282

ESP32基于Arduino环境开发128*160 TFT_LCD屏幕显示照片

ESP32基于Arduino环境开发128*160 TFT_LCD屏幕显示照片

weixin_42854045的博客 2059

汇编语言编写的Hello World

Linux 下用汇编语言编写的代码具有两种不同的形式。第一种是完全的汇编代码,指的是整个程序全部用汇编语言编写。尽管是完全的汇编代码,Linux 平台下的汇编工具也吸收了 C 语言的长处,使得程序员可以使用 #include、#ifdef 等预处理指令,并能够通过宏定义来简化代码。第二种是内嵌的汇编代码,指的是可以嵌入到C语言程序中的汇编代码片段。虽然 ANSI 的 C 语言标准中没有关于内嵌汇编代

后花园 2万+

超详细 | 贝叶斯网络基础——有图有真相

Hello,大家好,我是starz。这是本系列的第二篇《概率基础Ⅱ》,虽说是概率基础,但作为过渡,它开始以图为载体 这节的主要内容有: 贝叶斯网络 三种结构 D-划分 贝叶斯球 马尔科夫毯 应用例子 总结 让我们开始吧 :3 贝叶斯网络 概率图模型可笼统分为两类,一是基于无向图的马尔科夫网络: 二是基于有向图的贝叶斯...

qq_41603411的博客 2万+

深入浅出 Linux 内核模块,写一个内核版的 Hello World

本文介绍了Linux内核模块的开发流程。首先讲解了内核模块的定义及其在扩展内核功能中的作用,对比了用户空间与内核空间的差异。随后通过一个简单的"Hello World"内核模块示例,详细解析了模块初始化函数、退出函数、printk日志级别等核心内容。文章还提供了模块编译所需的Makefile模板,并演示了使用insmod、lsmod、rmmod等命令进行模块加载、查看和卸载的操作步骤。最后强调编写内核模块需要格外谨慎,因为错误可能导致系统崩溃。该教程为后续更复杂的驱动开发奠定了基础。

分享一些嵌入式Linux相关知识 1180

Pspice安装教程:Windows系统完整指南(含环境变量设置)

详细讲解在Windows系统中完成Pspice安装的全过程,涵盖安装步骤与关键的环境变量设置,帮助用户顺利运行仿真工具。适合初学者快速上手pspice安装教程中的核心环节。

weixin_42230607的博客 1174

告别130MB/s瓶颈!用Zynq+NVMe实现1.2GB/s高速存储的PL数据分流实战

本文详细介绍了如何通过Zynq+NVMe实现1.2GB/s高速存储的PL数据分流方案,突破传统Zynq架构130MB/s的瓶颈。文章分享了PL直连NVMe的硬件架构设计、软件栈优化及性能调优实战技巧,帮助开发者实现高速数据流的稳定存储。

weixin_42653691的博客 372

编写一个简单的内核模块程序Hello World!

一、Linux内核模块基本原理Linux 内核模块(LKM)是一些在启动的操作系统内核需要时可以载入内核执行的代码块,不需要时由操作系统卸载。它们扩展了操作系统内核功能却不需要重新编译内核、启动系统。如果没有内核模块,就不得不反复编译生成操作系统的内核镜像来加入新功能,当附加的功能很多时,还会使内核变得臃肿。一个Linux 内核模块主要由以下几个部分组成: (1) 模块加载函数(必须):当通过in

2万+

向华为学习——解读158页 华为敏捷园区网技术解决方案【附全文阅读】

华为敏捷园区网解决方案通过策略随行和体验随身技术,满足移动办公场景下的网络需求。该方案采用敏捷交换机和防火墙自动执行策略配置,实现用户身份认证与业务策略的无缝衔接,且策略配置与IP地址解耦,简化了管理流程。相比传统技术,该方案有效解决了用户分级、QoS保障、无线接入等关键问题,确保用户在移动过程中获得一致的网络体验。

cdfunlove的博客 378

内核编程五:从hello world开始

这是一个简单的 Linux 内核模块(LKM)示例,模块加载时打印 “Hello, world”,卸载时打印 “Goodbye curl world”。通过 module_init 和 module_exit 宏,分别指定了模块加载和卸载时的函数。

zyqash的博客 1251

【定位模块项目实战】正态分布变换(NDT)定位及建图(依赖高精度点云地图和三维激光雷达的定位方案)

系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 TODO:写完再整理 文章目录系列文章目录前言一、正态分布变换(NDT)定位解决的主要问题二、正态分布变换(NDT)定位的核心思想三、NDT算法流程1.多元正态分布概念2.网格化并计算正态分布参数3.变换参数和最大似然四、使用C++实现NDT配准参考资料 前言 认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长! 本文先对依赖高精度地图和激光雷达的定位技术–正态分布变换(NDT)定位做个简单的介绍,具体

qq_35635374的博客 4411

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Linux 内核模块 hello world 程序

Linux 内核模块的结构,一个简单的 hello 示例模块 ...

啸铁 1138

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

linux 内核程序 hello,Linux内核程序编写初试hello world

本文为学习笔记,资料来自网络1 *.c文件的编写$mkdir test 创建实验目录,例如test,名字随意$touch hello.c 创建hello.c文件$vim hello.c 编写hello.c代码,主要完成打印“hello world”功能hello.c 文件内容//hello world driver for linux//包含必要的头文件,主要存在于linux内核源码中#in...

weixin_36461619的博客 285

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

自己动手写hello world内核模块

hello world模块虽小却是五脏俱全,该有的部分都有了。话不多说,直接上手。 1.编译Linux中运行的内核模块,需要指定到当前系统对应的内核中。一般来说,Linux系统的内核模块都会安装到/lib/modules目录下,通过“uname -r”命令可以找到对应的内核版本(我的是/lib/modules/4.13.0-36-generic)。 2.找到内核版本后,进入到内核版本文件夹中,建立...

qq_39748948的博客 456

编写 Hello world 模块驱动

编写 Hello world 模块驱动华清远见-嵌入式领域著名讲师  O(∩_∩)O  牛牛猛点击链接加我好友!http://student.youkuaiyun.com/invite.php?u=111047&c=758a60d66d3a92d1欢迎大家去我优快云博客上踩踩我的个人主页 晚上没事,同学邀我写个模块的实验手册,拿出来和大家分享一下(1)

牛牛猛嵌入式的专栏 2713

linux内核hello world模块编写

linux内核模块编写

生命的紫金颜色 891

编写Linux驱动程序的hello world

0准备条件操作系统:VMware的虚拟机中运行的Debian的9采用的内核:linux4.9 1.下载linux的源代码的Debian或ubuntu的下,使用容易工具下载的Linux源码的CentOS使用yum的苏易于得到安装Linux的-y源安装完成后应当能够在/ usr / src目录中看到如下3个文件或文件夹:DTS @ debian的:〜$ LS的/ usr / src / linux...

时梦的博客 1430
上一篇: blender 快捷
下一篇: java 山寨QQ 实现(一)
EvilBinary_root
博客等级 码龄16年 104粉丝 64原创
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值