8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
1/680
MMURTL V1.0
(Developing Your Own 32bit Operation System)
Richard A. Burgess
[ http://www.SensoryPublishing.com/mmurtl.html ]
2002-10-10
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
2/680
Book cover & Catalog added by
Zhao Jiong
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
3/680
Table of contents
Table of contents
CHAPTER 1, INTRODUCTION...................................... 3OVERVI EW. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3WHAT THI S BOOK I S ABOUT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4WHO SHOULD USE THI S BOOK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5MY BASI C DESI GN GOALS ( YOURS MAY VARY) . . . . . . . . . . . . . . . . . . . . . . 6A BRI EF DESCRI PTI ON OF MMURTL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7USES FOR MMURTL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9SI MI LARI TI ES TO OTHER OPERATI NG SYSTEMS. . . . . . . . . . . . . . . . . . . . 10HARDWARE REQUI REMENTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
CHAPTER 2, GENERAL DISCUSSION AND BACKGROUND................ 11
WHERE DOES ONE BEGI N?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11YOU ARE WHERE YOU WERE WHEN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
THE DEVELOPMENT PLATFORM AND THE TOOLS. . . . . . . . . . . . . . . . . . . . . 13THE CHI CKEN AND THE EGG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13EARLY HARDWARE I NVESTI GATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15THE REAL TASK AT HAND ( PUN I NTENDED) . . . . . . . . . . . . . . . . . . . . . . . 17A STARTI NG POI NT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17THE CRI TI CAL PATH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17WORKI NG ATTI TUDE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
CHAPTER 3, THE TASKING MODEL................................ 19
TERMS WE SHOULD AGREE ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19RESOURCE MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20TASK MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
SI NGLE VS. MULTI USER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21THE HARDWARE STATE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22THE SOFTWARE STATE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22CPU TI ME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23SI NGLE TASKI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23MULTI TASKI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23COOPERATI VE MULTI TASKI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24PREEMPTI VE MULTI TASKI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24TASK SCHEDULI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25WHO, WHEN, AND HOWLONG?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25SCHEDULI NG TECHNI QUES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
TI ME SLI CI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26SI MPLE COOPERATI VE QUEUI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
PRI ORI TI ZED COOPERATI VE SCHEDULI NG. . . . . . . . . . . . . . . . . . . . . . . 27VARI ABLE TI ME SLI CI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
OTHER SCHEDULI NG FACTORS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28MI XED SCHEDULI NG TECHNI QUES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
FULLY TI ME SLI CED, SOME COOPERATI ON. . . . . . . . . . . . . . . . . . . . . . 29TI ME SLI CED, MORE COOPERATI VE. . . . . . . . . . . . . . . . . . . . . . . . . . . . 29PRI MARI LY COOPERATI VE, LI MI TED TI ME SLI CI NG. . . . . . . . . . . . . . 30PRI MARI LY COOPERATI VE, MORE TI ME SLI CI NG. . . . . . . . . . . . . . . . . 30
THE TASKI NG MODEL I CHOSE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30I NTERRUPT HANDLI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
CHAPTER 4, INTERPROCESS COMMUNICATIONS...................... 33
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33MESSAGI NG AND TASKS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
4/680
Table of contents
SYNCHRONI ZATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34SEMAPHORES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35PI PES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36MESSAGES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
SEND AND WAI T. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36REQUEST( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
RESPOND( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43LI NK BLOCKS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
REENTRANCY I SSUES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44I NTERRUPT LATENCY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45MEMORY MANAGEMENT I SSUES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
CHAPTER 5, MEMORY MANAGEMENT................................ 47
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47BASI C TERMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47MEMORY MODEL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
SI MPLE FLAT MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48PAGED FLAT MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
DEMAND- PAGED FLAT MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50VI RTUAL PAGED MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51DEMAND PAGED VI RTUAL MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53DEMAND- PAGED MEMORY MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . 53SEGMENTED MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
MEMORY MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54TRACKI NG LI NEAR MEMORY ALLOCATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . 55BASI C MEMORY ALLOCATI ON UNI T. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55LI NKED LI ST MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55MEMORY MANAGEMENT WI TH TABLES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60TRACKI NG PHYSI CAL MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60I NI TI ALI ZATI ON OF MEMORY MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . 60MEMORY PROTECTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62AN I NTEL BASED MEMORY MANAGEMENT I MPLEMENTATI ON. . . . . . . . . . . . 62
A FEW MORE WORDS ON SEGMENTATI ON. . . . . . . . . . . . . . . . . . . . . . . . . 61HOWMMURTL USES PAGI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
PAGE TABLES ( PTS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62PAGE DI RECTORI ES ( PDS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62THE MEMORY MAP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
CHAPTER 6, HARDWARE INTERFACE............................... 72HARDWARE I SOLATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72THE CPU. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73THE BUS STRUCTURE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74SERI AL I / O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
PARALLEL I / O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75BLOCK- ORI ENTED DEVI CES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76KEYBOARD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76VI DEO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77DI RECT MEMORY ACCESS ( DMA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79TI MERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87PRI ORI TY I NTERRUPT CONTROLLER UNI T ( PI CU) . . . . . . . . . . . . . . . . . . 88
CHAPTER 7, OS INITIALIZATION................................ 89
BOOT ROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89THE BOOT SECTOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90OPERATI NG SYSTEM BOOT I MAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
OTHER BOOT OPTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100BASI C HARDWARE I NI TI ALI ZATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100STATI C TABLES AND STRUCTURES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
5/680
Table of contents
I NI TI ALI ZATI ON OF TASK MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . . 101I NI TI ALI ZATI ON OF MEMORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102DYNAMI C TABLES AND STRUCTURES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
CHAPTER 8, PROGRAMMING INTERFACES.......................... 104
APPLI CATI ON PROGRAMMI NG I NTERFACE. . . . . . . . . . . . . . . . . . . . . . . . . 104
MECHANI CAL DETAI LS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106PORTABI LI TY CONSI DERATI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107ERROR HANDLI NG AND REPORTI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107SYSTEMS PROGRAMMI NG I NTERFACE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108I NTERNAL COOPERATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108DEVI CE DRI VER I NTERFACES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108A DEVI CE- DRI VER I NTERFACE EXAMPLE. . . . . . . . . . . . . . . . . . . . . . . . . 109
ANNOUNCI NG THE DRI VER TO THE OS. . . . . . . . . . . . . . . . . . . . . . . . . 110A CALL TO THE DRI VER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
CHAPTER 9, APPLICATION PROGRAMMING......................... 114
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
TERMI NOLOGY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114UNDERSTANDI NG 32- BI T SOFTWARE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114TABLE 9. 1 - MEMORY ALI GNMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
OPERATI NG SYSTEM CALLI NG CONVENTI ONS. . . . . . . . . . . . . . . . . . . . . . 115STACK USAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116MEMORY MANAGEMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
TABLE 9. 2 - BASI C MEMORY MAP. . . . . . . . . . . . . . . . . . . . . . . . . . . . 117OPERATI NG SYSTEM PROTECTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118APPLI CATI ON MESSAGI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119STARTI NG A NEWTHREAD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119J OB CONTROL BLOCK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120BASI C KEYBOARD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121BASI C VI DEO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
TABLE 9. 3 - FOREGROUND COLORS ( LOW NI BBLE) . . . . . . . . . . . . . . 122TABLE 9. 4 - BACKGROUND COLORS ( HI GH NI BBLE) . . . . . . . . . . . . . 123
TERMI NATI NG YOUR APPLI CATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123REPLACI NG YOUR APPLI CATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
CHAPTER 10, SYSTEMS PROGRAMMING............................ 125
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125WRI TI NG MESSAGE- BASED SYSTEM SERVI CES. . . . . . . . . . . . . . . . . . . . . 125I NI TI ALI ZI NG YOUR SYSTEM SERVI CE. . . . . . . . . . . . . . . . . . . . . . . . . . 125A SI MPLE SYSTEM SERVI CE EXAMPLE. . . . . . . . . . . . . . . . . . . . . . . . . . . 126
LI STI NG 10. 1 - A SYSTEM SERVI CE. . . . . . . . . . . . . . . . . . . . . . . . . 126THE REQUEST BLOCK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
I TEMS I N THE REQUEST BLOCK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127THE SERVI CE CODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128CALLER I NFORMATI ON I N A REQUEST. . . . . . . . . . . . . . . . . . . . . . . . . . . 128ASYNCHRONOUS SERVI CES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129SYSTEM SERVI CE ERROR HANDLI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129WRI TI NG DEVI CE DRI VERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130DEVI CE DRI VER THEORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131BUI LDI NG DEVI CE DRI VERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131HOW CALLERS REACH YOUR DRI VER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132DEVI CE DRI VER SETUP AND I NSTALLATI ON. . . . . . . . . . . . . . . . . . . . . . 132SYSTEM RESOURCES FOR DEVI CE DRI VERS. . . . . . . . . . . . . . . . . . . . . . . 133
I NTERRUPTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
DI RECT MEMORY ACCESS DEVI CE (DMA) . . . . . . . . . . . . . . . . . . . . . . . 134TI MER FACI LI TI ES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134MESSAGE FACI LI TI ES FOR DEVI CE DRI VERS. . . . . . . . . . . . . . . . . . . . . 134
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
6/680
Table of contents
DETAI LED DEVI CE I NTERFACE SPECI FI CATI ON. . . . . . . . . . . . . . . . . . . 135DEVI CE CONTROL BLOCK SETUP AND USE. . . . . . . . . . . . . . . . . . . . . . 135TABLE 10. 1- . DEVI CE CONTROL BLOCK DEFI NI TI ON. . . . . . . . . . . . 135
STANDARD DEVI CE CALL DEFI NI TI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . 137DEVI CEOP FUNCTI ON I MPLEMENTATI ON. . . . . . . . . . . . . . . . . . . . . . . . 138DEVI CESTAT FUNCTI ON I MPLEMENTATI ON. . . . . . . . . . . . . . . . . . . . . . 139
DEVI CEI NI T FUNCTI ON I MPLEMENTATI ON. . . . . . . . . . . . . . . . . . . . . . 139I NI TI ALI ZI NG YOUR DRI VER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140OS FUNCTI ONS FOR DEVI CE DRI VERS. . . . . . . . . . . . . . . . . . . . . . . . . . . 141STANDARD DEVI CE ERROR CODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
CHAPTER 11, THE MONITOR PROGRAM............................ 142
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142ACTI VE J OB ( VI DEO & KEYBOARD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142I NI TI AL J OBS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
LI STI NG 11. 1. SAMPLE I NI TI AL. J OB FI LE. . . . . . . . . . . . . . . . . . . 143MONI TOR FUNCTI ON KEYS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
TABLE 11. 1. MONI TOR FUNCTI ON KEYS. . . . . . . . . . . . . . . . . . . . . . . 143
MONI TOR PROGRAM THEORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143PERFORMANCE MONI TORI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144MONI TOR SOURCE LI STI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
LI STI NG 11. 2. MONI TOR PROGRAM SOURCE LI STI NG. . . . . . . . . . . . . 145
CHAPTER 12, DEBUGGER....................................... 162
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162USI NG THE DEBUGGER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
ENTERI NG THE DEBUGGER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162EXI TI NG THE DEBUGGER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162DEBUGGER DI SPLAY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163DEBUGGER FUNCTI ON KEYS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
DEBUGGI NG YOUR APPLI CATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165DEBUGGER THEORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
TABLE 12. 1. PROCESSOR EXCEPTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . 167
CHAPTER 13, KEYBOARD SERVICE............................... 169
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169AVAI LABLE SERVI CES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
TABLE 13. 1. AVAI LABLE KEYBOARD SERVI CES. . . . . . . . . . . . . . . . . . 170READ KEYBOARD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170NOTI FY ON GLOBAL KEYS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171CANCEL NOTI FY ON GLOBAL KEYS. . . . . . . . . . . . . . . . . . . . . . . . . . . . 171ASSI GN KEYBOARD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
KEY CODES AND STATUS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
ALPHA- NUMERI C KEY VALUES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172TABLE 13. 2 - . SHI FT STATE BI TS. . . . . . . . . . . . . . . . . . . . . . . . . . 172LOCK- STATE BYTE (THI RD BYTE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173TABLE 13. 3 - LOCK STATE BI TS. . . . . . . . . . . . . . . . . . . . . . . . . . . . 173NUMERI C PAD I NDI CATOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
KEY CODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174TABLE 13. 4 - . KEYS VALUES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174FUNCTI ON STRI P KEY CODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175TABLE 13. 5 - FUNCTI ON KEY STRI P CODES. . . . . . . . . . . . . . . . . . . 175NUMERI C PAD KEY CODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176TABLE 13. 6 - NUMERI C PAD KEY CODES. . . . . . . . . . . . . . . . . . . . . . 176CURSOR, EDI T, AND SPECI AL PAD KEY CODES. . . . . . . . . . . . . . . . . 176
13. 7. ADDI TI ONAL KEY CODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176YOUR KEYBOARD I MPLEMENTATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
7/680
Table of contents
CHAPTER 14, THE FILE SYSTEM SERVICE........................ 178
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178FI LE SPECI FI CATI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178NETWORK FI LE REQUEST ROUTI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178FI LE HANDLES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179FI LE OPEN MODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
FI LE ACCESS TYPE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179BLOCK MODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179STREAM MODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
LOGI CAL FI LE ADDRESS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180FI LE SYSTEM REQUESTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
LI STI NG 14. 1 - OPENFI LE REQUEST I N C. . . . . . . . . . . . . . . . . . . . 181TABLE 14. 1 - FI LE SYSTEM SERVI CE CODES. . . . . . . . . . . . . . . . . . 181
PROCEDURAL I NTERFACES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182DEVI CE ACCESS THROUGH THE FI LE SYSTEM. . . . . . . . . . . . . . . . . . . . . 182
TABLE 14. 2 - DEVI CE STREAM ACCESS. . . . . . . . . . . . . . . . . . . . . . . 182FI LE SYSTEM FUNCTI ONS I N DETAI L. . . . . . . . . . . . . . . . . . . . . . . . . . . 183
OPENFI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
CLOSEFI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183READBLOCK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184WRI TEBLOCK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185READBYTES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185WRI TEBYTES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186GETFI LELFA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187SETFI LELFA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187GETFI LESI ZE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188SETFI LESI ZE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188CREATEFI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189RENAMEFI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190DELETEFI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190CREATEDI RECTORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191DELETEDI RECTORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191GETDI RSECTOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
FI LE SYSTEM THEORY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193I NTERNAL FI LE SYSTEM STRUCTURES. . . . . . . . . . . . . . . . . . . . . . . . . 193
FI LE CONTROL BLOCKS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193FI LE USER BLOCKS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193FAT BUFFERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
FI LE OPERATI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195READ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195WRI TE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195CLOSE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
CHAPTER 15, API SPECIFICATION.............................. 197I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197PUBLI C CALLS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
PARAMETERS TO CALLS (ARGS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197TABLE 15. 1 - SI ZE AND TYPE PREFI XES. . . . . . . . . . . . . . . . . . . 197TABLE 15. 2 - ADDI TI ONAL PREFI XES. . . . . . . . . . . . . . . . . . . . . . 198TABLE1 5. 3 - COMPOUND PREFI XES. . . . . . . . . . . . . . . . . . . . . . . . 198
ALPHABETI CAL CALL LI STI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202ADDCALLGATE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202ADDI DTGATE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202ALARM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203ALI ASMEM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
ALLOCDMAPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204ALLOCEXCH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204ALLOCOSPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
8/680
Table of contents
ALLOCPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205BEEP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205CHAI N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205CHECKMSG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206CLRSCR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206COMPARE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
COMPARENCS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207COPYDATA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207COPYDATAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207DEALI ASMEM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208DEALLOCEXCH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208DEALLOCPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208DEVI CEI NI T. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208DEVI CEOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209DEVI CESTAT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209DMASETUP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210FI LLDATA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211GETCMDLI NE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
GETCMOSTI ME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212GETCMOSDATE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212GETDMACOUNT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213GETEXI TJ OB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213GETI RQVECTOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213GETJ OBNUM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214GETNORMVI D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214GETPATH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214GETPHYADD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215GETPJ CB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215GETSYSI N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215GETSYSOUT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215GETTI MERTI CK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216GETTSSEXCH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216GETUSERNAME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216GETVI DCHAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217GETVI DOWNER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217GETXY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217I NBYTE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217I NDWORD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217I NWORD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218I NWORDS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218I NI TDEVDR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218I SENDMSG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219KI LLALARM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
LOADNEWJ OB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220MASKI RQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220MI CRODELAY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220MOVEREQUEST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221NEWTASK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221OUTBYTE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222OUTDWORD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222OUTWORD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222PUTVI DATTRS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223PUTVI DCHARS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223QUERYPAGES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224READCMOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
READKBD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224REGI STERSVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224REQUEST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
9/680
Table of contents
RESPOND. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226SCROLLVI D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226SENDMSG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227SETCMDLI NE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227SETEXI TJ OB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228SETI RQVECTOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
TABLE 15. 4 - HARDWARE I RQS. . . . . . . . . . . . . . . . . . . . . . . . . . . . 230SETJ OBNAME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230SETNORMVI D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231SETPATH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231SETPRI ORI TY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231SETSYSI N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232SETSYSOUT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232SETUSERNAME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232SETVI DOWNER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232SETXY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233SLEEP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233SPAWNTASK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
TONE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234TTYOUT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234UNMASKI RQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234UNREGI STERSVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235WAI TMSG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
CHAPTER 16, MMURTL SAMPLE SOFTWARE......................... 237
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237COMMAND LI NE I NTERPRETER ( CLI ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
I NTERNAL COMMANDS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237CLS ( CLEAR SCREEN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238COPY ( COPY A FI LE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238DI R ( DI RECTORY LI STI NG) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238DEBUG (ENTER DEBUGGER) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239DEL ( DELETE A FI LE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239DUMP ( HEX DUMP OF A FI LE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239EXI T ( EXI T THE CLI ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239HELP ( LI ST I NTERNAL COMMANDS) . . . . . . . . . . . . . . . . . . . . . . . . . 239MONI TOR ( RETURN TO MONI TOR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 239MD ( MAKE DI RECTORY) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240PATH ( SET FI LE ACCESS PATH) . . . . . . . . . . . . . . . . . . . . . . . . . . . 240RD (REMOVE DI RECTORY) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240RENAME ( RENAME A FI LE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241RUN (EXECUTE A . RUN FI LE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241TYPE ( VI EW A TEXT FI LE ON THE SCREEN) . . . . . . . . . . . . . . . . . 241
EXTERNAL COMMANDS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241COMMANDS. CLI FI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241GLOBAL "HOT KEYS" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
CLI SOURCE LI STI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242LI STI NG 16. 1. CLI SOURCE CODE LI STI NG VER. 1. 0. . . . . . . . . . . 242
A SI MPLE EDI TOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259EDI TOR SCREEN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259EDI TOR COMMANDS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259TABLE 16. 1 - FI LE MANAGEMENT AND GENERAL COMMANDS. . . . . . . 260TABLE 16. 2 - CURSOR AND SCREEN MANAGEMENT COMMANDS. . . . . . 260LI STI NG 16. 2. EDI TOR SOURCE CODE. . . . . . . . . . . . . . . . . . . . . . . . . 261
DUMBTERM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
LI STI NG 16. 3 - DUMBTERM SOURCE CODE. . . . . . . . . . . . . . . . . . . . . 285PRI NT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288LI STI NG 16. 4. PRI NT SOURCE CODE. . . . . . . . . . . . . . . . . . . . . . . . . . 288
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
10/680
Table of contents
SYSTEM SERVI CE EXAMPLE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292SERVI CE. C LI STI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293LI STI NG 16. 5 - SI MPLE SERVI CE SOURCE CODE. . . . . . . . . . . . . . . 293
TESTSVC. C LI STI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295LI STI NG 16. 6 - TESTSVC. C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
CHAPTER 17, INTRODUCTION TO THE SOURCE CODE................ 296
COMMENTS I N THE CODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296CALLI NG CONVENTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296ORGANI ZATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296BUI LDI NG MMURTL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
LI STI NG 17. 1 - ASSEMBLER TEMPLATE FI LE. . . . . . . . . . . . . . . . . . 300USI NG PI ECES OF MMURTL I N YOUR OS. . . . . . . . . . . . . . . . . . . . . . . . . 301USI NG PI ECES OF MMURTL I N OTHER PROJ ECTS. . . . . . . . . . . . . . . . . . 301
CHAPTER 18, THE KERNEL..................................... 302
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302NAMI NG CONVENTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
KERNEL DATA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302LI STI NG 18. 1. KERNEL DATA SEGMENT SOURCE. . . . . . . . . . . . . . . . . 303LOCAL KERNEL HELPER FUNCTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303ENQUEUEMSG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
LI STI NG 18. 2. QUEUEI NG FOR MESSAGES AT AN EXCHANGE. . . . . . . 304DEQUEUEMSG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
LI STI NG 18. 3. DE- QUEUEI NG A MESSAGE FROM AN EXCHANGE. . . . . 305DEQUEUETSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
LI STI NG 18. 4. DE- QUEUEI NG A TASK FROM AN EXCHANGE. . . . . . . . 305ENQUEUERDY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
LI STI NG 18. 5. ADDI NG A TASK TO THE READY QUEUE. . . . . . . . . . . 306DEQUEUERDY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
LI STI NG 18. 6. DE- QUEUEI NG THE HI GHEST PRI ORI TY TASK. . . . . . 307CHKRDYQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307I NTERNAL PUBLI C HELPER FUNCTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . 308
REMOVERDYJ OB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308LI STI NG 18. 8. REMOVI NG A TERMI NATED TASK FROM THE READYQUEUE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
GETEXCHOWNER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309LI STI NG 18. 9. FI NDI NG THE OWNER OF AN EXCHANGE. . . . . . . . . 311
SETEXCHOWNER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312LI STI NG 18. 10. CHANGI NG THE OWNER OF AN EXCHANGE. . . . . . . 312
SENDABORT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312PUBLI C KERNEL FUNCTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
REQUEST( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
LI STI NG 18. 12. REQUEST KERNEL PRI MI TI VE CODE. . . . . . . . . . . 314RESPOND( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
LI STI NG 18. 13. RESPOND KERNEL PRI MI TI VE CODE. . . . . . . . . . . 317MOVEREQUEST( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320SENDMSG( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
LI STI NG 18. 15. SENDMSG KERNEL PRI MI TI VE CODE. . . . . . . . . . . 321I SENDMSG( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
LI STI NG 18. 16. I SENDMSG KERNEL PRI MI TI VE CODE. . . . . . . . . . 324WAI TMSG( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325CHECKMSG( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
LI STI NG 18. 18 - CHECKMSG KERNEL PRI MI TI VE CODE. . . . . . . . . 330NEWTASK( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
SPAWNTASK( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334ALLOCEXCH( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337DEALLOCEXCH( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
11/680
Table of contents
GETTSSEXCH( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339LI STI NG 18. 23. GETTSS EXCHANGE CODE. . . . . . . . . . . . . . . . . . . . 339
SETPRI ORI TY( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340LI STI NG 18. 24. SET PRI ORI TY CODE. . . . . . . . . . . . . . . . . . . . . . . 340
GETPRI ORI TY( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340LI STI NG 18. 25. GET PRI ORI TY CODE. . . . . . . . . . . . . . . . . . . . . . . 340
CHAPTER 19, MEMORY MANAGEMENT CODE......................... 342
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342MEMORY MANAGEMENT DATA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
I NTERNAL CODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343I NI TMEMMGMT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
LI STI NG 19. 3. CONTI NUATI ON OF MEMORY MANAGEMENT I NI T. . . 344LI STI NG 19. 4. CONTI NUATI ON OF MEMORY MANAGEMENT I NI T. . . 344LI STI NG 19. 6. ALLOCATI ON OF MEMORY MANAGEMENT EXCHANGE. 345LI STI NG 19. 7. FI NI SHI NG MEMORY MANAGEMENT I NI TI . . . . . . . . 345
FI NDHI PAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346LI STI NG 19. 8. FI ND HI GHEST PHYSI CAL PAGE CODE. . . . . . . . . . 346
FI NDLOPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347LI STI NG 19. 9. FI ND LOWEST PHYSI CAL PAGE CODE. . . . . . . . . . . 347MARKPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
LI STI NG 19. 10. CODE TO MARK A PHYSI CAL PAGE I N USE. . . . . 348UNMARKPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
LI STI NG 19. 11. CODE TO FREE A PHYSI CAL PAGE OFR REUSE. . 349LI NTOPHY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
LI STI NG 19. 12. CODE TO CONVERT LI NEAR TO PHYSI CALADDRESSES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
FI NDRUN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350LI STI NG 19. 13. CODE TO FI ND A FREE RUN OF LI NEAR MEMORY 350
ADDRUN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352ADDALI ASRUN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
LI STI NG 19. 15. ADDI NG AN ALI ASED RUN OF LI NEAR MEMORY. . 353ADDUSERPT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
LI STI NG 19. 16. ADDI NG A PAGE TABLE FOR USER MEMORY. . . . . 355ADDOSPT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
LI STI NG 19. 17. ADDI NG AN OPERATI NG SYSTEM PAGE TABLE. . . 357PUBLI C MEMORY MANAGEMENT CALLS. . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
ADDGDTCALLGATE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359LI STI NG 19. 18. ADDI NG A CALL GATE TO THE GDT. . . . . . . . . . . 359
ADDI DTGATE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360LI STI NG 19. 19. ADDI NG ENTRI ES TO THE I DT. . . . . . . . . . . . . . . 360
ALLOCOSPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361LI STI NG 19. 20. ALLOCATI NG A PAGE OF LI NEAR MEMORY. . . . . . 361
ALI ASMEM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362LI STI NG 19. 21. CODE TO ALI AS MEMORY. . . . . . . . . . . . . . . . . . . . 362
DEALI ASMEM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365LI STI NG 19. 22. CODE TO REMOVE ALI AS PTES. . . . . . . . . . . . . . . 365
DEALLOCPAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366LI STI NG 19. 23. DEALLOCATI NG LI NEAR MEMORY. . . . . . . . . . . . . . 366
QUERYMEMPAGES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368LI STI NG 19. 24. CODE TO FI ND NUMBER OF FREE PAGES. . . . . . . 368
GETPHYADD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
CHAPTER 20, TIMER MANAGEMENT SOURCE CODE................... 370
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
CHOOSI NG A STANDARD I NTERVAL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370LI STI NG 20. 1. LOOP I N TI MER I SR. . . . . . . . . . . . . . . . . . . . . . . . . . 371TI MER DATA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
12/680
Table of contents
LI STI NG 20. 2 - DATA AND CONSTANTS FOR TI MER CODE. . . . . . . . 372TI MER CODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
LI STI NG 20. 3 - EXTERNAL FUNCTI ONS FOR TI MER CODE. . . . . . . . 372THE TI MER I NTERRUPT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
LI STI NG 20. 4 - THE TI MER I NTERRUPT SERVI CE ROUTI NE 373SLEEP( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
LI STI NG 20. 5 - CODE FOR THE SLEEP FUNCTI ON. . . . . . . . . . . . 375ALARM( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
LI STI NG 20. 6 - CODE FOR THE ALARM FUNCTI ON. . . . . . . . . . . . 376KI LLALARM( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
LI STI NG 20. 7 - CODE FOR THE KI LLALARM FUNCTI ON. . . . . . . . 377MI CRODELAY( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
LI STI NG 20. 8 - CODE FOR THE MI CRDELAY FUNCTI ON. . . . . . . . 378GETCMOSTI ME( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
LI STI NG 20. 9. CODE TO READ THE CMOS TI ME. . . . . . . . . . . . . . . 378GETCMOSDATE( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
LI STI NG 20. 10 - CODE TO READ THE CMOS DATE. . . . . . . . . . . . 379GETTI MERTI CK( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
LI STI NG 20. 11. CODE TO RETURN THE TI MER TI CK. . . . . . . . . . . 380BEEP_WORK( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381LI STI NG 20. 12. HELPER FUNCTI ON FOR BEEP( ) AND TONE( ) 381
BEEP( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382LI STI NG 20. 13. CODE TO PRODUCE A BEEP. . . . . . . . . . . . . . . . . . 382
TONE( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382LI STI NG 20. 14 - CODE TO PRODUCE A TONE. . . . . . . . . . . . . . . . 382
CHAPTER 21, INITIALIZATION CODE............................ 384
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384OS GLOBAL DATA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
LI STI NG 21. 1 - MAI N OPERATI NG SYSTEM DATA. . . . . . . . . . . . . . . 385OPERATI NG SYSTEM ENTRY POI NT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
LI STI NG 21. 5 - END OF I NI TI ALI ZATI ON CODE. . . . . . . . . . . . . . . 393I NI TI ALI ZATI ON HELPERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
LI STI NG 21. 6 - I NI TI ALI ZATI ON SUBROUTI NES. . . . . . . . . . . . . . . 394LI STI NG 21. 7 - I NI TI ALI ZE CALL GATES AMD THE I DT. . . . . . . . 397
CHAPTER 22, JOB MANAGEMENT CODE............................ 403
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403RECLAMATI ON OF RESOURCES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403J OB MANAGEMENT HELPERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
LI STI NG 22. 1 - J OB MANAGEMENT SUBROUTI NES. . . . . . . . . . . . . . . 403J OB MANAGEMENT LI STI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
CHAPTER 23, DEBUGGER CODE.................................. 433I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433DEBUGGER I NTERRUPT PROCEDURE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
LI STI NG 23. 1 - EXCEPTI ON HANDLERS FOR ENTERI NG DEBUGGER 433
CHAPTER 24, SELECTED DEVICE DRIVER CODE.................... 458
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458I DE DI SK DEVI CE DRI VER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
LI STI NG 24. 1 - I DE DEVI CE DRI VER DATA ( DEFI NES ANDEXTERNS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459LI STI NG 24. 2 - CONTI NUATI ON OF I DE DRI VER DATA ( PROTOSAND DEFI NES) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
LI STI NG 24. 3 - CONTI NUATI ON OF I DE DRI VER DATA ( DATASTRUCTURES) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463LI STI NG 24. 4 - I DE DEVI CE DRI VER CODE ( I NI TI AKI ZATI ON) 464
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
13/680
Table of contents
LI STI NG 24. 5 - CONTI NUATI ON OF I DE DRI VER CODE ( CODE) 468LI STI NG 24. 6 - CONTI NUATI ON OF I DE DRI VER CODE ( DEVI CEI NTERFACE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
THE RS- 232 ASYNCHRONOUS DEVI CE DRI VER. . . . . . . . . . . . . . . . . . . . . 479RS232. H. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
LI STI NG 24. 7 - RS- 232. H HEADER FI LE. . . . . . . . . . . . . . . . . . . 480
THE RS- 232C DEVI CE DRI VER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481LI STI NG 24. 8 - RS- 232 DEVI CE DRI VER CODE ( DEFI NES ANDEXTERNS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481LI STI NG 24. 9 - RS- 232 DEVI CE DRI VER CODE CONTI NUED( REGI STER BI TS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484LI STI NG 24. 10 - RS- 232 DEVI CE DRI VER CODE CONTI NUED( STATUS RECORD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485LI STI NG 24. 11 - RS- 232 DEVI CE DRI VER CODE CONTI NUED( DCB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486LI STI NG 24. 12 - RS- 232 DEVI CE DRI VER CODE CONTI NUED( DCB I NI T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486LI STI NG 24. 13 - RS- 232 DEVI CE DRI VER CODE CONTI NUED
( I SR AND CODE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488LI STI NG 24. 14 - RS- 232 DEVI CE DRI VER CODE CONTI NUED( I NTERFACE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
CHAPTER 25, KEYBOARD SOURCE CODE........................... 501
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501KEYBOARD DATA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
LI STI NG 25. 1 - KEYBOARD DATA AND TABLES. . . . . . . . . . . . . . . . . 501KEYBOARD CODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
KEYBOARD I SR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509LI STI NG 25. 2 - KEYBOARD I SR CODE. . . . . . . . . . . . . . . . . . . . . . 509
KEYBOARD TRANSLATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510LI STI NG 25. 3 - KEYBOARD TRANSLATI ON CODE. . . . . . . . . . . . . . 511
READI NG THE FI NAL BUFFER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515LI STI NG 25. 4. READ KEYBOARD BUFFER CODE. . . . . . . . . . . . . . . . 515
THE KEYBOARD SERVI CE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516LI STI NG 25. 5. KEYBOARD SYSTEM SERVI CE CODE. . . . . . . . . . . . . 516
KEYBOARD PROCEDURAL I NTERFACE. . . . . . . . . . . . . . . . . . . . . . . . . . . 522LI STI NG 25. 6 - CODE FOR BLOCKI NG READ KEYBOARD CALL 522
DEBUGGER KEYBOARD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523LI STI NG 25. 7 - DEBUGGER READ KEYBOARD CODE. . . . . . . . . . . . 524
KEYBOARD HARDWARE SET UP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524LI STI NG 25. 8 - CODE TO I NI TI ALI ZE THE KEYBOARD HARDWARE 524
KEYBOARD SERVI CE I NI TI ALI ZATI ON. . . . . . . . . . . . . . . . . . . . . . . . . 525LI STI NG 25. 9 - KEYBOARD SERVI CE I NI TI ALI ZATI ON. . . . . . . . 525
HARDWARE HELPERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527LI STI NG 25. 10 - LOW- LEVEL KEYBOARD CONTROL CODE. . . . . . . 527
CHAPTER 26, VIDEO CODE..................................... 529
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529VI RTUAL VI DEO CONCEPT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529VGA TEXT VI DEO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529THE VI DEO CODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
LI STI NG 26. 1. VI DEO CONSTANTS AND DATA. . . . . . . . . . . . . . . . . . . 530LI STI NG 26. 2 - CODE TO I NI TI ALI ZE VI DEO. . . . . . . . . . . . . . . . . 531LI STI NG 26. 3 - CODE TO SET THE VI DEO OWNER. . . . . . . . . . . . . . 531LI STI NG 26. 4 - CODE TO SET NORMAL VI DEO ATTRI BUTE. . . . . . . 533
LI STI NG 26. 5 - CODE TO GET THE NORMAL VI DEO ATTRI BUTE 533LI STI NG 26. 6 - CODE TO GET THE CURRENT VI DEO OWNER. . . . . . 534LI STI NG 26. 7 - CODE TO CLEAR THE SCREEN. . . . . . . . . . . . . . . . . 534
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
14/680
Table of contents
LI STI NG 26. 8 - CODE FOR TTY STREAM OUTPUT TO SCREEN. . . . . 535LI STI NG 26. 9. CODE TO SCREEN VI DEO ATTRI BUTES. . . . . . . . . . . . 537LI STI NG 26. 10 - CODE TO PLACE VI DEO CHARS ANYWHERE ONTHE SCREEN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538LI STI NG 26. 11. CODE TO GET A CHARACTER AND ATTRI BUTE. . . . . 539LI STI NG 26. 12. CODE TO SCROLL AN AREA OF THE SCREEN. . . . . . 540
LI STI NG 26. 13 - SUPPORT CODE TO SET HARDWARE CURSORPOSI TI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542LI STI NG 26. 14. CODE TO SET CURSOR POSI TI ON. . . . . . . . . . . . . . . 542LI STI NG 26. 15. CODE TO RETURN CURRENT CURSOR POSI TI ON 543LI STI NG 26. 16 - CODE TO EDI T A LI NE ON THE SCREEN. . . . . . . 544
RELATI NG MMURTL' S VI DEO CODE TO YOUR OPERATI NG SYSTEM. . . . . 546
CHAPTER 27, FILE SYSTEM CODE............................... 547
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547HOWMMURTL HANDLES FAT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
PHYSI CAL ( ABSOLUTE) SECTOR NUMBERS. . . . . . . . . . . . . . . . . . . . . . 547HI DDEN SECTORS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
LOGI CAL BLOCK ADDRESS ( LBA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548MS- DOS BOOT SECTOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548FI LE SYSTEM I NI TI ALI ZATI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
FI LE SYSTEM LI STI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549LI STI NG 27. 1 - MS- DOS FAT- COMPATI BLE FI LE SYSTEM CODE 549
CHAPTER 28, DASM; A 32-BIT INTEL-BASED ASSEMBLER........... 617
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617DASM CONCEPTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617USI NG DASM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
APPLI CATI ON TEMPLATE FI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618LI STI NG 28. 1 - SI MPLE APPLI CATI ON TEMPLATE FI LE. . . . . . . 619LI STI NG 28. 2 - A MORE COMPLI CATED TEMPLATE FI LE. . . . . . 619
COMMAND LI NE OPTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620LI ST FI LE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620ERRORS ONLY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621DYNAMI C LI NK LI BRARI ES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621DEVI CE DRI VERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
DASM SOURCE FI LES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621LOCAL ( NON- PUBLI C) DECLARATI ONS. . . . . . . . . . . . . . . . . . . . . . . 621
MEMORY MODEL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621DASM PROGRAM STRUCTURE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
LI STI NG 28. 3 - ALTERNATI NG DATA & CODE I N A TEMPLATE FI LE 622DOT COMMANDS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622DASM VERSUS DOS ASSEMBLERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
DASM ADDRESSI NG MODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625TABLE 28. 1 - . SPECI FYI NG MEMORY OPERANDS. . . . . . . . . . . . . . 626
STORAGE DECLARATI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628DB ( DEFI NE BYTE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628DW ( DEFI NE WORD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628DD ( DEFI NE DOUBLE WORD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628DF ( DEFI NE FAR WORD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
DASM PECULI AR SYNTAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629PUBLI C DECLARATI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629EXTRN DECLARATI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629LABELS AND ADDRESS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630SEGMENT AND I NSTRUCTI ON PREFI XES. . . . . . . . . . . . . . . . . . . . . . 630
CALL AND J UMP ADDRESSES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631386 I NSTRUCTI ON SET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634TABLE 28. 2 - SUPPORTED I NSTRUCTI ONS. . . . . . . . . . . . . . . . . . . . . 634
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
15/680
Table of contents
EXECUTABLE FI LE FORMAT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638TABLE 28. 3 - TAG USAGE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639TAG DESCRI PTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
ERROR CODES FROM DASM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642YOUR OWN ASSEMBLER?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
CHAPTER 29, CM32; A 32-BIT C COMPILER...................... 646
I NTRODUCTI ON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646MEMORY USAGE AND ADDRESSI NG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647I MPORTANT I NTERNAL DI FFERENCES. . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
VARI ABLE LENGTH ARGUMENT LI STS. . . . . . . . . . . . . . . . . . . . . . . . . . 647CM32 LANGUAGE SPECI FI CS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
FAR CALLS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648I NTERRUPT FUNCTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648SUPPORTED C LANGUAGE FEATURES. . . . . . . . . . . . . . . . . . . . . . . . . . . 648LI BRARY HEADER FI LE ACCESS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650LI MI TATI ONS ( COMPARED TO ANSI ) . . . . . . . . . . . . . . . . . . . . . . . . . . 650
STRUCTURE LI MI TATI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
FAR EXTERNAL FUNCTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651TYPE CONVERSI ON AND POI NTERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . 651I NI TI ALI ZATI ON OF STATI C VARI ABLES. . . . . . . . . . . . . . . . . . . . . . 652
OTHER I MPLEMENTATI ON DEPENDENT I NFORMATI ON. . . . . . . . . . . . 654USI NG CM32. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
COMMAND LI NE OPTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654LI BRARY FUNCTI ONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
LI STI NG 29. 1 - SUPPORTED LI BRARY FUNCTI ONS. . . . . . . . . . . . 655LI BRARY CODE EXAMPLES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
LI STI NG 29. 2 - STANDARD FI LE I / O FOR MMURTL. . . . . . . . . . . 657LI STI NG 29. 3 - STANDARD STRI NG FUNCTI ONS FOR MMURTL . . 662
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
16/680
MMURTL V1.0 Page 1 of 667
MMURTL V1.0
Richard A. Burgess
Previously published by MacMillan Publishing/SAMS Computer Books as"Developing Your Own 32 Bit Computer Operating System"
Author: Richard A. BurgessCopyright 1995 Richard A. Burgess,
Copyright 1999 Sensory Publishing, Inc. & Richard A. Burgess
All Rights Reserved
Portions of this electronic book were formatted with software produced by:
Adobe Corporation for theAcrobat Reader Version,and Sensory Publishing, Inc. & Microsoft Corp. for the Wordand HTML versions.
Copyright 1999, Sensory Publishing, Inc.
Please treat the electronic version of this book as you would a printed copy of any book.
You may pass this text on to another individual or institution when you are through with it
upon deleting your copy as if you no longer held a printed copy of the book.
No portion of this book may be made publicly available by electronic or
other means without the express written permission of Sensory Publishing, Inc.
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
17/680
MMURTL V1.0 Page 2 of 667
Forward
First of all, I thank all of you for the continued interest in MMURTL. When the first printed copies of this book(then namedDeveloping Your Own 32 Bit Computer Operating System) hit the bookstands, 32 bitwas the buzzword of the day. Five years later the buzzword is the New Millennium; 2000. But it still seems that operating systemdevelopment is a hot topic (look at Linux go! - you GO Mr. Penguin, you go!). Many people have tracked me down
to attempt to find more copies of the original edition of the book and I can tell you they are very scarce. Anotherprinting was denied by the original publisher which turned the rights to my book back over to me. Of the 10,000-plus copies sold, I have 5 printed copies left (which needless to say, Im holding on to for sentimental reasons.). Butif youre reading this, the paper book itself isnt what youre interested in, its the information in the book.
I had initially intended to put up a web site for everyone to share their MMURTL interests and findings, but littlethings like "earning a living" - "kids in college, etc." kept getting in the way. A friend of mine (an entrepreneur atheart) was itching to start another online business and I had always been interested in publishing - the not so simpleart of information dissemination - and I offered some technical assistance. Electronic publishing is a new art - orscience as some would lead you to believe.
I have not had much time to work on MMURTL since the book was first published. As near as I can tell, a fewpeople have been playing with it and it has been used mostly as a learning tool. Sensory Publishing is willing to put
up a section on their servers for a BBS (Bulletin Board System) for MMURTL and other books that they willpublish for those that want a place to exchange information and ask questions. I will try to make as much time aspossible to answer questions there, and I would also like to see everyone that has something to share aboutMMURTL to add their two cents.
The book is being sold online in several unprotected formats which some people think is risky. I dont think so. I puta little bit more faith in the human race than most. And besides, it will cut the cost of distribution in half and I mayactually be able to pay for all those computers and books I bought to support the original development effort - thoseof you that think authors of books of this type make any money from them have a few lessons to learn. Of theapproximately one half million dollarsthe book grossed (between the publisher and book sellers - I got about $1.80a book... pitiful. That translates to roughly 1/2 minimum wage for the hours invested to produce this book. Thathardly repays the creditors who will gladly lend you money to feed the "MEGAHERTZ habit" that is needed to stayon top of the computing world. Remember when a 386 - 20 MHz cost $5000? I do, I bought one hot off theassembly line to write MMURTL back in 83 (whoa - I'm getting old...).
Anyway, I hope you use MMURTL V1.0 to learn, enjoy and explore. You have my permission to use any of the
code (with the exception of the C compiler) for any project you desire - public or private - so long as you have
purchased a copy of the book.My way of saying thanks. The only requirement is that you give some visible credit
to MMURTL for the assistance with your project.
Most sincerely,
Richard A. Burgess
Alexandria Virginia, 1999
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
18/680
MMURTL V1.0 Page 3 of 667
Chapter 1, Introduction
Overview
Computer programmers and software engineers work with computer operating systemsevery day. They use them, they work with them, and they even work "around" them toget their jobs done. If youre an experienced programmer, Im willing to bet youvepondered changes you would make to operating systems you use, or even thought aboutwhat you would build and design into one if you were to write your own.
You dont have to be Albert Einstein to figure out that many labor years go into writing acomputer operating system. You also dont have to be Einstein to write one, although Imsure it would have increased my productivity a little while writing my own.Unfortunately, I have Alberts absent-mindedness, not his IQ.
Late in 1989 I undertook the task of writing a computer operating system. What Idiscovered is that most of the books about operating system design are very heavy ongeneral theory and existing designs, but very light on specifics, code, and advice into theseemingly endless tasks facing someone that really wants to sit down and create one. Imnot knocking the authors of these books; Ive learned a lot from all that Ive read. It justseemed there should be a more "down to earth" book that documents what you need toknow to get the job done.
Writing my own operating system has turned into a very personal thing for me. I havebeen involved in many large software projects where others conceived the, and I was to
turn them into working code and documentation. In the case of my own operatingsystem, I set the ground rules. I decided what stayed and what went. I determined thespecifications for the final product.
When I started out, I thought the specs would be the easy part. I would just make a list,do some research on each of the desirables, and begin coding. NOT SO FAST THERE,BUCKO!
I may not have to tell you this if you've done some serious "code cutting," but the
simplest items on the wish list for a piece of software can add years to the critical path forresearch and development, especially if you have a huge programming team of one
person.
My first list of desirables looked like a kid's wish list for Santa Claus. I hadn't been agood-enough boy to get all those things (and the average life expectancy of a male human
being wouldn't allow it either). Realizing this, I whittled away to find a basic set ofingredients that constitute the real make-up of the "guts" of operating systems. The most
obvious of these, as viewed by the outside programmer, are the tasking model, memorymodel, and the programming interface. As coding began, certain not-so-obvious
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
19/680
MMURTL V1.0 Page 4 of 667
ingredients came into play such as the OS-to-hardware interface, portability issues sizeand speed.
One of the single most obvious concerns for commercial operating system developers iscompatibility. If you write something that no one can use, its unlikely theyll use it. Each
of the versions of MS-DOS that were produced had to be able to run software written forprevious versions. When OS/2 was first developed, the "DOS-BOX" was a bad joke. Itdidnt provide the needed compatibility to run programs that business depended on. In theearly days, Unix had a somewhat more luxurious life, because source-code compatibilitywas the main objective; you were pretty much expected to recompile something if youwanted it to run on your particular flavor of the system. Im sure youve noticed that thishas changed substantially.
I did not intend to let compatibility issues drive my design decisions. I was not out tocompete with the likes of Microsoft, IBM, or Sun. You, however, may want compatibilityif you intend to write your own operating system. Its up to you. One thing about desiring
compatibility is that the interfaces are all well documented. You dont need to design yourown. But I wanted a small API. That was part of my wish list.
Along my somewhat twisted journey of operating-system design and implementation, Ihave documented what it takes to really write one, and Ive included the "unfinished"product for you to use with very few limitations or restrictions. Even though I refer to itas unfinished, its a complete system. Its my belief that when a piece of software is"finished" its probably time to replace it due to obsolescence or lack of maintenance.
What This Book Is About
In this book I discuss the major topics of designing your own OS: the tasking model, thememory model, programming interfaces, hardware interface, portability issues, size andspeed. I back up these discussions showing you what I decided on (for my own system),and then finally discuss my code in great detail. Your wish list may not be exactly likemine. In fact, Im sure it wont. For instance, youll notice the ever so popular term "objectoriented" blatantly missing from any of my discussions. This was completely intentional.It clouds far too many real issues these days. Dont get me wrong - Im an avid C++ andOOP user. But thats not what this book is about, so Ive removed the mystique. Onceagain, your goals may be different.
Throughout this book, I use the operating system I developed (MMURTL) to helpexplain some of the topics I discuss. You can use pieces of it, ideas from it, or all of it ifyou wish to write your own.
I have included the complete source code to my 32-bit; message based, multitasking,real-time, operating system (MMURTL) which is designed for the Intel 386/486/Pentiumprocessors on the PC Industry Standard Architecture (ISA) platforms.
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
20/680
MMURTL V1.0 Page 5 of 667
The source code, written in 32-bit Intel based assembly language and C, along with allthe tools necessary to build, modify, and use the operating system are included on theaccompanying CD-ROM. The hardware requirement section below tells you what youneed to run MMURTL and use the tools I have developed. Its not really required if youdecide on a different platform and simply use this book as a reference.
One thing I didnt put on my wish list was documentation. I always thought that manysystems lacked adequate documentation (and most of it was poorly written). I wanted toknow more about how the system worked internally, which I felt, would help me writebetter application software for it. For this reason, I have completely documented everyfacet of my computer operating system and included it here. You may or may not want todo this for your system, but it was a "must" for me. I think you will find that it will helpyou if you intend to write your own. Well-commented code examples are worth tentimes their weight in generic theory or simple text-based pseudo-algorithms.
The Architecture and General Theory section of this book along with the sections that
are specific the MMURTL operating system, will provide the information for you to usemy system as is, redesign it to meet your requirements, or write your own. If you don't
like the way something in my system is designed or implemented, change it to suit yourneeds. The only legal restriction placed on your use of the source code is that you may
not sell it or give it away.
Who Should Use This Book
This book is for you if you are a professional programmer or a serious hobbyist, and you
have an interest in any of the following topics:
Writing a 32-bit microcomputer operating system 80386/486/Pentium 32 bit assembly language (an assembler is included).
The C Programming language (a C compiler is included
Intel 80386/486/Pentium Paged memory operation and management using theprocessor paging hardware
Intel 80386/486/Pentium 32-bit hardware and software task management usingthe processor hardware task management facilities, Embedded or dedicated
systems using the 32 bit PC ISA architecture real-time, message based operatingsystems
PC Industry Standard Architecture hardware management including: DMAControllers, Hardware Timers, Priority Interrupt Controller Units, Serial and
Parallel Ports, Hard/Floppy disk controllers File systems (a DOS File Allocation Table compatible file system in C is
included)
You may note that throughout this book I refer to the 386, 486, and Pentium processorsas if they were all the same. If you know a fair amount about the Intel 32-bit processors,
you know that a quantum leap was made between the 286 and 386 processors. This wasthe 16- to 32-bit jump.
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
21/680
MMURTL V1.0 Page 6 of 667
The 486 and Pentium series have maintained compatibility with the 386-instruction set.Even though they have added speed, a few more instructions, and some serious "turbo"modifications, they are effectively super-386 processors. In my opinion, the nextquantum leap really hasnt been made, even though the Pentium provides 64-bit access.
My Basic Design Goals (yours may vary)
My initial desires that went into the design of my own operating system will help youunderstand what I faced, and what my end result was. If youre going to write your own, Irecommend you take care not to make it an "endless" list. You may start with my list andmodify it, or start one from scratch. You can also gather some serious insight to my wishlist in chapter 2, General Discussion and Background.
Here was my wish list:
True Multitasking- Not just task switching between programs, or even sharingthe processor between multiple programs loaded in memory, but real multi-threading - the ability for a single program to create several threads of executionthat could communicate and synchronize with each other while carrying outindividual tasks. Ill discuss some of your options, and also what I decided on, inchapters 3 (Tasking Model), and 4 (Interprocess Communications).
Real-time operation- The ability to react to outside events in real time. Thisdesign goal demanded MMURTL to be message based. The messaging systemwould be the heart of the kernel. Synchronization of the messages would be thebasis for effective CPU utilization (the Tasking Model). Real-time operation maynot even be on your list of requirements, but I think youll see the push in industry
is towards real-time systems, and for a very good reason. We, the humans, areoutside events. We want the system to react to us. This is also discussed inchapters 3 (Tasking Model), and 4 (Interprocess Communications).
Client/Server design- The ability to share services with multiple clientapplications (on the same machine or across a network). A message-basedoperating system seemed the best way to do this. I added the Request andRespond message types to the more common Send and Wait found on mostmessage based systems. Message agents could be added to any platform. If youdont have this requirement, the Request and Respond primitives could beremoved, but I dont recommend it. I discuss this in chapter 4 (InterprocessCommunications).
Common affordable hardware platform with minimal hardwarerequirements- The most common 32-bit platform in the world is the PC ISAplatform. How many do you think are out there? Millions! How much does a386SX cost these days? A little more than dirt, or maybe a little less? Of course,you may have a different platform in mind. The hardware interface may radicallychange based on your choice. I discuss this in chapter 6, The HardwareInterface.
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
22/680
MMURTL V1.0 Page 7 of 667
Flat 32-Bit Virtual Memory Model - Those of you that program on the Intelprocessors know all about segmentation and the headaches it can cause (Not tomention Expanded, Extended, LIM, UMBs, HIMEM, LOMEM,YOURMOTHERSMEM, and who knows what other memory schemes anddefinitions are out there). Computers using the Intel compatible 32-bit processors
are cheap, and most of them are grossly under used. MMURTL would use thememory paging capabilities of 386/486 processors to provide an EASY 32-bit flataddress space for each application running on the system. Chapter 5 coversmemory management, but not all the options. Many detailed books have beenwritten on schemes for memory management, and I stuck with a very simplepaged model that made use of the Intel hardware. You could go nuts here andthrow in the kitchen sink if desired.
Easy programming - The easiest way to interface to an operating system from aprocedural language, such as C or Pascal, is with a procedural interface. Aprocedural interface directly into the operating system (with no intermediatelibrary) is the easiest there is. Simple descriptive names of procedures and
structures are used throughout the system. I also wanted to maintain a small,uncluttered Applications Programming Interface (API) specification, adding onlythe necessary calls to get the job done. Power without BLOAT... the right mix ofcapability and simplicity. I would shoot for less than 200 basic public functions. Idiscuss some of your options in chapter 8, Programming Interfaces.
Protection from other programs - But not the programmer. I wanted an OSthat would prevent another program from taking the system down, yet allow usthe power to do what we wanted, if we knew how. This could be a function of the
hardware as well as the software, and the techniques may change based on yourplatform and processor of choice.
Use the CPU Instruction Set as Designed - Many languages and operating
systems ported between processors tend to ignore many of the finer capabilities ofthe target processor. I didn't want to do this. I use the stack, calling conventions,
hardware paging, and task management native to the Intel 32-bit x86 series ofprocessors. I have attempted to isolate the API from the hardware as much as
possible (for future source code portability). You may want your operating systemto run on another platform or even another processor. If it is another processor,
many of the items that I use and discuss may not apply. I take a generic look at itin chapter 6, The Hardware Interface, and chapter 8, Programming Interfaces.
Simplicity- Keep the system as simple as possible, yet powerful enough to getthe job done. We also wanted to reduce the "jargon" level and minimize thenumber of terse, archaic names and labels so often used in the computer industry.
A Brief Description of MMURTL
From my wish list above, I put together my own operating system. I will describe it toyou here so that you can see what I ended up with based on my list.
8/10/2019 8604308 Developing Your Own 32Bit Operating System MMURTL
23/680
MMURTL V1.0 Page 8 of 667
MMURTL (pronounced like the girls name Myrtle) is a 32-bit, Message based,Multitasking, Real-Time, operating system designed around the Intel 80386 and 80486processors on the PC Industry Standard Architecture (ISA) platforms. The name is anacronym for Message based MUltitasking, Real-Time, kerneL. If you dont like myacronym, make up your own! But, I warn you: Acronyms are in short supply in the
computer industry.
MMURTL is designed to run on most 32-bit ISA PCs in existence. Yes, this means itwill even run on an 80386SX with one megabyte of RAM (although I recommend 2Mb).Then again, it runs rather well on a Pentium wi