introduc)on to the linux kernel · – device drivers – file system drivers • implementaon of a...
TRANSCRIPT
![Page 1: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/1.jpg)
Introduc)ontotheLinuxKernel
Prak)kumKernelProgrammingUniversityofHamburgScien)ficCompu)ng
Wintersemester2015/2016
Konstan)nosChasapisKonstan)nos.chasapis@informa)k.uni-hamburg.de
![Page 2: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/2.jpg)
Outline
• Introduc)on(story,licence,versioning)• Mainparts• LoadableKernelModules• SystemCalls• Security
4/11/15 Linux Kernel Intro. 1
![Page 3: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/3.jpg)
Introduc)on• DevelopedbyLinusTorvalds(1991)
– JustforFun:TheStoryofanAccidentalRevolu)onarybyLinusTorvalds
• BasedonUnix• 1stversionsupportedIntel80386• Currentlyvariouspla\ormsaresupported• ImplementedinGNUC• SeveralDistribu)ons(distro)
– RedHat,CentOS,Ubuntu,SUSE,Debian,Arch– Differentpackagesystem,configura)onetc.– Applydifferentpatches
4/11/15 Linux Kernel Intro. 2
![Page 4: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/4.jpg)
Introduc)on(cont.)• X-ServerisnotimplementedwithintheKernel• Everythingrunin“Kernelmode”
– Privilegedaccesstohardware• Monolithicbutboastsmodulardesign
– Kernelpreemp)on(undercertaincondi)ons)• Theschedulerispermieedtoforciblyperforma
contextswitch– Supportskernelthreads– Dynamicloadandunloadbinaries(kernelmodules)– Reentrant,severalprocessescanbeinkernelmode
simultaneously
4/11/15 Linux Kernel Intro. 3
![Page 5: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/5.jpg)
Introduc)on(cont.)• LicenseTerms
– islicensedundertheVersion2oftheGNUGeneralPublicLicense(GPL)
– AllowsanybodytoredistributeandevensellaproductcoveredbyGPLaslongastherecipienthasaccesstothesourceandisabletoexercisethesamerights
– AnysogwarederivedbyaproductcoveredbyGPLmustbereleasedundertheGPL
• Democra)ze,everyonecancontribute– Ifyouwantyourcodetogointothemainlineoryouhave
modifiedthekernelthenyouhavetouseGPL-compa)blelicense
4/11/15 Linux Kernel Intro. 4
![Page 6: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/6.jpg)
Introduc)on(cont.)• UseofbinaryBlobs(Modules,firmware)
– Thesourceisnotgiven– Maycontainpartofthedriverfromanotherfilesystem– Ifthecodehasbeenportedfromanotheropera)ngsystem
islegal– Ifacompanywantstokeepthesourceprivate– Usingsuchsogwareisdiscourage
• Versioning– $uname–a
3 . 17 . 1
major . minor . revision 4/11/15 Linux Kernel Intro. 5
![Page 7: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/7.jpg)
Outline
• Introduc)on(story,licence,versioning)• Mainparts• LoadableKernelModules• SystemCalls• Security
4/11/15 Linux Kernel Intro. 6
![Page 8: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/8.jpg)
Αpplications
Linuxsystemoverview
Shell
Kernel
Hardware
4/11/15 Linux Kernel Intro. 7
![Page 9: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/9.jpg)
Privileged mode
Requestflow
Applications
Shell
Kernel Hardware
4/11/15 Linux Kernel Intro. 8
![Page 10: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/10.jpg)
Mainparts
Process Management (PM)
Memory Management (MM)
Network Stack
Virtual File System (VFS)
Device Drivers (DD) Arch
System Call Interface
4/11/15 Linux Kernel Intro. 9
![Page 11: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/11.jpg)
Mainparts(cont.)• Systemcallinterface(SCI)
– Athinlayerthatprovidesamethodtointeractfromuserspacetokernelspace
• ProcessManagement(PM)– Create,destroyprocesses– Communica)onbetweendifferentprocesses(kernelthreads)– CPUscheduling
• MemoryManagement(MM)– Physicaltovirtualmemorymanagement– Memoryalloca)on– Swapping,frommemorytoharddisk
4/11/15 Linux Kernel Intro. 10
![Page 12: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/12.jpg)
Mainparts--I/OPath• VirtualFileSystem(VFS)
– Eportsthecommonfileinterface– Abstractfilesystemfunc)onalityfrom
implementa)on
• FileSystems– Implementa)onofFSfunc)onality
• BufferCache– Asetoffunc)onstomanipulatemain
memorydesignedforFS• DeviceDriver• PhysicalDevice
– Wheredatalive
VFS
ext4 /proc XFS
Buffer Cache
Device Drivers
Physical Devices 4/11/15 Linux Kernel Intro. 11
![Page 13: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/13.jpg)
Mainparts(cont.)• NetworkStack
– Implementthenetworkprotocols– Deliverpacketsacrossprogramsandnetworkinterfaces
• DeviceDrivers(DD)– Interactwiththehardware– Extractanabstrac)onofthedevicefunc)onali)es
• Arch– Architecturedependentcode
4/11/15 Linux Kernel Intro. 12
![Page 14: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/14.jpg)
Outline
• Introduc)on(story,licence,versioning)• Mainparts• LoadableKernelModules• SystemCalls• Security
4/11/15 Linux Kernel Intro. 13
![Page 15: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/15.jpg)
LKMs• LKMs(LoadableKernelModules)• Pre-compiledbinarypieces• Eachpieceiscalled“module”• Canbeloadedatrun)me• Extendthefunc)onalityofthesystem• Enforcemodularity
– Easytodevelop,debugandmaintain– Noneedtorebuildthekernel
• Cansavememory(loadonlythenecessary)
4/11/15 Linux Kernel Intro. 14
![Page 16: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/16.jpg)
WhatareLKMsusedfor• Everythingthatisnotrequiredinthecore• 6maincategories
– Devicedrivers– Filesystemdrivers
• Implementa)onofaspecificfilesystem– Systemcalls– Networkstack
• Interpretsanetworkprotocol– TTYlinedisciplines– Executableinterpretersforthesupportedformats
4/11/15 Linux Kernel Intro. 15
![Page 17: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/17.jpg)
CharacterDeviceDriver
• ReadorWriteabyteata)me• Accessedbyastreamofbytes• Usuallypermitonlysequen)alaccess• Implement:open,close,read,write• Similartoregularfiles
– /dev/console– /dev/eyS0
4/11/15 Linux Kernel Intro. 16
![Page 18: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/18.jpg)
BlockDeviceDriver
• ReadorWriteblock-sizemul)ples• Permitrandomaccess• Accessedinthe/dev/• Filesystemscanbemountontop• HandleI/Oopera)ons• Differwiththecharmoduleinthewaythemanagedatainsidethekernel
• Differentinterfacetothekernelthancharmodules
4/11/15 Linux Kernel Intro. 17
![Page 19: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/19.jpg)
NetworkDrivers• Handleanynetworktransac)onmade• Transferpacketsofdata• Independentofaspecificprotocol• Recep)onandTransmissioninsteadofRead/Write• Usuallytheinterfaceisahardwaredevicebutitcanalsobe
sogwareliketheloopback– loopbackisusedtocommunicatewiththeserversthat
runinthesamenode,debuggingetc.• Theyarenotmappedtothefilesystem;theyareiden)fied
byaname
4/11/15 Linux Kernel Intro. 18
![Page 20: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/20.jpg)
Outline
• Introduc)on(story,licence,versioning)• Mainparts• LoadableKernelModules• SystemCalls• Security
4/11/15 Linux Kernel Intro. 19
![Page 21: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/21.jpg)
Systemcalls• Asyscallcausesaprogrammedexcep)on(trap)ontheCPU
– syscall(number,arguments)• Withinthekernelyoucannotaccessuserspacebuffers
directly
Syscall Table
User Space
Kernel Space
write(fd, ptr, sz)
syscall(WRITE, fd, ptr,sz)
sys_write(f, up, sz)
vfs_write(f, p, sz) etx4_write(f, p, sz)
copy_from/to_user (to,from,sz)
4/11/15 Linux Kernel Intro. 20
![Page 22: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/22.jpg)
Outline
• Introduc)on(story,licence,versioning)• Mainparts• LoadableKernelModules• SystemCalls• Security
4/11/15 Linux Kernel Intro. 21
![Page 23: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/23.jpg)
Securityconsidera)ons• Securitycheckisenforcedbythekernel• IftheKernelhas„holes“Systemhasholes• Avoidintroducingtypicalprogrammingbugs
– Moduleparameters– Bufferoverrun– Memorycorrup)on
• Zeroorini)alizememorygiventouser• Runprecompiledkernelsfoundinyourdistro• Inofficialdistrosonlythesuperusercanloadandunload
modules
4/11/15 Linux Kernel Intro. 22
![Page 24: Introduc)on to the Linux Kernel · – Device drivers – File system drivers • Implementaon of a specific file system – System calls – Network stack • Interprets a network](https://reader034.vdocument.in/reader034/viewer/2022050610/5fb13966736d7c0c5f02375a/html5/thumbnails/24.jpg)
Kernel programming is vital for as long as new hardware is being
designed and produced or old-obsolete hardware is maintained.