Skip to content
Snippets Groups Projects
Commit 08208321 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Vortragsfolien, Beispiele, Tafelbilder und Notizen 29.4.2024

parent bafd86cb
Branches 2014ss
No related tags found
No related merge requests found
Showing
with 382 additions and 0 deletions
obj-m += chardev-5.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
File added
% bs-20240429.pdf - Lecture Slides on Operating Systems
% Copyright (C) 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
\documentclass[10pt,t]{beamer}
\usepackage{pgslides}
\usepackage{tikz}
\title{Treiberentwicklung,\\[\medskipamount]Echtzeit- und Betriebssysteme}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{29.\ April 2024}
\begin{document}
\maketitleframe
%\nosectionnonumber{\inserttitle}
\nosectionnonumber{Treiberentwicklung, Echtzeit- und Betriebssysteme}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\item[\textbf{2}] \textbf{Unix}
\item[\textbf{3}] \textbf{Treiberentwicklung}
\begin{itemize}
\color{medgreen}
\item[3.1] Mikrocontroller
\item[3.2] Betriebssysteme ohne Speicherschutz
\item[3.3] Linux-Kernel-Module
\color{red}
\item[3.4] Die glibc-Systembibliothek
\item[3.5] Der Linux-Kernel
\end{itemize}
\vspace{-\smallskipamount}
\item[\textbf{\dots}]
\end{itemize}
\end{frame}
\setcounter{section}{2}
\section{Treiberentwicklung}
\begin{frame}[fragile]
\showsection
\subsection{Mikrocontroller}
\showsubsection
\vspace{-\medskipamount}
\begin{itemize}
\item
Direkter Zugriff auf Speicher, I/O-Ports und Interrupts
\end{itemize}
\bigskip
\subsection{Betriebssysteme ohne Speicherschutz}
\showsubsection
\vspace{-\medskipamount}
\begin{itemize}
\item
FreeDOS (früher: PC-DOS, MS-DOS, DR-DOS, Novell-DOS,\\
aber auch Apple-DOS, Commodore-Kernal, \dots):\\[\smallskipamount]
Direkter Zugriff auf Speicher, I/O-Ports und Interrupts
% \pause
\medskip
\item
Bildschirm (Textmodus): ab Speicherzelle \lstinline{B800:0000}\\
abwechselnd Zeichen (CP\,437) und Attribut (Farbe, Hintergrund)
% \pause
\medskip
\item
Bildschirm (Grafikmodus): später
\end{itemize}
\end{frame}
\subsection{Linux-Kernel-Module}
\begin{frame}[fragile]
% \showsection
\showsubsection
\begin{itemize}
\item
Kommunikation mit Betriebssystem über Gerätedateien,\\
z.\,B.\ \newterm{char devices}
\medskip
\item
\lstinline{#include <linux/module.h>}\\
\lstinline{#include <linux/kernel.h>}
\item
Zwei "`Hauptprogramme"': \lstinline{init_module()}, \lstinline{cleanup_module()}
\item
\lstinline{printk()} statt \lstinline{printf()}
\item
Compilieren mit speziellem \file{Makefile}
% \pause
\item
Angabe der Lizenz ist wichtig: \lstinline{MODULE_LICENSE()}
\medskip
\item
Kernel-Module können (via \newterm{udev\/})
Gerätedateien beim Laden anlegen\\
und beim Entladen wieder entfernen.
\end{itemize}
\end{frame}
\end{document}
https://www.kernel.org/doc/html/latest/driver-api/infrastructure.html?highlight=device_create
device_create() liefert im Fehlerfall nicht NULL zurück, sondern einen ERR_PTR.
https://www.kernel.org/doc/html/latest/core-api/kernel-api.html#c.ERR_PTR
Auswertung mit:
bool IS_ERR_OR_NULL(__force const void *ptr)
Detect an error pointer or a null pointer.
Parameters
__force const void *ptr
The pointer to check.
Description
Like IS_ERR(), but also returns true for a null pointer.
/*
* chardev.c: Creates a read-only char device that says how many times
* you've read from the dev file
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <asm/uaccess.h> /* for put_user */
MODULE_LICENSE("GPL");
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
struct class *dev_Class;
struct device *chr_dev;
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/*
* This function is called when the module is loaded
*/
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
return Major;
}
// Create module class
printk (KERN_INFO "Creating device class \"chardev\" ...\n");
dev_Class = class_create (THIS_MODULE,DEVICE_NAME);
if( dev_Class == NULL)
{
printk (KERN_ALERT "Error! Class couldn't be created!\n");
return 1;
}
printk (KERN_INFO "Class created!\n");
// Create device in /dev/...
printk (KERN_INFO "Creating device\n");
chr_dev = device_create (dev_Class, NULL, MKDEV (Major,0), NULL, DEVICE_NAME);
if (IS_ERR_OR_NULL (chr_dev))
{
printk( KERN_ALERT "Error! Device couldn't be created!\n" );
return 1 ;
}
return SUCCESS;
}
/*
* This function is called when the module is unloaded
*/
void cleanup_module(void)
{
printk(KERN_INFO "module chardev-3 cleanup\n");
//Unregister the device
if (chr_dev)
{
printk(KERN_INFO "Unregister device ...\n");
device_unregister(chr_dev);
printk(KERN_INFO "OK\n");
}
if (dev_Class)
{
printk(KERN_INFO "Unregister class ...\n");
class_unregister(dev_Class);
printk(KERN_INFO "OK\n");
}
printk(KERN_INFO "Unregister Chardev ...\n");
unregister_chrdev(Major, DEVICE_NAME);
printk(KERN_INFO "Device %s unregistered!\n", DEVICE_NAME);
}
/*
* Methods
*/
/*
* Called when a process tries to open the device file, like
* "cat /dev/mycharfile"
*/
static int device_open(struct inode *inode, struct file *file)
{
static int counter = 0;
if (Device_Open)
return -EBUSY;
Device_Open++;
sprintf(msg, "I already told you %d times Hello world!\n", counter++);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
/*
* Called when a process closes the device file.
*/
static int device_release(struct inode *inode, struct file *file)
{
Device_Open--; /* We're now ready for our next caller */
/*
* Decrement the usage count, or else once you opened the file, you'll
* never get get rid of the module.
*/
module_put(THIS_MODULE);
return 0;
}
/*
* Called when a process, which already opened the dev file, attempts to
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
/*
* Number of bytes actually written to the buffer
*/
int bytes_read = 0;
/*
* If we're at the end of the message,
* return 0 signifying end of file
*/
if (*msg_Ptr == 0)
return 0;
/*
* Actually put the data into the buffer
*/
while (length && *msg_Ptr) {
/*
* The buffer is in the user data segment, not the kernel
* segment so "*" assignment won't work. We have to use
* put_user which copies data from the kernel data segment to
* the user data segment.
*/
put_user(*(msg_Ptr++), buffer++);
length--;
bytes_read++;
}
/*
* Most read functions return the number of bytes put into the buffer
*/
return bytes_read;
}
/*
* Called when a process writes to dev file: echo "hi" > /dev/hello
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;
}
#include <stdio.h>
int main (void)
{
printf ("Hello, world!\n");
return 0;
}
#include <stdio.h>
int main (void)
{
puts ("Hello, world!");
return 0;
}
../common/io-ports-and-interrupts.pdf
\ No newline at end of file
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
../common/os-layers-0-planet.jpg
\ No newline at end of file
../common/os-layers-1-model.jpg
\ No newline at end of file
../common/os-layers-2-module.jpg
\ No newline at end of file
../common/os-layers-3-glibc.jpg
\ No newline at end of file
../common/os-layers-5-kernel.jpg
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
20240429/screenshot-20240506-174310.png

23.5 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment