├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── _config.yml ├── bin └── .gitignore ├── images ├── First_Progress.png └── Keyboard_Input.PNG ├── link.ld ├── scripts ├── gdb.script └── run_qemu.bat └── src ├── include ├── arch │ └── x86 │ │ ├── icxxabi.h │ │ └── isr_helper.h ├── assert.h ├── basics.h ├── debugger_device.h ├── drivers │ ├── keyboard.h │ └── serial_port.h ├── libc │ ├── memory.h │ ├── queue.hpp │ ├── stdio.h │ ├── stdlib.h │ ├── syslib.h │ └── vector.hpp ├── paging.h ├── time.h ├── unpacked_page_dir_ent.h └── unpacked_page_table_ent.h ├── kernel ├── arch │ └── x86 │ │ ├── gdt.asm │ │ ├── icxxabi.cpp │ │ ├── idt.asm │ │ ├── isr.asm │ │ └── isr_helper.cpp ├── boot_utils.asm ├── debugger_device.cpp ├── descriptorTables.cpp ├── drivers │ ├── keyboard.cpp │ ├── serial_port.cpp │ └── vga_controller.cpp ├── kernel.cpp ├── libc │ ├── memory.cpp │ ├── print.cpp │ └── stdlib.cpp ├── paging.cpp ├── time.cpp ├── unpacked_page_dir_ent.cpp └── unpacked_page_table_ent.cpp └── libc ├── .gitignore ├── ELF_Types.h ├── ELF_reader ├── ELF_reader.cpp ├── test └── test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.bin 3 | *.vscode -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #NOTE: $@ is right of the :, and $< is the left 2 | MAKEFLAGS = -s #silent 3 | 4 | #locations of files 5 | OBJS_DIR := ./objects 6 | BIN_DIR := ./bin 7 | SRC_DIR := ./src/kernel 8 | INCLUDE_DIR := ./src/include 9 | 10 | #This project is made of .asm and .cpp files 11 | #Go into the SRC_DIR and lookd for any of those. 12 | #each file type needs to be located separatly because duplicate 13 | #names will mess up the search if I try to do it in one command 14 | 15 | ASM_SRC_FILES := $(wildcard $(SRC_DIR)/*.asm) $(wildcard $(SRC_DIR)/*/*.asm) $(wildcard $(SRC_DIR)/*/*/*.asm) 16 | CPP_SRC_FILES := $(wildcard $(SRC_DIR)/*.cpp) $(wildcard $(SRC_DIR)/*/*.cpp) $(wildcard $(SRC_DIR)/*/*/*.cpp) 17 | C_SRC_FILES := $(wildcard $(SRC_DIR)/*.c) $(wildcard $(SRC_DIR)/*/*.c) $(wildcard $(SRC_DIR)/*/*/*.c) 18 | 19 | #Put all the files together in one list 20 | #This is now a single list of every file that needs to be compiled together 21 | SRC_FILES := $(ASM_SRC_FILES) $(C_SRC_FILES) $(CPP_SRC_FILES) 22 | 23 | #strip the SRC_DIR/*.sufix and change to OBJ_DIR/*.o 24 | #After this, every file with .asm, .c, etc. now has a .o 25 | #This assumes for now that every file in the project is going into the same final output file 26 | #When I start building multiple output files, this will have to change somehow 27 | 28 | #OBJ_FILES := $($(notdir $(SRC_FILES)), $(OBJS_DIR), $(addsuffix .o, $(basename $(SRC_FILES)))) 29 | OBJ_FILES := $(subst $(SRC_DIR), $(OBJS_DIR), $(addsuffix .o, $(basename $(SRC_FILES)))) 30 | #OBJ_FILES := $(OBJS_DIR)/asm.o $(OBJS_DIR)/cpp.o $(OBJS_DIR)/c.o 31 | 32 | #This is what the output file will be called 33 | OUTPUT_FILE = $(BIN_DIR)/kernel.bin 34 | 35 | #flags used by both compilers 36 | COMMON_FLAGS = -g -c -O0 -ffreestanding -lgcc -fno-pic -Werror -Wall -Wextra -I$(INCLUDE_DIR) 37 | 38 | 39 | #The compiler and its flags 40 | CC = i686-elf-gcc 41 | CFLAGS = -std=gnu17 $(COMMON_FLAGS) 42 | 43 | #If I ever want to use C++ 44 | CPP = i686-elf-c++ 45 | CPPFLAGS = -fno-exceptions -std=c++17 $(COMMON_FLAGS) -fno-rtti 46 | 47 | #Linker Flags 48 | LDFLAGS = -T link.ld -ffreestanding -O0 -lgcc -nostdlib 49 | 50 | #The assembler and its flags 51 | ASM = nasm 52 | ASMFLAGS = -felf32 -F dwarf -g 53 | 54 | #GCC assembler does not need flags 55 | #ASM=i686-elf-as 56 | 57 | #.ONESHELL: # this lets everything be run in one shell, so things like "cd" work as expected 58 | 59 | build : $(OUTPUT_FILE) 60 | 61 | $(OUTPUT_FILE) : $(OBJ_FILES) 62 | $(CC) $(LDFLAGS) $(OBJ_FILES) -o $(OUTPUT_FILE) 63 | echo "Linking $(OBJ_FILES) ----------> $@" 64 | 65 | # assemble any .asm files and put them in the OBJS_DIR 66 | $(OBJS_DIR)/%.o : $(SRC_DIR)/%.asm 67 | mkdir -p $(dir $@) 68 | $(ASM) $(ASMFLAGS) $< -o $@ 69 | echo "$< -----> $@" 70 | 71 | #compile any .c files and put them in the OBJS_DIR 72 | $(OBJS_DIR)/%.o : $(SRC_DIR)/%.c 73 | mkdir -p $(dir $@) 74 | $(CC) $(CFLAGS) $< -o $@ 75 | echo "$< -----> $@" 76 | 77 | #compiile and .cpp files and put them in the OBJS_DIR 78 | $(OBJS_DIR)/%.o : $(SRC_DIR)/%.cpp 79 | mkdir -p $(dir $@) 80 | $(CPP) $(CPPFLAGS) $< -o $@ 81 | echo "$< -----> $@" 82 | 83 | files: 84 | echo "<----- .asm ----->" 85 | echo $(ASM_SRC_FILES) "\n" 86 | echo "<----- .c ----->" 87 | echo $(C_SRC_FILES) "\n" 88 | echo "<----- .cpp ----->" 89 | echo $(CPP_SRC_FILES) "\n" 90 | echo "<----- objects ----->" 91 | echo $(OBJ_FILES) "\n" 92 | echo "<----- Output File ----->" 93 | echo $(OUTPUT_FILE) 94 | 95 | #.PHONY tells make that there will never be these files 96 | .PHONY: clean print run all list files 97 | 98 | list: 99 | echo "list ---> list all options" 100 | echo "build ---> build any changes" 101 | echo "clean ---> remove all compiled files" 102 | echo "rebuild ---> clean, then build" 103 | echo "run ---> run virtual test" 104 | echo "all ---> clean, build, then run" 105 | echo "files ---> show what source files will be used when building" 106 | echo "print ---> prints a sick message" 107 | 108 | rebuild: print clean build 109 | echo "Full Clean and Make done..." 110 | 111 | all: rebuild run 112 | 113 | clean: 114 | echo "Clean up..." 115 | rm -rf $(OBJS_DIR) 116 | 117 | rm -f $(BIN_DIR)/*~ $(BIN_DIR)/*.bin 118 | 119 | print: 120 | # don't fail if figlet isn't installed 121 | if which figlet >/dev/null; then figlet -c Twilight; else echo " ##### Twilight #####"; fi 122 | 123 | run: 124 | echo "Starting QEMU" 125 | #qemu-system-i386 -kernel $(OUTPUT_FILE) -serial stdio 126 | cmd.exe /C "scripts\run_qemu.bat" 127 | 128 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | This is a project that is intended to become my every day use OS. I want to learn how to make the tools that I use everyday. It's still in the very first stages, but there are promising resultes so far. I'm not going for a Unix clone, but I will include Unix design choices that I think are good ones, and there will be just enough POSIX compliance to port some software. The future plan is to use it as a base to develope standalone applications for it, like a code IDE and web browser. Perhaps even a complier or language interpreter. 5 | 6 | ## Design Philosophy 7 | ---- 8 | This OS is for me, but I'd love for others to help and use it as well. I want Twilight to be approchable and understandable to anyone looking at it for the first time. With that in mind, I'm making a conscious effort to make things readable and not do anything super fancy. Even though it probably affects performance, I'm valuing maintainability as a higher priority. I'd also prefer to be the one to add any major features, since this is primarily a learining excersise for me. If you have a large feature that would like to add, please talk to me about it first. I'm only interested in having functionality that I would personally use. If you have made a contribution, you are more then welcome to put your name down at the bottom to make yourself known! 9 | 10 | ## Building 11 | ---- 12 | For now, Twilight is built using the cross compiler described in the [OSDev.org Cross Compiler](https://wiki.osdev.org/GCC_Cross-Compiler) section, and NASM. I'm using Visual Studio Code as the main editor and doing the compiling with the Windows Subsystem for Linux, but standalone linux works fine too. I've only done testing on Qemu and VirtualBox. You'll have to change the path in the `make run` recipe to perform testing in the manner that you want. 13 | 14 | ## **Some useful makefile commands** 15 | ---- 16 | Check out the make file to see all the options, but these are the most useful 17 | - `make clean` will clean all built files 18 | - `make build` will build all files 19 | - `make run` will start the OS with Qemu 20 | - `make files` will show you every file that is located for use in building process 21 | 22 | ## **Goals** 23 | ---- 24 | This isn't a strict order, but makes sense as a logical progression. Look in the wiki for more detail about what is already in. 25 | - Be able to boot ✔️ 26 | - Display text to the screen ✔️ 27 | - GDT ✔️ 28 | - IDT ✔️ 29 | - Exceptions ✔️ 30 | - System clock ✔️ 31 | - Keep time ✔️ 32 | - Delay/Sleep functions ❌ 33 | - Keyboard Support ✔️ 34 | - Dynamic Memory ✔️ 35 | - malloc() ✔️ 36 | - heap ✔️ 37 | - free() ✔️ 38 | - File System ❌ 39 | - ext2 ❌ 40 | - VFS ❌ 41 | - Graphics ❌ 42 | - GUI library ❌ 43 | 44 | 45 | ## **Problems** 46 | ---- 47 | #### Code 48 | - vga_controler.c needs rework. The indexing on the screen is all messed up. 49 | #### Theory 50 | - Organize file structure better 51 | * Separate CPU based things 52 | * Look at POSIX libC 53 | - Decide on Makefile process 54 | * Can't decide what is better. Single make file, or individual Makefiles per folder of source 55 | 56 | ## **Current Screenshot** 57 | ---- 58 | ![Latest Screenshot](/images/Keyboard_Input.PNG) 59 | 60 | ## **Contributors** 61 | ---- 62 | - Zane Youmans 63 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-hacker -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | *.bin -------------------------------------------------------------------------------- /images/First_Progress.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zandr0id/TwilightOS/7225a4feb82702ef1569fa6d201b8af2db210e56/images/First_Progress.png -------------------------------------------------------------------------------- /images/Keyboard_Input.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zandr0id/TwilightOS/7225a4feb82702ef1569fa6d201b8af2db210e56/images/Keyboard_Input.PNG -------------------------------------------------------------------------------- /link.ld: -------------------------------------------------------------------------------- 1 | 2 | /* i'm pretty sure this script only looks for symbols and places them in memory the way thay've been outlind below */ 3 | 4 | ENTRY(start) /* search for this symbol and make it the entry point */ 5 | phys = 0x00100000; /* the address to start at I guess? */ 6 | SECTIONS 7 | { 8 | .text phys : AT(phys) /*place this at the starting memory location */ 9 | { 10 | code = .; 11 | *(.text) 12 | *(.rodata) 13 | . = ALIGN(4096); 14 | } 15 | .data : AT(phys + (data - code)) /* place this right after the code section */ 16 | { 17 | data = .; 18 | *(.data) 19 | . = ALIGN(4096); 20 | } 21 | .bss : AT(phys + (bss - code)) 22 | { 23 | bss = .; 24 | *(.bss) 25 | . = ALIGN(4096); 26 | } 27 | end = .; 28 | } 29 | -------------------------------------------------------------------------------- /scripts/gdb.script: -------------------------------------------------------------------------------- 1 | # Run by starting qemu with "-s -S" parameters. Then run the following command: 2 | # gdb -tui -command=gdb.script 3 | target remote localhost:1234 4 | file bin/kernel.bin 5 | advance start 6 | -------------------------------------------------------------------------------- /scripts/run_qemu.bat: -------------------------------------------------------------------------------- 1 | rem Go run the qemu program with the kernel 2 | rem The windows command prompt will use the same working directory as we ran make from 3 | 4 | set QEMU="D:\Installed_Programs\qemu\qemu-system-i386.exe" 5 | 6 | %QEMU% -kernel "bin\kernel.bin" -serial stdio 7 | rem %QEMU% -kernel "bin\kernel.bin" -serial stdio 8 | -------------------------------------------------------------------------------- /src/include/arch/x86/icxxabi.h: -------------------------------------------------------------------------------- 1 | #ifndef _ICXXABI_H 2 | #define _ICXXABI_H 3 | 4 | #define ATEXIT_MAX_FUNCS 128 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | typedef unsigned uarch_t; 11 | 12 | struct atexit_func_entry_t 13 | { 14 | /* 15 | * Each member is at least 4 bytes large. Such that each entry is 12bytes. 16 | * 128 * 12 = 1.5KB exact. 17 | **/ 18 | void (*destructor_func)(void *); 19 | void *obj_ptr; 20 | void *dso_handle; 21 | }; 22 | 23 | int __cxa_atexit(void (*f)(void *), void *objptr, void *dso); 24 | void __cxa_finalize(void *f); 25 | 26 | #ifdef __cplusplus 27 | }; 28 | #endif 29 | 30 | #endif -------------------------------------------------------------------------------- /src/include/arch/x86/isr_helper.h: -------------------------------------------------------------------------------- 1 | #ifndef ISR_HELPER_H_ 2 | #define ISR_HELPER_H_ 3 | 4 | #include 5 | 6 | enum PIC_Ports 7 | { 8 | PIC0_CMD = 0x20, 9 | PIC0_DATA = 0x21, 10 | PIC1_CMD = 0xA0, 11 | PIC1_DATA = 0xA1 12 | }; 13 | 14 | //all of these are defined in the boot_utils.asm file 15 | extern "C" 16 | { 17 | //Exceptions 18 | extern void isr0(); 19 | extern void isr1(); 20 | extern void isr2(); 21 | extern void isr3(); 22 | extern void isr4(); 23 | extern void isr5(); 24 | extern void isr6(); 25 | extern void isr7(); 26 | extern void isr8(); 27 | extern void isr9(); 28 | extern void isr10(); 29 | extern void isr11(); 30 | extern void isr12(); 31 | extern void isr13(); 32 | extern void isr14(); 33 | extern void isr15(); 34 | extern void isr16(); 35 | extern void isr17(); 36 | extern void isr18(); 37 | extern void isr19(); 38 | extern void isr20(); 39 | extern void isr21(); 40 | extern void isr22(); 41 | extern void isr23(); 42 | extern void isr24(); 43 | extern void isr25(); 44 | extern void isr26(); 45 | extern void isr27(); 46 | extern void isr28(); 47 | extern void isr29(); 48 | extern void isr30(); 49 | extern void isr31(); 50 | 51 | //IRQs 52 | extern void irq0(); //timer 53 | extern void irq1(); //keyboard 54 | extern void irq2(); //don't use this one!!! 55 | extern void irq3(); 56 | extern void irq4(); 57 | extern void irq5(); 58 | extern void irq6(); 59 | extern void irq7(); 60 | extern void irq8(); 61 | extern void irq9(); 62 | extern void irq10(); 63 | extern void irq11(); 64 | extern void irq12(); 65 | extern void irq13(); 66 | extern void irq14(); 67 | extern void irq15(); 68 | extern void irq16(); 69 | } 70 | 71 | typedef void (*isr_t)(struct regs); 72 | void register_interrupt_handler(int n, isr_t handler); 73 | 74 | #endif //ISR_HELPER_H_ -------------------------------------------------------------------------------- /src/include/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef ASSERT_H_ 2 | #define ASSERT_H_ 3 | 4 | #include 5 | #include 6 | 7 | //junk to make __LINE__ print as a string 8 | #define STRINGIFY(X) STRINGIFY2(X) 9 | #define STRINGIFY2(X) #X 10 | 11 | #define CHECK( expression ) \ 12 | do { \ 13 | if(false == (expression)) \ 14 | { \ 15 | Debug_Logger::Instance()->print_string("CHECK: " #expression ", File: " __FILE__ ", Line: " STRINGIFY(__LINE__) "\n");\ 16 | while(true);\ 17 | }\ 18 | }while(false) 19 | 20 | #define ASSERT_NOT_REACHED() \ 21 | do { \ 22 | Debug_Logger::Instance()->print_string("ASSERT REACHED! File: " __FILE__ ", Line: " STRINGIFY(__LINE__) "\n");\ 23 | while(true);\ 24 | }while(false) 25 | 26 | #endif //ASSERT_H_ -------------------------------------------------------------------------------- /src/include/basics.h: -------------------------------------------------------------------------------- 1 | /* 2 | basics.h 3 | This is as low-level as it gets this is going to be only things provided by the compiler, and perhaps 4 | other universal things like structs or enums 5 | Written by Zane Youmans 6 | 10/16/19 7 | */ 8 | 9 | #ifndef BASICS_H_ 10 | #define BASICS_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #endif // BASICLIB_H_ -------------------------------------------------------------------------------- /src/include/debugger_device.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGGER_DEVICE_H_ 2 | #define DEBUGGER_DEVICE_H_ 3 | 4 | #include 5 | #include 6 | 7 | class Debug_Logger 8 | { 9 | public: 10 | static Debug_Logger * Instance(); 11 | void print_string(const char * string_to_print); 12 | void print_char(const char * c); 13 | private: 14 | Debug_Logger(); 15 | Serial_port * output; //where should output go? 16 | static Debug_Logger * instance_; 17 | }; 18 | 19 | #endif -------------------------------------------------------------------------------- /src/include/drivers/keyboard.h: -------------------------------------------------------------------------------- 1 | /* 2 | keyboard.h 3 | 4 | */ 5 | #include "../include/basics.h" 6 | 7 | #ifndef KEYBOARD_H_ 8 | #define KEYBOARD_H_ 9 | 10 | //the ports for PS/2 keyboard communication 11 | enum Keyboard_Ports 12 | { 13 | Keyboard_Encoder = 0x60, 14 | Keyboard_Controller = 0x64 15 | }; 16 | 17 | //Holds the current scancod, and the state of modifier keys 18 | typedef struct keyboard_state_buffer 19 | { 20 | bool modified_table; //got an 0xE0 scancode? 21 | bool shift_pressed; //holding shift? 22 | unsigned char scancode; 23 | }keyboard_state_buffer; 24 | 25 | //A scancode decoder table 26 | //Add more scancodes here 27 | enum Scancode_decoder_table 28 | { 29 | Keyboard_A = 0x1E, 30 | Keyboard_B = 0x30, 31 | Keyboard_C = 0x2E, 32 | Keyboard_D = 0x20, 33 | Keyboard_E = 0x12, 34 | Keyboard_F = 0x21, 35 | Keyboard_G = 0x22, 36 | Keyboard_H = 0x23, 37 | Keyboard_I = 0x17, 38 | Keyboard_J = 0x24, 39 | Keyboard_K = 0x25, 40 | Keyboard_L = 0x26, 41 | Keyboard_M = 0x32, 42 | Keyboard_N = 0x31, 43 | Keyboard_O = 0x18, 44 | Keyboard_P = 0x19, 45 | Keyboard_Q = 0x10, 46 | Keyboard_R = 0x13, 47 | Keyboard_S = 0x1F, 48 | Keyboard_T = 0x14, 49 | Keyboard_U = 0x16, 50 | Keyboard_V = 0x2F, 51 | Keyboard_W = 0x11, 52 | Keyboard_X = 0x2D, 53 | Keyboard_Y = 0x15, 54 | Keyboard_Z = 0x2C, 55 | Keyboard_SHIFT_L_PRESS = 0x2A, 56 | Keyboard_SHIFT_R_PRESS = 0x36, 57 | Keyboard_SHIFT_L_RELEASE = 0xAA, 58 | Keyboard_SHIFT_R_RELEASE = 0xB6, 59 | Keyboard_ENTER = 0x1C, 60 | Keyboard_SPACE = 0x39, 61 | Keyboard_TOGGLE = 0xE0 //not an actual key, but appears before some other scancodes 62 | }; 63 | 64 | #endif -------------------------------------------------------------------------------- /src/include/drivers/serial_port.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SERIAL_PORT_H_ 3 | #define SERIAL_PORT_H_ 4 | 5 | #include 6 | #include 7 | 8 | enum Serial_port_keywords 9 | { 10 | COM1 = 0x3F8, 11 | COM2 = 0x2F8, 12 | COM3 = 0x3E8, 13 | COM4 = 0x2E8 14 | }; 15 | 16 | class Serial_port 17 | { 18 | public: 19 | Serial_port(short port_number); //private constructor 20 | void print_string(const char * string_to_print); 21 | void print_char(const char * c); 22 | char * read(); 23 | int get_port_num(); //return the port number being used 24 | private: 25 | int com_port_num_; 26 | char rx_buff[128]; //store incoming data here 27 | }; 28 | 29 | #endif -------------------------------------------------------------------------------- /src/include/libc/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | memory.h 3 | This is the header for dynamic memory allocation functions 4 | Written by Zane Youmans 5 | 11/02/2019 6 | */ 7 | #include 8 | 9 | #ifndef MEMORY_H_ 10 | #define MEMORY_H_ 11 | 12 | enum Heap_constants 13 | { 14 | HEAP_MINIMUM_BLOCK_SIZE = 64, //Minimum size of block payload to allow splitting 15 | HEAP_ETERNAL_SIZE = 1024, //how many bytes the eternal heap is 16 | HEAP_DYNAMIC_SIZE = 16384 //how many bytes the dynamic heap is 17 | }; 18 | 19 | //this will go at the front of every malloc segment 20 | #pragma pack(2) 21 | typedef struct Heap_element_header 22 | { 23 | Heap_element_header * next; //pointer to the next header 24 | Heap_element_header * previous; //pointer to the previous header 25 | unsigned int payload_size; //number of bytes in this block 26 | bool in_use; //is this block in use? 27 | }Heap_element_header; 28 | 29 | void heap_install(); //initialize the heap 30 | void * malloc(size_t size); //memory allocate 31 | //void * malloc_eternal(size_t size); //eternal memeory allocate 32 | void free(void * loc); //memory free 33 | 34 | #endif //MEMORY_H_ -------------------------------------------------------------------------------- /src/include/libc/queue.hpp: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_H_ 2 | #define QUEUE_H_ 3 | 4 | #include 5 | #include 6 | 7 | constexpr int queue_size_unit = 8; 8 | 9 | template 10 | class Queue 11 | { 12 | public: 13 | Queue(); 14 | ~Queue(); 15 | void push(T item); 16 | T pop(); 17 | unsigned int size(); //return the size 18 | unsigned int capacity(); //return the capacity 19 | private: 20 | T * data_; 21 | void resize_up(); //get more space 22 | void resize_down(); //give up extra space 23 | unsigned int size_; //how many slots are filled 24 | unsigned int capacity_; //current max slots 25 | T operator[](int index); 26 | }; 27 | 28 | template 29 | Queue::Queue() 30 | { 31 | size_ = 0; 32 | capacity_ = queue_size_unit; 33 | data_ = new T[capacity_]; 34 | } 35 | 36 | template 37 | Queue::~Queue() 38 | { 39 | delete data_; // free the data section 40 | } 41 | 42 | //create a new data array that is larger, 43 | //and copy the old one into it 44 | template 45 | void Queue::resize_up() 46 | { 47 | //T * bigger_data = new T[capacity_ + vector_size_unit]; //make a new array that is larger 48 | T * bigger_data = new T[capacity_ + queue_size_unit]; //make a new array that is larger 49 | for (size_t i = 0; i 61 | void Queue::resize_down() 62 | { 63 | //TODO: implement me 64 | } 65 | 66 | template 67 | void Queue::push(T new_item) 68 | { 69 | if(size_ >= capacity_) 70 | { 71 | resize_up(); 72 | } 73 | 74 | data_[size_] = new_item; //put the new item at the end of the data array 75 | size_++; 76 | } 77 | 78 | template 79 | T Queue::pop() 80 | { 81 | T ret = data_[0]; 82 | for(unsigned int i = 0; i < size_ ;i++) 83 | { 84 | data_[i] = data_[i+1]; 85 | } 86 | size_--; 87 | return ret; 88 | } 89 | 90 | template 91 | unsigned int Queue::size() 92 | { 93 | return size_; 94 | } 95 | 96 | template 97 | unsigned int Queue::capacity() 98 | { 99 | return capacity_; 100 | } 101 | 102 | #endif //QUEUE_H_ -------------------------------------------------------------------------------- /src/include/libc/stdio.h: -------------------------------------------------------------------------------- 1 | /* 2 | stdio.h 3 | This header contains prototypes for functions that deal with I/o related stuff 4 | Written by Zane Youmans 5 | 10/16/19 6 | */ 7 | 8 | #ifndef STDIO_H_ 9 | #define STDIO_H_ 10 | 11 | #include 12 | 13 | //I/O related functions 14 | 15 | extern void printf(const char * format, ...); // The good ol' printf function 16 | extern void print_char(const char c);//print a single char to the next location in the screen 17 | extern void write_serial_string(const char *c); 18 | extern void write_char_serial(char c); 19 | 20 | //These must be defined here to avoid compiler warnings 21 | //It wasn't liking me trying to use Extern Inline as a function signature, and have it defined somewhere else 22 | //Just having them in the .h file solved it 23 | inline void outb(uint16_t port, uint8_t val) // write a value to a port 24 | { 25 | __asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) ); 26 | } 27 | 28 | inline uint8_t inb(uint16_t port) //read a value from a port 29 | { 30 | uint8_t ret; 31 | __asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port) ); 32 | return ret; 33 | } 34 | #endif //STDIO_H_ -------------------------------------------------------------------------------- /src/include/libc/stdlib.h: -------------------------------------------------------------------------------- 1 | //this is standard lib! 2 | // This H file will be included into the kernel, and all other files will be liked into this one. 3 | 4 | #ifndef STDLIB_H 5 | #define STDLIB_H 6 | 7 | #include 8 | 9 | //memory related functions 10 | extern void memcpy(void * dest, const void * src, int count); 11 | extern void memset(void * dest, char val, int count); 12 | extern void memsetw(void* dest, unsigned short val, int count); 13 | extern int strlen(const char* data); 14 | 15 | 16 | #endif //STDLIB_H -------------------------------------------------------------------------------- /src/include/libc/syslib.h: -------------------------------------------------------------------------------- 1 | /* 2 | syslib.h 3 | These are system level functions meant to be used by the kernel 4 | Written by Zane Youmans 5 | 10/16/19 6 | */ 7 | 8 | #ifndef SYSLIB_H 9 | #define SYSLIB_H 10 | 11 | #include 12 | 13 | // screen related functions 14 | extern void clear_screen(); //whipe out the screen 15 | 16 | extern void set_colors(char text, char back); 17 | extern void set_text_grey(); 18 | extern void set_text_blue(); 19 | extern void set_text_green(); 20 | extern void set_text_red(); 21 | 22 | // system installation functions 23 | extern void gdt_add_entry(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran); 24 | extern void gdt_install(); 25 | extern void idt_add_entry(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags); 26 | extern void idt_install(); 27 | extern void isr_install(); 28 | extern void time_install(int freq); 29 | extern void PIC_install(); 30 | extern void keyboard_install(); 31 | extern void paging_install(); 32 | 33 | //beginning of syscalls? 34 | extern unsigned int get_system_uptime(); 35 | extern char getchar(); 36 | 37 | struct regs 38 | { 39 | unsigned int gs, fs, es, ds; 40 | unsigned int edi, esi, ebp, esp ,ebx, edx, ecx, eax; 41 | unsigned int int_no, err_code; 42 | unsigned int eip, cs, eflags, useresp, ss; 43 | }; 44 | 45 | #endif //SYSLIB_H_ -------------------------------------------------------------------------------- /src/include/libc/vector.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VECTOR_H_ 2 | #define VECTOR_H_ 3 | 4 | #include 5 | #include 6 | static const int vector_size_unit = 8; //auto resize by this amount 7 | 8 | template //T will get replaced with whatever Typename is supplied 9 | class Vector 10 | { 11 | public: 12 | void push_back(T new_item); //add a new item 13 | T pop_back(); //return and remove 14 | unsigned int size(); //return the size 15 | unsigned int capacity(); //return the capacity 16 | void insert(unsigned int index, T new_item); //insert an item at a specific spot 17 | Vector(); 18 | ~Vector(); 19 | protected: 20 | T * data_; 21 | void resize_up(); //get more space 22 | void resize_down(); //give up extra space 23 | unsigned int size_; //how many slots are filled 24 | unsigned int capacity_; //current max slots 25 | T operator[](int index); 26 | }; 27 | 28 | template 29 | Vector::Vector() 30 | { 31 | size_ = 0; 32 | capacity_ = vector_size_unit; 33 | data_ = new T[capacity_]; 34 | } 35 | 36 | template 37 | Vector::~Vector() 38 | { 39 | delete data_; // free the data section 40 | } 41 | 42 | template 43 | T Vector::operator[](int index) 44 | { 45 | T ret = NULL; 46 | if (index < size_) 47 | { 48 | ret = data_[index]; 49 | } 50 | return ret; 51 | } 52 | 53 | //create a new data array that is larger, 54 | //and copy the old one into it 55 | template 56 | void Vector::resize_up() 57 | { 58 | //T * bigger_data = new T[capacity_ + vector_size_unit]; //make a new array that is larger 59 | T * bigger_data = new T[capacity_ + vector_size_unit]; //make a new array that is larger 60 | for (size_t i = 0; i 72 | void Vector::resize_down() 73 | { 74 | //TODO: implement me 75 | } 76 | 77 | template 78 | void Vector::push_back(T new_item) 79 | { 80 | if(size_ >= capacity_) 81 | { 82 | resize_up(); 83 | } 84 | 85 | data_[size_] = new_item; //put the new item at the end of the data array 86 | size_++; 87 | } 88 | 89 | template 90 | void Vector::insert(unsigned int index, T new_item) 91 | { 92 | (void)index; 93 | (void)new_item; 94 | //TODO: implement me 95 | } 96 | 97 | template 98 | T Vector::pop_back() 99 | { 100 | //return the top element, and back up one. 101 | return data_[--size_]; 102 | } 103 | 104 | template 105 | unsigned int Vector::capacity() 106 | { 107 | return capacity_; 108 | } 109 | 110 | template 111 | unsigned int Vector::size() 112 | { 113 | return size_; 114 | } 115 | 116 | #endif //VECTOR_H_ -------------------------------------------------------------------------------- /src/include/paging.h: -------------------------------------------------------------------------------- 1 | #ifndef PAGING_H_ 2 | #define PAGING_H_ 3 | 4 | #include 5 | 6 | 7 | const int FRAME_SIZE = 4096; //frames will be 4KiB each 8 | const int NUMBER_OF_FRAMES = 262144; //1 Gig worth of frames 9 | 10 | struct frame 11 | { 12 | unsigned char frames[FRAME_SIZE]; 13 | }; 14 | 15 | void * find_new_frame(); 16 | void free_frame(void * frame_to_free); 17 | 18 | #endif -------------------------------------------------------------------------------- /src/include/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | timer.h 3 | This will hold time based structs and related functions 4 | */ 5 | 6 | #ifndef TIMER_H_ 7 | #define TIMER_H_ 8 | 9 | #define BASE_FREQUENCY 1193180 10 | 11 | enum PIT_Ports 12 | { 13 | PIT_CHANNEL_0 = 0x40, 14 | PIT_CHANNEL_1 = 0x41, 15 | PIT_CHANNEL_2 = 0x42, 16 | PIT_CMD = 0x43 17 | }; 18 | 19 | typedef struct time_data 20 | { 21 | int clock_divisor; //fed to timer to 22 | unsigned int millisecs_since_poweron; //a huge spot to count all system ticks 23 | unsigned int seconds_since_poweron; //seconds 24 | unsigned int minutes_since_poweron; //minutes 25 | 26 | /*TODO: This not working for some reason 27 | time_data() 28 | { 29 | clock_divisor = 1; 30 | millisecs_since_poweron = 0; 31 | seconds_since_poweron = 0; 32 | minutes_since_poweron = 0; 33 | } 34 | 35 | void increment_time() 36 | { 37 | minutes_since_poweron++; 38 | seconds_since_poweron = millisecs_since_poweron % 100; //100 millisecs in a second 39 | minutes_since_poweron = seconds_since_poweron % 60; //60 seconds in a minute 40 | } 41 | */ 42 | 43 | }time_data; 44 | 45 | //returns the diff in millisecs of two times 46 | unsigned int time_diff(time_data future, time_data past) 47 | { 48 | return (future.millisecs_since_poweron - past.millisecs_since_poweron); 49 | } 50 | 51 | //TODO: Move into struct above 52 | void increment_time(time_data* time) 53 | { 54 | time->millisecs_since_poweron++; 55 | time->seconds_since_poweron = time->millisecs_since_poweron / 1000; //1000 millisecs in a second 56 | time->minutes_since_poweron = time->seconds_since_poweron / 60; //60 seconds in a minute 57 | } 58 | 59 | #endif -------------------------------------------------------------------------------- /src/include/unpacked_page_dir_ent.h: -------------------------------------------------------------------------------- 1 | #ifndef UNPACKED_PAGE_DIR_H_ 2 | #define UNPACKED_PAGE_DIR_H_ 3 | 4 | #include 5 | 6 | class unpacked_page_dir_ent 7 | { 8 | public: 9 | unpacked_page_dir_ent(unsigned int * addr, unsigned char extra, bool global, bool pat, bool size, bool accessed, bool cache_disable, bool write_through, bool priv, bool read_write, bool present); 10 | unpacked_page_dir_ent(unsigned int int_form); 11 | unsigned int return_as_dir_entry(); 12 | private: 13 | unsigned int * physical_page_address_; //12-31 14 | unsigned char extra_; //9-11 15 | bool global_; 16 | bool size_; 17 | bool pat_; 18 | bool accessed_; 19 | bool cache_disabled_; 20 | bool write_through_; 21 | bool priv_; //user\supervisor 22 | bool read_write_; 23 | bool present_; 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/include/unpacked_page_table_ent.h: -------------------------------------------------------------------------------- 1 | #ifndef UNPACKED_PAGE_H_ 2 | #define UNPACKED_PAGE_H_ 3 | 4 | #include 5 | 6 | class unpacked_page_table_ent 7 | { 8 | public: 9 | unpacked_page_table_ent(unsigned int * addr, unsigned char extra, bool global, bool pat, bool dirty, bool accessed, bool cache, bool write_through, bool priv, bool read_write, bool present); 10 | unpacked_page_table_ent(unsigned int int_form); 11 | unsigned int return_as_table_entry(); 12 | private: 13 | unsigned int * physical_page_address_; //12-31 14 | unsigned char extra_; //9-11 15 | bool global_; 16 | bool pat_; 17 | bool dirty_; 18 | bool accessed_; 19 | bool cache_disabled_; 20 | bool write_through_; 21 | bool priv_; //user\supervisor 22 | bool read_write_; 23 | bool present_; 24 | }; 25 | 26 | #endif -------------------------------------------------------------------------------- /src/kernel/arch/x86/gdt.asm: -------------------------------------------------------------------------------- 1 | ; code for loading GDT 2 | global gdt_flush 3 | extern gdt_ptr 4 | 5 | gdt_flush: 6 | lgdt [gdt_ptr] 7 | mov ax, 0x10 8 | mov ds, ax 9 | mov es, ax 10 | mov fs, ax 11 | mov gs, ax 12 | mov ss, ax 13 | jmp 0x08:flush2 14 | flush2: 15 | ret -------------------------------------------------------------------------------- /src/kernel/arch/x86/icxxabi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" { 4 | 5 | 6 | atexit_func_entry_t __atexit_funcs[ATEXIT_MAX_FUNCS]; 7 | uarch_t __atexit_func_count = 0; 8 | 9 | void *__dso_handle = 0; //Attention! Optimally, you should remove the '= 0' part and define this in your asm script. 10 | 11 | int __cxa_atexit(void (*f)(void *), void *objptr, void *dso) 12 | { 13 | if (__atexit_func_count >= ATEXIT_MAX_FUNCS) {return -1;}; 14 | __atexit_funcs[__atexit_func_count].destructor_func = f; 15 | __atexit_funcs[__atexit_func_count].obj_ptr = objptr; 16 | __atexit_funcs[__atexit_func_count].dso_handle = dso; 17 | __atexit_func_count++; 18 | return 0; /*I would prefer if functions returned 1 on success, but the ABI says...*/ 19 | }; 20 | 21 | void __cxa_finalize(void *f) 22 | { 23 | uarch_t i = __atexit_func_count; 24 | if (!f) 25 | { 26 | /* 27 | * According to the Itanium C++ ABI, if __cxa_finalize is called without a 28 | * function ptr, then it means that we should destroy EVERYTHING MUAHAHAHA!! 29 | * 30 | * TODO: 31 | * Note well, however, that deleting a function from here that contains a __dso_handle 32 | * means that one link to a shared object file has been terminated. In other words, 33 | * We should monitor this list (optional, of course), since it tells us how many links to 34 | * an object file exist at runtime in a particular application. This can be used to tell 35 | * when a shared object is no longer in use. It is one of many methods, however. 36 | **/ 37 | //You may insert a prinf() here to tell you whether or not the function gets called. Testing 38 | //is CRITICAL! 39 | while (i--) 40 | { 41 | if (__atexit_funcs[i].destructor_func) 42 | { 43 | /* ^^^ That if statement is a safeguard... 44 | * To make sure we don't call any entries that have already been called and unset at runtime. 45 | * Those will contain a value of 0, and calling a function with value 0 46 | * will cause undefined behaviour. Remember that linear address 0, 47 | * in a non-virtual address space (physical) contains the IVT and BDA. 48 | * 49 | * In a virtual environment, the kernel will receive a page fault, and then probably 50 | * map in some trash, or a blank page, or something stupid like that. 51 | * This will result in the processor executing trash, and...we don't want that. 52 | **/ 53 | (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); 54 | }; 55 | }; 56 | return; 57 | }; 58 | 59 | while (i--) 60 | { 61 | /* 62 | * The ABI states that multiple calls to the __cxa_finalize(destructor_func_ptr) function 63 | * should not destroy objects multiple times. Only one call is needed to eliminate multiple 64 | * entries with the same address. 65 | * 66 | * FIXME: 67 | * This presents the obvious problem: all destructors must be stored in the order they 68 | * were placed in the list. I.e: the last initialized object's destructor must be first 69 | * in the list of destructors to be called. But removing a destructor from the list at runtime 70 | * creates holes in the table with unfilled entries. 71 | * Remember that the insertion algorithm in __cxa_atexit simply inserts the next destructor 72 | * at the end of the table. So, we have holes with our current algorithm 73 | * This function should be modified to move all the destructors above the one currently 74 | * being called and removed one place down in the list, so as to cover up the hole. 75 | * Otherwise, whenever a destructor is called and removed, an entire space in the table is wasted. 76 | **/ 77 | if (__atexit_funcs[i].destructor_func == f) 78 | { 79 | /* 80 | * Note that in the next line, not every destructor function is a class destructor. 81 | * It is perfectly legal to register a non class destructor function as a simple cleanup 82 | * function to be called on program termination, in which case, it would not NEED an 83 | * object This pointer. A smart programmer may even take advantage of this and register 84 | * a C function in the table with the address of some structure containing data about 85 | * what to clean up on exit. 86 | * In the case of a function that takes no arguments, it will simply be ignore within the 87 | * function itself. No worries. 88 | **/ 89 | (*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr); 90 | __atexit_funcs[i].destructor_func = 0; 91 | 92 | /* 93 | * Notice that we didn't decrement __atexit_func_count: this is because this algorithm 94 | * requires patching to deal with the FIXME outlined above. 95 | **/ 96 | }; 97 | }; 98 | }; 99 | 100 | }; 101 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/idt.asm: -------------------------------------------------------------------------------- 1 | ; code for loading interrupts descriptor table 2 | global idt_load 3 | extern idt_ptr 4 | 5 | idt_load: 6 | lidt[idt_ptr] 7 | sti 8 | ret -------------------------------------------------------------------------------- /src/kernel/arch/x86/isr.asm: -------------------------------------------------------------------------------- 1 | ; actual ISR function calls 2 | global isr0 3 | global isr1 4 | global isr2 5 | global isr3 6 | global isr4 7 | global isr5 8 | global isr6 9 | global isr7 10 | global isr8 11 | global isr9 12 | global isr10 13 | global isr11 14 | global isr12 15 | global isr13 16 | global isr14 17 | global isr15 18 | global isr16 19 | global isr17 20 | global isr18 21 | global isr19 22 | global isr20 23 | global isr21 24 | global isr22 25 | global isr23 26 | global isr24 27 | global isr25 28 | global isr26 29 | global isr27 30 | global isr28 31 | global isr29 32 | global isr30 33 | global isr31 34 | 35 | global irq0 36 | global irq1 37 | global irq2 38 | global irq3 39 | global irq4 40 | global irq5 41 | global irq6 42 | global irq7 43 | global irq8 44 | global irq9 45 | global irq10 46 | global irq11 47 | global irq12 48 | global irq13 49 | global irq14 50 | global irq15 51 | 52 | 53 | ;if an ISR does not provide an error code on it's own, just push a 0 to the stack to keep them all the same 54 | isr0: ;divide by 0 exception 55 | push byte 0 ; a dummy error code 56 | push byte 0 ; which exeption it is 57 | jmp exception_common_stub ; a common function for all ISRs 58 | isr1: ;Debug exception 59 | push byte 0 ; a dummy error code 60 | push byte 1 ; which exeption it is 61 | jmp exception_common_stub ; a common function for all ISRs 62 | isr2: ;non maskable interrupt exception 63 | push byte 0 ; a dummy error code 64 | push byte 2 ; which exeption it is 65 | jmp exception_common_stub ; a common function for all ISRs 66 | isr3: ;Breakpoint exception 67 | push byte 0 ; a dummy error code 68 | push byte 3 ; which exeption it is 69 | jmp exception_common_stub ; a common function for all ISRs 70 | isr4: ;Into Detected Overflow exception 71 | push byte 0 ; a dummy error code 72 | push byte 4 ; which exeption it is 73 | jmp exception_common_stub ; a common function for all ISRs 74 | isr5: ;Out Of bounds exception 75 | push byte 0 ; a dummy error code 76 | push byte 5 ; which exeption it is 77 | jmp exception_common_stub ; a common function for all ISRs 78 | isr6: ;Invalide Opcode exception 79 | push byte 0 ; a dummy error code 80 | push byte 6 ; which exeption it is 81 | jmp exception_common_stub ; a common function for all ISRs 82 | isr7: ;No Coprocessor exception 83 | push byte 0 ; a dummy error code 84 | push byte 7 ; which exeption it is 85 | jmp exception_common_stub ; a common function for all ISRs 86 | isr8: ;Double Fault exception. Already pushes a fault code 87 | push byte 8 ; which exeption it is 88 | jmp exception_common_stub ; a common function for all ISRs 89 | isr9: ;Coprocessor Segment Overrun Exception 90 | push byte 0 ; a dummy error code 91 | push byte 9 ; which exeption it is 92 | jmp exception_common_stub ; a common function for all ISRs 93 | isr10: ;Bad TSS exception. Already pushes a fault code 94 | push byte 10 ; which exeption it is 95 | jmp exception_common_stub ; a common function for all ISRs 96 | isr11: ;Segment Not Present Exception. Already pushes a fault code 97 | push byte 11 ; which exeption it is 98 | jmp exception_common_stub ; a common function for all ISRs 99 | isr12: ;Stack Fault Exception. Already pushes a fault code 100 | push byte 12 ; which exeption it is 101 | jmp exception_common_stub ; a common function for all ISRs 102 | isr13: ;General Protection Fault Exception. Already pushes a fault code 103 | push byte 13 ; which exeption it is 104 | jmp exception_common_stub ; a common function for all ISRs 105 | isr14: ;Page Fault Exception. Already pushes a fault code 106 | push byte 14 ; which exeption it is 107 | jmp exception_common_stub ; a common function for all ISRs 108 | isr15: ;Unknown Interrupt Exception 109 | push byte 0 ; a dummy error code 110 | push byte 15 ; which exeption it is 111 | jmp exception_common_stub ; a common function for all ISRs 112 | isr16: ;Coprocessor Fault Exception 113 | push byte 0 ; a dummy error code 114 | push byte 16 ; which exeption it is 115 | jmp exception_common_stub ; a common function for all ISRs 116 | isr17: ;Alignment Check Exception 117 | push byte 0 ; a dummy error code 118 | push byte 17 ; which exeption it is 119 | jmp exception_common_stub ; a common function for all ISRs 120 | isr18: ;Machine Check Exception 121 | push byte 0 ; a dummy error code 122 | push byte 18 ; which exeption it is 123 | jmp exception_common_stub ; a common function for all ISRs 124 | isr19: ;Reserved Exception 125 | push byte 0 ; a dummy error code 126 | push byte 19 ; which exeption it is 127 | jmp exception_common_stub ; a common function for all ISRs 128 | isr20: ;Reserved Exception 129 | push byte 0 ; a dummy error code 130 | push byte 20 ; which exeption it is 131 | jmp exception_common_stub ; a common function for all ISRs 132 | isr21: ;Reserved Exception 133 | push byte 0 ; a dummy error code 134 | push byte 21 ; which exeption it is 135 | jmp exception_common_stub ; a common function for all ISRs 136 | isr22: ;Reserved Exception 137 | push byte 0 ; a dummy error code 138 | push byte 22 ; which exeption it is 139 | jmp exception_common_stub ; a common function for all ISRs 140 | isr23: ;Reserved Exception 141 | push byte 0 ; a dummy error code 142 | push byte 23 ; which exeption it is 143 | jmp exception_common_stub ; a common function for all ISRs 144 | isr24: ;Reserved Exception 145 | push byte 0 ; a dummy error code 146 | push byte 24 ; which exeption it is 147 | jmp exception_common_stub ; a common function for all ISRs 148 | isr25: ;Reserved Exception 149 | push byte 0 ; a dummy error code 150 | push byte 25 ; which exeption it is 151 | jmp exception_common_stub ; a common function for all ISRs 152 | isr26: ;Reserved Exception 153 | push byte 0 ; a dummy error code 154 | push byte 26 ; which exeption it is 155 | jmp exception_common_stub ; a common function for all ISRs 156 | isr27: ;Reserved Exception 157 | push byte 0 ; a dummy error code 158 | push byte 27 ; which exeption it is 159 | jmp exception_common_stub ; a common function for all ISRs 160 | isr28: ;Reserved Exception 161 | push byte 0 ; a dummy error code 162 | push byte 28 ; which exeption it is 163 | jmp exception_common_stub ; a common function for all ISRs 164 | isr29: ;Reserved Exception 165 | push byte 0 ; a dummy error code 166 | push byte 29 ; which exeption it is 167 | jmp exception_common_stub ; a common function for all ISRs 168 | isr30: ;Reserved Exception 169 | push byte 0 ; a dummy error code 170 | push byte 30 ; which exeption it is 171 | jmp exception_common_stub ; a common function for all ISRs 172 | isr31: ;Reserved Exception 173 | push byte 0 ; a dummy error code 174 | push byte 31 ; which exeption it is 175 | jmp exception_common_stub ; a common function for all ISRs 176 | 177 | irq0: 178 | push byte 0 179 | push byte 32 180 | jmp irq_common_stub 181 | irq1: 182 | push byte 1 183 | push byte 33 184 | jmp irq_common_stub 185 | irq2: 186 | push byte 2 187 | push byte 34 188 | jmp irq_common_stub 189 | irq3: 190 | push byte 3 191 | push byte 35 192 | jmp irq_common_stub 193 | irq4: 194 | push byte 4 195 | push byte 36 196 | jmp irq_common_stub 197 | irq5: 198 | push byte 5 199 | push byte 37 200 | jmp irq_common_stub 201 | irq6: 202 | push byte 6 203 | push byte 38 204 | jmp irq_common_stub 205 | irq7: 206 | push byte 7 207 | push byte 39 208 | jmp irq_common_stub 209 | irq8: 210 | push byte 8 211 | push byte 40 212 | jmp irq_common_stub 213 | irq9: 214 | push byte 9 215 | push byte 41 216 | jmp irq_common_stub 217 | irq10: 218 | push byte 10 219 | push byte 42 220 | jmp irq_common_stub 221 | irq11: 222 | push byte 11 223 | push byte 43 224 | jmp irq_common_stub 225 | irq12: 226 | push byte 12 227 | push byte 44 228 | jmp irq_common_stub 229 | irq13: 230 | push byte 13 231 | push byte 45 232 | jmp irq_common_stub 233 | irq14: 234 | push byte 14 235 | push byte 46 236 | jmp irq_common_stub 237 | irq15: 238 | push byte 15 239 | push byte 47 240 | jmp irq_common_stub 241 | 242 | extern fault_handler 243 | extern irq_handler 244 | 245 | extern TEST_MSG 246 | 247 | exception_common_stub: ; the place all ISRs will jump to 248 | pusha 249 | push ds 250 | push es 251 | push fs 252 | push gs 253 | mov ax, 0x10 ; Load the Kernel Data Segment descriptor! 254 | mov ds, ax 255 | mov es, ax 256 | mov fs, ax 257 | mov gs, ax 258 | mov eax, esp ; Push us the stack pointer 259 | push eax 260 | mov eax, fault_handler 261 | call eax ; A special call, preserves the 'eip' register 262 | pop eax 263 | pop gs 264 | pop fs 265 | pop es 266 | pop ds 267 | popa 268 | add esp, 8 ; Cleans up the pushed error code and pushed ISR number 269 | iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP! 270 | 271 | irq_common_stub: 272 | pusha 273 | push ds 274 | push es 275 | push fs 276 | push gs 277 | mov ax, 0x10 ; Load the Kernel Data Segment descriptor! 278 | mov ds, ax 279 | mov es, ax 280 | mov fs, ax 281 | mov gs, ax 282 | mov eax, esp ; Push us the stack pointer 283 | push eax 284 | mov eax, irq_handler 285 | ;call TEST_MSG 286 | call eax ; A special call, preserves the 'eip' register 287 | pop eax 288 | pop gs 289 | pop fs 290 | pop es 291 | pop ds 292 | popa 293 | add esp, 8 ; Cleans up the pushed error code and pushed ISR number 294 | iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP! -------------------------------------------------------------------------------- /src/kernel/arch/x86/isr_helper.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | isr_t interrupt_handlers[256]; 8 | //static Debug_Logger * logger = Debug_Logger::Instance(); 9 | 10 | void register_interrupt_handler(int n, isr_t handler) 11 | { 12 | interrupt_handlers[n] = handler; 13 | } 14 | 15 | //TODO: Remove this eventually 16 | void TEST_MSG() 17 | { 18 | printf("TEST\n\0"); 19 | } 20 | 21 | //links each ISR number with it's respctive function 22 | void isr_install() 23 | { 24 | //Exception handlers 25 | printf("Install ISR\n"); 26 | idt_add_entry(0, (unsigned)isr0, 0x08, 0x8E); 27 | idt_add_entry(1, (unsigned)isr1, 0x08, 0x8E); 28 | idt_add_entry(2, (unsigned)isr2, 0x08, 0x8E); 29 | idt_add_entry(3, (unsigned)isr3, 0x08, 0x8E); 30 | idt_add_entry(4, (unsigned)isr4, 0x08, 0x8E); 31 | idt_add_entry(5, (unsigned)isr5, 0x08, 0x8E); 32 | idt_add_entry(6, (unsigned)isr6, 0x08, 0x8E); 33 | idt_add_entry(7, (unsigned)isr7, 0x08, 0x8E); 34 | idt_add_entry(8, (unsigned)isr8, 0x08, 0x8E); 35 | idt_add_entry(9, (unsigned)isr9, 0x08, 0x8E); 36 | idt_add_entry(10, (unsigned)isr10, 0x08, 0x8E); 37 | idt_add_entry(11, (unsigned)isr11, 0x08, 0x8E); 38 | idt_add_entry(12, (unsigned)isr12, 0x08, 0x8E); 39 | idt_add_entry(13, (unsigned)isr13, 0x08, 0x8E); 40 | idt_add_entry(14, (unsigned)isr14, 0x08, 0x8E); 41 | idt_add_entry(15, (unsigned)isr15, 0x08, 0x8E); 42 | idt_add_entry(16, (unsigned)isr16, 0x08, 0x8E); 43 | idt_add_entry(17, (unsigned)isr17, 0x08, 0x8E); 44 | idt_add_entry(18, (unsigned)isr18, 0x08, 0x8E); 45 | idt_add_entry(19, (unsigned)isr19, 0x08, 0x8E); 46 | idt_add_entry(20, (unsigned)isr20, 0x08, 0x8E); 47 | idt_add_entry(21, (unsigned)isr21, 0x08, 0x8E); 48 | idt_add_entry(22, (unsigned)isr22, 0x08, 0x8E); 49 | idt_add_entry(23, (unsigned)isr23, 0x08, 0x8E); 50 | idt_add_entry(24, (unsigned)isr24, 0x08, 0x8E); 51 | idt_add_entry(25, (unsigned)isr25, 0x08, 0x8E); 52 | idt_add_entry(26, (unsigned)isr26, 0x08, 0x8E); 53 | idt_add_entry(27, (unsigned)isr27, 0x08, 0x8E); 54 | idt_add_entry(28, (unsigned)isr28, 0x08, 0x8E); 55 | idt_add_entry(29, (unsigned)isr29, 0x08, 0x8E); 56 | idt_add_entry(30, (unsigned)isr30, 0x08, 0x8E); 57 | idt_add_entry(31, (unsigned)isr31, 0x08, 0x8E); 58 | 59 | //start of IRQs 60 | printf("Install IRQs\n"); 61 | idt_add_entry(32, (unsigned)irq0, 0x08, 0x8E); //timer 62 | idt_add_entry(33, (unsigned)irq1, 0x08, 0x8E); //keyboard 63 | //TODO: perhaps catch ISR 2 and produce error? 64 | //idt_add_entry(34, (unsigned)irq2, 0x08, 0x8E); // This one should never be assigned 65 | idt_add_entry(35, (unsigned)irq3, 0x08, 0x8E); // COM 2 66 | idt_add_entry(36, (unsigned)irq4, 0x08, 0x8E); // COM 1 67 | idt_add_entry(37, (unsigned)irq5, 0x08, 0x8E); // LPT2 68 | idt_add_entry(38, (unsigned)irq6, 0x08, 0x8E); // Floppy disk 69 | //TODO: react to spurious irq 7 70 | idt_add_entry(39, (unsigned)irq7, 0x08, 0x8E); // LPT1/Spurious 71 | idt_add_entry(40, (unsigned)irq8, 0x08, 0x8E); // CMOS real-time 72 | idt_add_entry(41, (unsigned)irq9, 0x08, 0x8E); // SCSI/NIC 73 | idt_add_entry(42, (unsigned)irq10, 0x08, 0x8E); // SCSI/NIC 74 | idt_add_entry(43, (unsigned)irq11, 0x08, 0x8E); // SCSI/NIC 75 | idt_add_entry(44, (unsigned)irq12, 0x08, 0x8E); // Mouse 76 | idt_add_entry(45, (unsigned)irq13, 0x08, 0x8E); // FPU 77 | idt_add_entry(46, (unsigned)irq14, 0x08, 0x8E); // Primary ATA 78 | idt_add_entry(47, (unsigned)irq15, 0x08, 0x8E); // Secondary ATA 79 | 80 | } 81 | 82 | void PIC_install() 83 | { 84 | //program the PIC 85 | printf("Reprogramming PIC\n"); 86 | outb(PIC0_CMD,0x11); 87 | outb(PIC1_CMD,0x11); 88 | outb(PIC0_DATA,0x20); 89 | outb(PIC1_DATA,0x28); 90 | outb(PIC0_DATA,0x04); 91 | outb(PIC1_DATA,0x02); 92 | outb(PIC0_DATA,0x01); 93 | outb(PIC1_DATA,0x01); 94 | 95 | //set all interrupts on 96 | //TODO: turn all of and turn on as needed 97 | outb(PIC0_DATA,0x0); 98 | outb(PIC1_DATA,0x0); 99 | } 100 | 101 | const char*exception_messages[] = 102 | { 103 | "Divide By Zero exception\0", 104 | "Debug exception\0", 105 | "non maskable interrupt exception\0", 106 | "Breakpoint exception\0", 107 | "Into Detected Overflow exception\0", 108 | "Out Of bounds exception\0", 109 | "Invalide Opcode exception\0", 110 | "No Coprocessor exception\0", 111 | "Double Fault exception\0", 112 | "Coprocessor Segment Overrun Exception\0", 113 | "Bad TSS exception\0", 114 | "Segment Not Present Exception\0", 115 | "Stack Fault Exception\0", 116 | "General Protection Fault Exception\0", 117 | "Page Fault Exception\0", 118 | "Unknown Interrupt Exception\0", 119 | "Coprocessor Fault Exception\0", 120 | "Alignment Check Exception\0", 121 | "Machine Check Exception\0", 122 | "Reserved Exception\0", 123 | "Reserved Exception\0", 124 | "Reserved Exception\0", 125 | "Reserved Exception\0", 126 | "Reserved Exception\0", 127 | "Reserved Exception\0", 128 | "Reserved Exception\0", 129 | "Reserved Exception\0", 130 | "Reserved Exception\0", 131 | "Reserved Exception\0", 132 | "Reserved Exception\0", 133 | "Reserved Exception\0", 134 | "Reserved Exception\0" 135 | }; 136 | 137 | extern "C" 138 | { 139 | void fault_handler(struct regs *r) 140 | { 141 | printf("***EXCEPTION***\n"); 142 | if (r->int_no < 32) 143 | { 144 | printf("%s\n",exception_messages[r->int_no]); 145 | printf("Error Code: %d\n",r->err_code); 146 | printf("Exeption Number: %d\n",r->int_no); 147 | printf("SYSTEM HALT..."); 148 | while(true); 149 | } 150 | else 151 | { 152 | printf("Unknown Exception.....oops...."); 153 | } 154 | } 155 | 156 | void irq_handler(struct regs * r) 157 | { 158 | unsigned int irq_number = r->int_no; 159 | 160 | //acknowledge the PIC so it will send more interrupts 161 | 162 | if (irq_number >= 40) 163 | { 164 | outb(0xA0,0x20); 165 | } 166 | outb(0x20,0x20); 167 | 168 | if (interrupt_handlers[irq_number] != 0) 169 | { 170 | isr_t handler = interrupt_handlers[irq_number]; 171 | handler(*r); 172 | } 173 | } 174 | 175 | } -------------------------------------------------------------------------------- /src/kernel/boot_utils.asm: -------------------------------------------------------------------------------- 1 | [BITS 32] ;32 bit protected mode 2 | GLOBAL start ; start is the entry point 3 | 4 | start: 5 | mov esp, _stack_top ;move the stack pointer to where I want it 6 | jmp stublet ;jump down to the portion that does most of the actual work 7 | 8 | 9 | ;The multiboot header is used by grub to detect an OS 10 | ;not totally sure what everything means yet, but i'll just get it working and not worry about it for right now. 11 | 12 | ALIGN 4 ;this moves us to the next 4 btye section of memory 13 | multi_boot: 14 | 15 | ;These are just declaring macros 16 | MULTIBOOT_PAGE_ALIGN equ 1<<0 ;set some bits that are required 17 | MULTIBOOT_MEMORY_INFO equ 1<<1 18 | MULTIBOOT_AOUT_KLUDG equ 1<<16 19 | MULTIBOOT_HEADER_MAGIC equ 0x1BADB002 20 | 21 | ;OR them all together to make some more macros 22 | MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDG 23 | MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) 24 | 25 | EXTERN code, bss, end ;these portions will be defined by the linker script 26 | 27 | ;this is actually setting the data into memory 28 | dd MULTIBOOT_HEADER_MAGIC ;magic number goes first so GRUB will recognize the OS 29 | dd MULTIBOOT_HEADER_FLAGS ;flags come next 30 | dd MULTIBOOT_CHECKSUM ;checksum at the end 31 | 32 | dd multi_boot 33 | dd code 34 | dd bss 35 | dd end 36 | dd start 37 | 38 | stublet: 39 | ;make the call to main right here!!! 40 | extern kernel_main ;kernel_main is defined in another file 41 | call kernel_main ;CALL THAT KERNEL!!! 42 | 43 | jmp $ ;endless loop 44 | 45 | SECTION .bss 46 | ALIGN 16 47 | _stack_bot: 48 | resb 16834 ;reserving an 16K stack 49 | _stack_top: ;the stack grows backwards, so the "top" is actually set at the end -------------------------------------------------------------------------------- /src/kernel/debugger_device.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | Debug_Logger * Debug_Logger::instance_ = nullptr; 4 | 5 | Debug_Logger::Debug_Logger() 6 | { 7 | output = new Serial_port(COM1); 8 | } 9 | 10 | Debug_Logger * Debug_Logger::Instance() 11 | { 12 | if(nullptr == instance_) 13 | { 14 | instance_ = new Debug_Logger; 15 | } 16 | return instance_; 17 | } 18 | 19 | void Debug_Logger::print_string(const char * string_to_print) 20 | { 21 | output->print_string(string_to_print); 22 | } 23 | 24 | void Debug_Logger::print_char(const char * c) 25 | { 26 | output->print_char(c); 27 | } -------------------------------------------------------------------------------- /src/kernel/descriptorTables.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This is the layout for the Global Descriptor Table 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #pragma pack(2) 9 | struct gdt_entry 10 | { 11 | unsigned short limit_low; 12 | unsigned short base_low; 13 | unsigned char base_middle; 14 | unsigned char access; 15 | unsigned char granularity; 16 | unsigned char base_high; 17 | }; 18 | 19 | #pragma pack(2) 20 | struct gdt_ptr 21 | { 22 | unsigned short limit; 23 | unsigned int base; 24 | }; 25 | 26 | #pragma pack(2) 27 | struct idt_entry 28 | { 29 | unsigned short base_low; 30 | unsigned short sel; 31 | unsigned char always0; //always needs to be 0 32 | unsigned char flags; 33 | unsigned short base_hi; 34 | }; 35 | 36 | #pragma pack(2) 37 | struct idt_ptr 38 | { 39 | unsigned short limit; 40 | unsigned int base; 41 | }; 42 | 43 | struct gdt_entry gdt[3]; 44 | struct gdt_ptr gdt_ptr; 45 | 46 | struct idt_entry idt[256]; 47 | struct idt_ptr idt_ptr; 48 | 49 | extern "C" 50 | { 51 | extern void gdt_flush(); 52 | extern void idt_load(); 53 | } 54 | 55 | //sets up one segment of the GDT with the settings you give it 56 | void gdt_add_entry(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran) 57 | { 58 | //descriptor table address 59 | gdt[num].base_low = (base & 0xFFFF); 60 | gdt[num].base_middle = (base >> 16) & 0x0F; 61 | gdt[num].base_high = (base >> 24) & 0xFF; 62 | gdt[num].limit_low = (limit & 0xFFFF); 63 | gdt[num].granularity = ((limit >> 16) & 0x0F); 64 | gdt[num].granularity |= (gran & 0xF0); 65 | gdt[num].access = access; 66 | } 67 | void gdt_install() 68 | { 69 | printf("Install GDT\n"); 70 | //the two ends of memory for the gdt 71 | //the base is the starting location, 72 | //and the limiit is 3 entrys minus 1 73 | gdt_ptr.limit = (sizeof(struct gdt_entry) * 3) - 1; 74 | gdt_ptr.base = (unsigned int)&gdt; 75 | 76 | gdt_add_entry(0,0,0,0,0); 77 | 78 | gdt_add_entry(1,0,0xFFFFFFFF, 0x9A, 0xCF); 79 | 80 | gdt_add_entry(2,0,0xFFFFFFFF, 0x92, 0xCF); 81 | 82 | gdt_flush(); 83 | } 84 | void idt_add_entry(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags) 85 | { 86 | idt[num].always0 = 0; 87 | idt[num].base_low = (unsigned short) (base & 0xFFFF); 88 | idt[num].base_hi = (unsigned short) (base >> 16) & 0xFFFF; 89 | idt[num].sel = sel; 90 | idt[num].flags = flags; 91 | } 92 | void idt_install() 93 | { 94 | printf("Install IDT\n"); 95 | idt_ptr.limit = (sizeof(struct idt_entry) * 256)-1; 96 | memset((unsigned char*)&idt,0,sizeof(idt)); //0 out all that memory 97 | idt_ptr.base = (unsigned int)&idt; 98 | idt_load(); 99 | } -------------------------------------------------------------------------------- /src/kernel/drivers/keyboard.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static keyboard_state_buffer keyboard_state; 9 | static Queue keyboard_buffer; 10 | 11 | //Look at the keyboard state buffer and decide the character to send 12 | char decode_to_ascii() 13 | { 14 | char return_char; 15 | switch (keyboard_state.scancode) 16 | { 17 | case Keyboard_A: 18 | { 19 | if (true == keyboard_state.shift_pressed) 20 | { 21 | return_char = 'A'; 22 | } 23 | else 24 | { 25 | return_char ='a'; 26 | } 27 | break; 28 | } 29 | case Keyboard_B: 30 | { 31 | if (true == keyboard_state.shift_pressed) 32 | { 33 | return_char = 'B'; 34 | } 35 | else 36 | { 37 | return_char = 'b'; 38 | } 39 | break; 40 | } 41 | case Keyboard_C: 42 | { 43 | if (true == keyboard_state.shift_pressed) 44 | { 45 | return_char = 'C'; 46 | } 47 | else 48 | { 49 | return_char = 'c'; 50 | } 51 | break; 52 | } 53 | case Keyboard_D: 54 | { 55 | if (true == keyboard_state.shift_pressed) 56 | { 57 | return_char = 'D'; 58 | } 59 | else 60 | { 61 | return_char = 'd'; 62 | } 63 | break; 64 | } 65 | case Keyboard_E: 66 | { 67 | if (true == keyboard_state.shift_pressed) 68 | { 69 | return_char = 'E'; 70 | } 71 | else 72 | { 73 | return_char = 'e'; 74 | } 75 | break; 76 | } 77 | case Keyboard_F: 78 | { 79 | if (true == keyboard_state.shift_pressed) 80 | { 81 | return_char = 'F'; 82 | } 83 | else 84 | { 85 | return_char = 'f'; 86 | } 87 | break; 88 | } 89 | case Keyboard_G: 90 | { 91 | if (true == keyboard_state.shift_pressed) 92 | { 93 | return_char = 'G'; 94 | } 95 | else 96 | { 97 | return_char = 'g'; 98 | } 99 | break; 100 | } 101 | case Keyboard_H: 102 | { 103 | if (true == keyboard_state.shift_pressed) 104 | { 105 | return_char = 'H'; 106 | } 107 | else 108 | { 109 | return_char = 'h'; 110 | } 111 | break; 112 | } 113 | case Keyboard_I: 114 | { 115 | if (true == keyboard_state.shift_pressed) 116 | { 117 | return_char = 'I'; 118 | } 119 | else 120 | { 121 | return_char = 'i'; 122 | } 123 | break; 124 | } 125 | case Keyboard_J: 126 | { 127 | if (true == keyboard_state.shift_pressed) 128 | { 129 | return_char = 'J'; 130 | } 131 | else 132 | { 133 | return_char = 'j'; 134 | } 135 | break; 136 | } 137 | case Keyboard_K: 138 | { 139 | if (true == keyboard_state.shift_pressed) 140 | { 141 | return_char = 'K'; 142 | } 143 | else 144 | { 145 | return_char = 'k'; 146 | } 147 | break; 148 | } 149 | case Keyboard_L: 150 | { 151 | if (true == keyboard_state.shift_pressed) 152 | { 153 | return_char = 'L'; 154 | } 155 | else 156 | { 157 | return_char = 'l'; 158 | } 159 | break; 160 | } 161 | case Keyboard_M: 162 | { 163 | if (true == keyboard_state.shift_pressed) 164 | { 165 | return_char = 'M'; 166 | } 167 | else 168 | { 169 | return_char = 'm'; 170 | } 171 | break; 172 | } 173 | case Keyboard_N: 174 | { 175 | if (true == keyboard_state.shift_pressed) 176 | { 177 | return_char = 'N'; 178 | } 179 | else 180 | { 181 | return_char = 'n'; 182 | } 183 | break; 184 | } 185 | case Keyboard_O: 186 | { 187 | if (true == keyboard_state.shift_pressed) 188 | { 189 | return_char = 'O'; 190 | } 191 | else 192 | { 193 | return_char = 'o'; 194 | } 195 | break; 196 | } 197 | case Keyboard_P: 198 | { 199 | if (true == keyboard_state.shift_pressed) 200 | { 201 | return_char = 'P'; 202 | } 203 | else 204 | { 205 | return_char = 'p'; 206 | } 207 | break; 208 | } 209 | case Keyboard_Q: 210 | { 211 | if (true == keyboard_state.shift_pressed) 212 | { 213 | return_char = 'Q'; 214 | } 215 | else 216 | { 217 | return_char = 'q'; 218 | } 219 | break; 220 | } 221 | case Keyboard_R: 222 | { 223 | if (true == keyboard_state.shift_pressed) 224 | { 225 | return_char = 'R'; 226 | } 227 | else 228 | { 229 | return_char = 'r'; 230 | } 231 | break; 232 | } 233 | case Keyboard_S: 234 | { 235 | if (true == keyboard_state.shift_pressed) 236 | { 237 | return_char = 'S'; 238 | } 239 | else 240 | { 241 | return_char = 's'; 242 | } 243 | break; 244 | } 245 | case Keyboard_T: 246 | { 247 | if (true == keyboard_state.shift_pressed) 248 | { 249 | return_char = 'T'; 250 | } 251 | else 252 | { 253 | return_char = 't'; 254 | } 255 | break; 256 | } 257 | case Keyboard_U: 258 | { 259 | if (true == keyboard_state.shift_pressed) 260 | { 261 | return_char = 'U'; 262 | } 263 | else 264 | { 265 | return_char = 'u'; 266 | } 267 | break; 268 | } 269 | case Keyboard_V: 270 | { 271 | if (true == keyboard_state.shift_pressed) 272 | { 273 | return_char = 'V'; 274 | } 275 | else 276 | { 277 | return_char = 'v'; 278 | } 279 | break; 280 | } 281 | case Keyboard_W: 282 | { 283 | if (true == keyboard_state.shift_pressed) 284 | { 285 | return_char = 'W'; 286 | } 287 | else 288 | { 289 | return_char = 'w'; 290 | } 291 | break; 292 | } 293 | case Keyboard_X: 294 | { 295 | if (true == keyboard_state.shift_pressed) 296 | { 297 | return_char = 'X'; 298 | } 299 | else 300 | { 301 | return_char = 'x'; 302 | } 303 | break; 304 | } 305 | case Keyboard_Y: 306 | { 307 | if (true == keyboard_state.shift_pressed) 308 | { 309 | return_char = 'Y'; 310 | } 311 | else 312 | { 313 | return_char = 'y'; 314 | } 315 | break; 316 | } 317 | case Keyboard_Z: 318 | { 319 | if (true == keyboard_state.shift_pressed) 320 | { 321 | return_char = 'Z'; 322 | } 323 | else 324 | { 325 | return_char = 'z'; 326 | } 327 | break; 328 | } 329 | case Keyboard_SPACE: 330 | { 331 | return_char = ' '; 332 | break; 333 | } 334 | case Keyboard_ENTER: 335 | { 336 | return_char = '\n'; 337 | break; 338 | } 339 | default: 340 | { 341 | return_char = 0x00; 342 | break; 343 | } 344 | } 345 | // printf("%x %x ",keyboard_state.scancode,keyboard_state.shift_pressed); 346 | keyboard_state.scancode = 0; 347 | return return_char; 348 | } 349 | 350 | //every time a key gets hit, grab the scancode and update the keyboard state buffer 351 | void keyboard_callback() 352 | { 353 | unsigned char scancode = inb(Keyboard_Encoder); 354 | //printf("Scancode: %x\n", scancode); 355 | if (Keyboard_TOGGLE == scancode) 356 | { 357 | keyboard_state.modified_table = true; 358 | } 359 | else if ((Keyboard_SHIFT_L_PRESS == scancode) || (Keyboard_SHIFT_R_PRESS == scancode)) 360 | { 361 | keyboard_state.shift_pressed = true; 362 | } 363 | else if ((Keyboard_SHIFT_L_RELEASE == scancode) || (Keyboard_SHIFT_R_RELEASE == scancode)) 364 | { 365 | keyboard_state.shift_pressed = false; 366 | } 367 | else 368 | { 369 | keyboard_state.scancode = scancode; 370 | } 371 | keyboard_buffer.push(decode_to_ascii()); 372 | } 373 | 374 | char getchar() 375 | { 376 | if(keyboard_buffer.size() > 0) 377 | { 378 | return keyboard_buffer.pop(); 379 | } 380 | else 381 | { 382 | return 0x00; 383 | } 384 | } 385 | 386 | void keyboard_install() 387 | { 388 | printf("Install Keyboard\n\0 "); 389 | register_interrupt_handler(33, (isr_t)keyboard_callback); 390 | } 391 | -------------------------------------------------------------------------------- /src/kernel/drivers/serial_port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | 7 | Serial_port::Serial_port(short port_number) 8 | { 9 | com_port_num_ = port_number; //save the port number, because reasons 10 | 11 | outb(port_number+1,0x00);//disable interrupts 12 | outb(port_number+3,0x80);// 13 | outb(port_number+0,0x03); 14 | outb(port_number+1,0x00); 15 | outb(port_number+3,0x03); 16 | outb(port_number+2,0xC7); 17 | outb(port_number+4,0x0B); 18 | } 19 | 20 | int Serial_port::get_port_num() 21 | { 22 | return com_port_num_; 23 | } 24 | 25 | void Serial_port::print_char(const char * c) 26 | { 27 | while((inb(com_port_num_+5) & 0x20) == 0); //make sure the TX buffer is empty 28 | outb(com_port_num_, *c); //print the char 29 | 30 | } 31 | 32 | void Serial_port::print_string(const char * string_to_print) 33 | { 34 | while(*string_to_print != '\0') //check if it's the end of the string 35 | { 36 | print_char(string_to_print); 37 | string_to_print++; //next char 38 | } 39 | } 40 | 41 | 42 | 43 | char * Serial_port::read() 44 | { 45 | return rx_buff; 46 | } 47 | 48 | void serial_port_callback() 49 | { 50 | //TODO: implement me!!! 51 | } 52 | 53 | void serial_tx_install() 54 | { 55 | //register_interrupt_handler(36, (isr_t)serial_port_callback); 56 | } 57 | 58 | -------------------------------------------------------------------------------- /src/kernel/drivers/vga_controller.cpp: -------------------------------------------------------------------------------- 1 | //vga_controller 2 | //Has functions that control the screen 3 | //It knows how to place chars on the screen 4 | //change text color 5 | //move the cursor 6 | 7 | #include 8 | #include 9 | 10 | enum vga_color { 11 | VGA_COLOR_BLACK = 0, 12 | VGA_COLOR_BLUE = 1, 13 | VGA_COLOR_GREEN = 2, 14 | VGA_COLOR_CYAN = 3, 15 | VGA_COLOR_RED = 4, 16 | VGA_COLOR_MAGENTA = 5, 17 | VGA_COLOR_BROWN = 6, 18 | VGA_COLOR_LIGHT_GREY = 7, 19 | VGA_COLOR_DARK_GREY = 8, 20 | VGA_COLOR_LIGHT_BLUE = 9, 21 | VGA_COLOR_LIGHT_GREEN = 10, 22 | VGA_COLOR_LIGHT_CYAN = 11, 23 | VGA_COLOR_LIGHT_RED = 12, 24 | VGA_COLOR_LIGHT_MAGENTA = 13, 25 | VGA_COLOR_LIGHT_BROWN = 14, 26 | VGA_COLOR_WHITE = 15, 27 | }; 28 | 29 | static const size_t vga_width = 80; 30 | static const size_t vga_height = 25; 31 | 32 | size_t coursor_x = 0; 33 | size_t coursor_y = 0; 34 | 35 | uint8_t text_colors = VGA_COLOR_LIGHT_GREY; 36 | int8_t background = VGA_COLOR_BLACK; 37 | 38 | uint16_t * screen_buffer = (uint16_t *)0xB8000;//location of screen memory 39 | 40 | void set_colors(char text, char back) 41 | { 42 | text_colors = text; 43 | background = back; 44 | } 45 | 46 | void enable_cursor(uint8_t cursor_start, uint8_t cursor_end) 47 | { 48 | outb(0x3D4, 0x0A); 49 | outb(0x3D5, (inb(0x3D5) & 0xC0) | cursor_start); 50 | 51 | outb(0x3D4, 0x0B); 52 | outb(0x3D5, (inb(0x3D5) & 0xE0) | cursor_end); 53 | } 54 | 55 | void update_cursor(int x, int y) 56 | { 57 | uint16_t pos = (x* vga_width) + y; 58 | 59 | outb(0x3D4, 0x0F); 60 | outb(0x3D5, (uint8_t) (pos & 0xFF)); 61 | outb(0x3D4, 0x0E); 62 | outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF)); 63 | } 64 | 65 | //blends the char with the color bits that are needed for vga 66 | uint16_t format_char_data(char c) 67 | { 68 | uint8_t colors = (background << 4) | text_colors; 69 | 70 | uint16_t colored_char = ((uint16_t) colors << 8 | (uint16_t) c); 71 | return colored_char; 72 | } 73 | 74 | //places a single character at the specified location 75 | void place_char_at_location(char c, size_t x, size_t y) 76 | { 77 | screen_buffer[(x * vga_width) + y] = format_char_data(c); //put the char at the location 78 | } 79 | 80 | void scroll_screen() 81 | { 82 | for(size_t i = 0;i vga_width-1) || (c == '\n')) 95 | { 96 | if (coursor_x >= vga_height-1) 97 | { 98 | scroll_screen(); 99 | } 100 | else 101 | { 102 | coursor_x++; 103 | } 104 | 105 | coursor_y=0; 106 | if (c != '\n') 107 | { 108 | place_char_at_location(c,coursor_x, coursor_y); 109 | } 110 | } 111 | else if (coursor_x > vga_height-1) 112 | { 113 | scroll_screen(); 114 | place_char_at_location(c,coursor_x, coursor_y); 115 | } 116 | else 117 | { 118 | place_char_at_location(c,coursor_x, coursor_y); 119 | } 120 | coursor_y++; 121 | update_cursor(coursor_x,coursor_y); 122 | } 123 | 124 | //goes through the entire screen and puts in blank spaces 125 | void clear_screen() 126 | { 127 | enable_cursor(0,25); 128 | update_cursor(0,0); 129 | unsigned short c = format_char_data(' '); 130 | for (size_t i = 0; i < vga_width * vga_height; i++) 131 | { 132 | screen_buffer[i] = c; 133 | } 134 | coursor_x = 0; 135 | coursor_y = 0; 136 | } 137 | 138 | void set_text_red() 139 | { 140 | set_colors(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); 141 | } 142 | 143 | void set_text_green() 144 | { 145 | set_colors(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK); 146 | } 147 | 148 | void set_text_blue() 149 | { 150 | set_colors(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_BLACK); 151 | } 152 | 153 | void set_text_grey() 154 | { 155 | set_colors(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); 156 | } 157 | -------------------------------------------------------------------------------- /src/kernel/kernel.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This is the kernel main! This is the entry point into the system 3 | It calls all the functions needed for the initial processor setup 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include //malloc 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | //#define ALL_TESTS 17 | 18 | //#define INSTA_FAIL 19 | //#define MALLOC_TEST 20 | //#define PRINTF_TEST 21 | //#define SERIAL_TEST 22 | //#define PAGING_TEST 23 | //#define VECTOR_TEST 24 | //#define TIME_TEST 25 | 26 | #ifdef ALL_TESTS 27 | #define TIME_TEST 28 | #define INSTA_FAIL 29 | #define MALLOC_TEST 30 | #define PRINTF_TEST 31 | #define SERIAL_TEST 32 | #define PAGING_TEST 33 | #define VECTOR_TEST 34 | #endif 35 | 36 | //Forward declare this as Extern C so it can be called from Assembly code 37 | extern "C" 38 | { 39 | void kernel_main(void); 40 | void __cxa_pure_virtual(){}; // needed for pure virtual functions 41 | } 42 | 43 | void kernel_main(void) 44 | { 45 | clear_screen(); 46 | set_text_red(); 47 | 48 | //print a really sweet message 49 | //Twilight is running! 50 | printf( 51 | " _____ _ _ _ _ _ \n" 52 | "|_ _|_ _(_) (_) __ _| |__ | |_ \n" 53 | " | | \\ \\ /\\ / / | | |/ _` | '_ \\| __| \n" 54 | " | | \\ V V /| | | | (_| | | | | |_ \n" 55 | " |_| \\_/\\_/ |_|_|_|\\__, |_| |_|\\__| \n" 56 | " |___/ \n"); 57 | 58 | //install the heap 59 | heap_install(); 60 | 61 | //begin paging 62 | paging_install(); 63 | 64 | //reprogram the PIC 65 | PIC_install(); 66 | 67 | //set the Global Descriptor Table 68 | gdt_install(); 69 | 70 | //set the Interrupt Descriptor Table 71 | idt_install(); 72 | 73 | //fill the IDT with interrupt functions 74 | isr_install(); 75 | 76 | //install the keyboard interrupt 77 | keyboard_install(); 78 | 79 | //start the system clock 80 | time_install(1000); //1000hz 81 | 82 | printf("\n"); 83 | 84 | //printf test 85 | #ifdef PRINTF_TEST 86 | printf("PRINTF_TEST\n"); 87 | printf("Int: %d Char: %c Hex: %x \nOct: %o Str: %s \n",-85,'R',47893,128,"Hello"); 88 | printf("END PRINTF_TEST\n"); 89 | #endif 90 | set_text_green(); 91 | 92 | //dynamic memory test 93 | #ifdef MALLOC_TEST 94 | //turn on MALLOC_DEBUG for this 95 | printf("Malloc Test: \n"); 96 | void * number1 = malloc(30); 97 | void * number2 = malloc(90); 98 | void * number3 = malloc(120); 99 | free(number1); 100 | //void * number3 = malloc(130); 101 | void * number4 = malloc(40); 102 | free(number2); 103 | free(number3); 104 | free(number4); 105 | //free(number3); 106 | printf("END MALLOC_TEST\n"); 107 | #endif 108 | 109 | #ifdef SERIAL_TEST 110 | //write_serial_string("This is testing the serial port!\n"); 111 | printf("SERIAL_TEST\n"); 112 | Debug_Logger::Instance()->print_string("Hello\n"); 113 | Debug_Logger::Instance()->print_string("This is testing the serial port\n"); 114 | printf("look in the terminal...\n"); 115 | printf("END SERIAL_TEST\n"); 116 | #endif 117 | 118 | #ifdef VECTOR_TEST 119 | printf("VECTOR_TEST\n"); 120 | Vector vec; 121 | vec.push_back(5); 122 | vec.push_back(6); 123 | vec.push_back(10); 124 | int test1 = vec.pop_back(); 125 | int test2 = vec.pop_back(); 126 | int test3 = vec.pop_back(); 127 | printf("%d %d %d\n",test1,test2,test3); 128 | printf("END VECTOR_TEST\n"); 129 | 130 | printf("QUEUE_TEST\n"); 131 | Queue queue; 132 | printf("size: %d\n",queue.size()); 133 | queue.push(12); 134 | queue.push(-9); 135 | queue.push(5); 136 | printf("%d ",queue.pop()); 137 | printf("%d ",queue.pop()); 138 | printf("%d ",queue.pop()); 139 | printf("size: %d\n",queue.size()); 140 | printf("END QUEUE_TEST\n"); 141 | #endif 142 | 143 | #ifdef PAGING_TEST 144 | printf("PAGING_TEST\n"); 145 | auto * new_page = find_new_frame(); 146 | printf("%x\n",new_page); 147 | auto * new_page2 = find_new_frame(); 148 | printf("%x\n",new_page2); 149 | free_frame(new_page); 150 | auto * new_page3 = find_new_frame(); 151 | printf("%x\n",new_page3); 152 | printf("END PAGING_TEST\n"); 153 | #endif 154 | 155 | //exception test 156 | #ifdef INSTA_FAIL 157 | printf("FAIL_TEST\n"); 158 | int test = 0; 159 | __asm volatile ("div %b0" : "+a"(test)); 160 | #endif 161 | printf("DONE\n"); 162 | 163 | while(true) 164 | { 165 | #ifdef TIME_TEST 166 | unsigned int sys_time = get_system_uptime(); 167 | if (0 == (sys_time % 1000)) 168 | { 169 | printf("Millisecs since poweron: %d\n\0",(sys_time/1000)); //TODO: make time accessable everywhere 170 | } 171 | #endif 172 | 173 | //unsigned char * temp = (unsigned char *)get_last_character(); 174 | char c = getchar(); 175 | //auto add = &c; 176 | if (0x00 != c) 177 | { 178 | //printf("%d ", temp); 179 | print_char(c); 180 | } 181 | } 182 | } -------------------------------------------------------------------------------- /src/kernel/libc/memory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //#define MALLOC_DEBUG 5 | 6 | //static Heap kernel_heap; 7 | static Heap_element_header * kernel_heap_start = (Heap_element_header * )0x100000; //up to 0x104000 8 | 9 | int amount_of_free_heap() 10 | { 11 | int free_heap = 0; 12 | Heap_element_header * current_header = kernel_heap_start; 13 | while(true) 14 | { 15 | if(false == current_header->in_use) 16 | { 17 | free_heap += current_header->payload_size; 18 | } 19 | if(nullptr != current_header->next) 20 | { 21 | current_header = current_header->next; 22 | } 23 | else 24 | { 25 | break; 26 | } 27 | } 28 | return free_heap; 29 | } 30 | 31 | void merge_neighbor_blocks(void * loc) 32 | { 33 | //assume the neighbors are in use 34 | bool next_in_use = true; 35 | bool prev_in_use = true; 36 | 37 | Heap_element_header * current_header = (Heap_element_header *)(loc) - sizeof(Heap_element_header); 38 | //look at both neighbors and see if they are in use 39 | if (nullptr != current_header->next) 40 | { 41 | next_in_use = current_header->next->in_use; 42 | } 43 | if (nullptr != current_header->previous) 44 | { 45 | prev_in_use = current_header->previous->in_use; 46 | } 47 | 48 | //both neighbors are free to merge 49 | //point the previous neighbor to point to after the next neighbor 50 | //thus combining all three 51 | /* 52 | -------- --------- -------- ------------- 53 | | prev |------>|current|------>| next | ---->| next->next | 54 | -------- --------- -------- ^ ------------- 55 | |______________________________________| 56 | */ 57 | 58 | if((false == next_in_use) && (false == prev_in_use)) 59 | { 60 | current_header->previous->payload_size += (current_header->next->payload_size +current_header->payload_size + (2 * sizeof(Heap_element_header))); 61 | current_header->next->next->previous = current_header->previous; 62 | current_header->previous->next = current_header->next->next; 63 | #ifdef MALLOC_DEBUG 64 | printf("merge across\n"); 65 | #endif 66 | } 67 | 68 | //only the previous neighbor is free 69 | //point previous neighbor to currnet neighbor.next 70 | //only combine first two 71 | /* 72 | -------- --------- -------- 73 | | prev |------>|current|------>| next | ----> 74 | -------- --------- ^ -------- 75 | |_______________________| 76 | */ 77 | else if((true == next_in_use) && (false == prev_in_use)) 78 | { 79 | current_header->previous->payload_size += (current_header->payload_size + sizeof(Heap_element_header)); 80 | current_header->next->previous = current_header->previous; 81 | current_header->previous->next = current_header->next; 82 | #ifdef MALLOC_DEBUG 83 | printf("merge up\n"); 84 | #endif 85 | 86 | } 87 | 88 | //only the only the next neighbor is free 89 | //point current.next to after next neighbor 90 | //and combine the second two 91 | /* 92 | -------- --------- -------- 93 | | prev |------>|current|------>| next | ----> 94 | -------- --------- -------- ^ 95 | |_______________________| 96 | */ 97 | else if ((false == next_in_use) && (true == prev_in_use)) 98 | { 99 | current_header->payload_size += (current_header->next->payload_size + sizeof(Heap_element_header)); 100 | current_header->next->next->previous = current_header; 101 | current_header->next = current_header->next->next; 102 | #ifdef MALLOC_DEBUG 103 | printf("merge down\n"); 104 | #endif 105 | } 106 | else 107 | { 108 | //nothing to do... 109 | } 110 | 111 | } 112 | 113 | void * malloc(size_t size) 114 | { 115 | 116 | if (size < HEAP_MINIMUM_BLOCK_SIZE) 117 | { 118 | size = HEAP_MINIMUM_BLOCK_SIZE; 119 | } 120 | unsigned int total_allocation_size = size + sizeof(Heap_element_header); 121 | 122 | #ifdef MALLOC_DEBUG 123 | printf("Before Malloc %d : %d\n",total_allocation_size, amount_of_free_heap()); 124 | #endif 125 | //printf("Allocation size: %d\n", total_allocation_size); 126 | 127 | Heap_element_header * current_header = kernel_heap_start; 128 | 129 | //jump from one header to the next until we find one that is not in use 130 | while(true) 131 | { 132 | //check if the block is unused, and large enough for our need 133 | if((false==current_header->in_use) && (current_header->payload_size >= size)) 134 | { 135 | break; 136 | } 137 | 138 | if(nullptr != current_header->next) 139 | { 140 | current_header = current_header->next; // move to the next header 141 | } 142 | else 143 | { 144 | return nullptr; 145 | } 146 | } 147 | 148 | //We already know the block is big enough to use, but if it's big enough to split then we should... 149 | //By "big enough", I mean we can get at least a minimum sized block out of it. 150 | if(current_header->payload_size >= ((size+HEAP_MINIMUM_BLOCK_SIZE)+sizeof(Heap_element_header))) 151 | { 152 | // printf("Split a block\n"); 153 | Heap_element_header * new_header = (current_header + total_allocation_size); 154 | new_header->in_use = false; 155 | new_header->next = current_header->next; 156 | new_header->previous = current_header; 157 | new_header->payload_size = (current_header->payload_size - total_allocation_size); 158 | 159 | current_header->next=new_header; 160 | current_header->payload_size = size; 161 | } 162 | 163 | //make note that this block is now being used 164 | current_header->in_use = true; 165 | 166 | #ifdef MALLOC_DEBUG 167 | printf("After Malloc %d : %d\n",total_allocation_size, amount_of_free_heap()); 168 | #endif 169 | return (current_header + sizeof(Heap_element_header)); 170 | } 171 | 172 | void free(void * loc) 173 | { 174 | Heap_element_header * to_free = (Heap_element_header *)(loc) - sizeof(Heap_element_header); 175 | #ifdef MALLOC_DEBUG 176 | int size = to_free->payload_size; 177 | printf("Before Free %d : %d\n",size, amount_of_free_heap()); 178 | #endif 179 | 180 | to_free->in_use = false; 181 | merge_neighbor_blocks(loc); 182 | 183 | #ifdef MALLOC_DEBUG 184 | printf("After Free %d : %d\n",size, amount_of_free_heap()); 185 | #endif 186 | } 187 | 188 | void * operator new (size_t size) 189 | { 190 | return malloc(size); 191 | } 192 | 193 | void * operator new[] (size_t size) 194 | { 195 | return malloc(size); 196 | } 197 | 198 | void operator delete (void* ptr) 199 | { 200 | free(ptr); 201 | } 202 | 203 | void operator delete (void* ptr, size_t size) 204 | { 205 | size = size; 206 | free(ptr); 207 | } 208 | 209 | void heap_install() 210 | { 211 | // Heap_element_header * heap_top = kernal_heap_start; 212 | printf("Install Heap\n"); 213 | kernel_heap_start->in_use = false; 214 | kernel_heap_start->payload_size = (HEAP_DYNAMIC_SIZE - sizeof(Heap_element_header)); 215 | kernel_heap_start->next = nullptr; 216 | kernel_heap_start->previous = nullptr; 217 | } -------------------------------------------------------------------------------- /src/kernel/libc/print.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | //converts a number to a base of choice 5 | char *convert(unsigned int num, int base) 6 | { 7 | static char Representation[]= "0123456789ABCDEF"; 8 | static char buffer[50]; 9 | char *ptr; 10 | 11 | ptr = &buffer[49]; 12 | *ptr = '\0'; 13 | 14 | do 15 | { 16 | *--ptr = Representation[num%base]; 17 | num /= base; 18 | }while(num != 0); 19 | return(ptr); 20 | } 21 | 22 | //straight print until \0 is hit 23 | void print_until_null(const char * data) 24 | { 25 | while (*data != '\0') 26 | { 27 | print_char(*data); 28 | data++; 29 | } 30 | } 31 | 32 | void printf( const char * format, ...) 33 | { 34 | const char * traverse = format; 35 | int i; 36 | char *s; 37 | 38 | va_list arg; 39 | va_start(arg, format); 40 | 41 | while(*traverse != '\0') 42 | { 43 | if(*traverse == '%') 44 | { 45 | traverse++; 46 | switch (*traverse) 47 | { 48 | case 'c': i = va_arg(arg, int); 49 | print_until_null((const char *)&i); 50 | break; 51 | 52 | case 'd': i = va_arg(arg, int); 53 | if(i<0) 54 | { 55 | i=-i; 56 | print_until_null("-\0"); 57 | } 58 | print_until_null(convert(i,10)); 59 | break; 60 | 61 | case 'o': i = va_arg(arg,unsigned int); 62 | print_until_null(convert(i,8)); 63 | break; 64 | 65 | case 's': s = va_arg(arg,char *); 66 | print_until_null(s); 67 | break; 68 | 69 | case 'x': i = va_arg(arg,unsigned int); 70 | print_until_null(convert(i,16)); 71 | break; 72 | 73 | default: 74 | break; 75 | } 76 | } 77 | else if(*traverse == '\t') 78 | { 79 | print_until_null(" \0"); 80 | } 81 | else 82 | { 83 | print_char(*traverse); 84 | } 85 | traverse++; 86 | } 87 | va_end(arg); 88 | } 89 | 90 | -------------------------------------------------------------------------------- /src/kernel/libc/stdlib.cpp: -------------------------------------------------------------------------------- 1 | 2 | //this is where the functions unique to stdlib will be implemented 3 | 4 | #include 5 | 6 | //takes to chars as locations and copies one to another 7 | void memcpy(void * dest, const void * src, int count) 8 | { 9 | const char * src_ptr = (const char *)src; 10 | char * dest_ptr = (char*)dest; 11 | for(;count > 0;count--) 12 | { 13 | *dest_ptr++ = *src_ptr++; 14 | } 15 | } 16 | 17 | //takes a char location and a char value to place count number of slots 18 | void memset(void * dest, char val, int count) 19 | { 20 | char *temp = (char *)dest; 21 | 22 | for (; count>0; count--) 23 | { 24 | *temp = val; 25 | temp+=sizeof(char); 26 | } 27 | } 28 | 29 | //same as above but with shorts 30 | void memsetw(void* dest, unsigned short val, int count) 31 | { 32 | unsigned short *temp = (unsigned short *)dest; 33 | for (; count>0; count--) 34 | { 35 | *temp = val; 36 | temp+=sizeof(short); 37 | } 38 | } 39 | 40 | //returns the length of the string 41 | //a \0 denotes the end of a string 42 | //make sure it's added at the end of string literals to print 43 | int strlen(const char* data) 44 | { 45 | int len = 0; 46 | while(data[len] != '\0') 47 | { 48 | len++; 49 | } 50 | return len; 51 | } 52 | -------------------------------------------------------------------------------- /src/kernel/paging.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //#define DEBUG 6 | 7 | static frame * frame_field = (frame *)0x40000000; //the start of the frame field is at the 1 gig mark, and is 1 gig in size 8 | 9 | //TODO: make the magic number a const somehow.... 10 | static unsigned int frame_field_bitmap[8192]; //this is how many ints can fit in the frame_field 11 | 12 | void paging_install() 13 | { 14 | printf("Install Paging\n"); 15 | for (int i=0;i<8192;i++) 16 | { 17 | frame_field_bitmap[i] = 0; //zero out the whole bitmap 18 | } 19 | } 20 | 21 | void * find_new_frame() 22 | { 23 | int major_index = -1; 24 | int minor_index = -1; 25 | for (int i=0;i<8192;i++) //TODO: magic number no bueno 26 | { 27 | if(frame_field_bitmap[i] != 0xFFFFFFFF) //this section is not full and must have free spot 28 | { 29 | unsigned int bitmap_cpy = frame_field_bitmap[i]; 30 | #ifdef DEBUG 31 | printf("Start: %x\n", bitmap_cpy); 32 | #endif 33 | int tmp_index = 0; 34 | while(bitmap_cpy & 0x80000000) //see if the MSB is not a zero 35 | { 36 | tmp_index++; 37 | bitmap_cpy = bitmap_cpy << 1; 38 | } 39 | minor_index = tmp_index; //the number if ints into the bitmap 40 | major_index = i; //how many bits into the current 41 | 42 | frame_field_bitmap[major_index] = (frame_field_bitmap[major_index] | (1<<(31-minor_index))); //mark that frame taken in the bitmask 43 | #ifdef DEBUG 44 | printf("End: %x\n", frame_field_bitmap[major_index]); 45 | printf("Mjr: %d Mnr: %d\n",major_index,minor_index); 46 | #endif 47 | int bitmask_index = ((sizeof(unsigned int)*major_index) + minor_index); //get the number of bits into the bitmap of a hole 48 | return (frame_field + bitmask_index); // return the address of the free frame! 49 | } 50 | } 51 | ASSERT_NOT_REACHED(); //if we got here, something went wrong! 52 | } 53 | 54 | void free_frame(void * frame_to_free) 55 | { 56 | 57 | CHECK((unsigned int)frame_to_free >= (unsigned int)frame_field); 58 | 59 | unsigned int * bits = &frame_field_bitmap[((frame *)frame_to_free - frame_field) / (sizeof(unsigned int) * 8)]; //find the index into the bitmap 60 | unsigned int mask = 1U << (31 - ((frame *)frame_to_free - frame_field) % (sizeof(unsigned int) * 8)); //find the specific bit to flip 61 | *bits &= ~mask; //and flip it 62 | } 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/kernel/time.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | static time_data time; // the master system clock, for now 7 | 8 | void timer_callback()//struct regs *r) 9 | { 10 | increment_time(&time); 11 | } 12 | 13 | unsigned int get_system_uptime() 14 | { 15 | return time.millisecs_since_poweron; 16 | } 17 | 18 | void time_install(int freq) 19 | { 20 | register_interrupt_handler(32, (isr_t)timer_callback); 21 | 22 | printf("Install Timer at %dHz\n\0",freq); 23 | int divisor = 1193180 / freq; 24 | 25 | //the top and bottom bytes of the divisor must be sent separately 26 | char low = (char)(divisor & 0xFF); 27 | char high = (char)((divisor >> 8) & 0xFF); 28 | 29 | //set PIT channel 0 to use the new divisor 30 | outb(PIT_CMD,0x36); 31 | outb(PIT_CHANNEL_0,low); 32 | outb(PIT_CHANNEL_0,high); 33 | } 34 | -------------------------------------------------------------------------------- /src/kernel/unpacked_page_dir_ent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unpacked_page_dir_ent::unpacked_page_dir_ent(unsigned int * addr, unsigned char extra, bool global, bool pat, bool size, bool accessed, 4 | bool cache, bool write_through, bool priv, bool read_write, bool present) 5 | { 6 | physical_page_address_ = addr; //(int *)((int)addr | (0<<11)); //zero out bits 0-11 7 | extra_ = extra; 8 | global_ = global; 9 | pat_ = pat; 10 | size_ = size; 11 | accessed_ = accessed; 12 | cache_disabled_ = cache; 13 | write_through_ = write_through; 14 | priv_ = priv; 15 | read_write_ = read_write; 16 | present_ = present; 17 | } 18 | 19 | //break down the input into its bytes and fill in the members 20 | unpacked_page_dir_ent::unpacked_page_dir_ent(unsigned int int_form ) 21 | { 22 | physical_page_address_ = (unsigned int *)(int_form | 0xFFFFF000); //align to 4KiB 23 | extra_ = (unsigned char)((int_form | 0x00000E00) >> 9); 24 | global_ = (bool)(int_form | 0x00000100); 25 | size_ = (bool)(int_form | 0x00000080); 26 | pat_ = (bool)(int_form | 0x00000040); 27 | accessed_ = (bool)(int_form | 0x00000020); 28 | cache_disabled_ = (bool)(int_form | 0x00000010); 29 | write_through_ = (bool)(int_form | 0x00000008); 30 | priv_ = (bool)(int_form | 0x00000004); 31 | read_write_ = (bool)(int_form | 0x00000002); 32 | present_ = (bool)(int_form | 0x00000001); 33 | } 34 | 35 | //OR everything together to make a single into table entry 36 | unsigned int unpacked_page_dir_ent::return_as_dir_entry() 37 | { 38 | int entry = ((unsigned int)physical_page_address_ | (((int)extra_) << 9) | (((int)global_) << 8) | (((int)pat_) << 7) | (((int)size_) << 6) | (((int)accessed_) << 5) | 39 | (((int)cache_disabled_) << 4) | (((int)write_through_) << 3) | (((int)priv_) <<2) | (((int)read_write_) <<1) | (int)present_); 40 | return entry; 41 | } -------------------------------------------------------------------------------- /src/kernel/unpacked_page_table_ent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | unpacked_page_table_ent::unpacked_page_table_ent(unsigned int * addr, unsigned char extra, bool global, bool pat, bool dirty, bool accessed, 5 | bool cache, bool write_through, bool priv, bool read_write, bool present) 6 | { 7 | physical_page_address_ = addr; 8 | extra_ = extra; 9 | global_ = global; 10 | pat_ = pat; 11 | dirty_ = dirty; 12 | accessed_ = accessed; 13 | cache_disabled_ = cache; 14 | write_through_ = write_through; 15 | priv_ = priv; 16 | read_write_ = read_write; 17 | present_ = present; 18 | 19 | CHECK(((unsigned int)physical_page_address_ & 0xFFF) == 0); 20 | } 21 | 22 | //break down the input into its bytes and fill in the members 23 | unpacked_page_table_ent::unpacked_page_table_ent(unsigned int int_form ) 24 | { 25 | physical_page_address_ = (unsigned int *)(int_form & 0xFFFFF000); //align to 4KiB 26 | extra_ = (unsigned char)((int_form & 0x00000E00) >> 9); 27 | global_ = (bool)(int_form & 0x00000100); 28 | pat_ = (bool)(int_form & 0x00000080); 29 | dirty_ = (bool)(int_form & 0x00000040); 30 | accessed_ = (bool)(int_form & 0x00000020); 31 | cache_disabled_ = (bool)(int_form & 0x00000010); 32 | write_through_ = (bool)(int_form & 0x00000008); 33 | priv_ = (bool)(int_form & 0x00000004); 34 | read_write_ = (bool)(int_form & 0x00000002); 35 | present_ = (bool)(int_form & 0x00000001); 36 | } 37 | 38 | //OR everything together to make a single into table entry 39 | unsigned int unpacked_page_table_ent::return_as_table_entry() 40 | { 41 | int entry = ((int)physical_page_address_ | (((int)extra_) << 9) | (((int)global_) << 8) | (((int)pat_) << 7) | (((int)dirty_) << 6) | (((int)accessed_) << 5) | 42 | (((int)cache_disabled_) << 4) | (((int)write_through_) << 3) | (((int)priv_) << 2) | (((int)read_write_) << 1) | (int)present_); 43 | return entry; 44 | } -------------------------------------------------------------------------------- /src/libc/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zandr0id/TwilightOS/7225a4feb82702ef1569fa6d201b8af2db210e56/src/libc/.gitignore -------------------------------------------------------------------------------- /src/libc/ELF_Types.h: -------------------------------------------------------------------------------- 1 | /* 2 | elfreader.h 3 | This is the header file to support the reading and extraction of ELF files. 4 | Heavy use of this file was used. http://www.skyfree.org/linux/references/ELF_Format.pdf 5 | This is the actual specs. http://sco.com/developers/gabi/latest/ch4.sheader.html 6 | 7 | Written by Zane Youmans 8 | 10/18/19 9 | */ 10 | 11 | #ifndef ELF_TYPES_H_ 12 | #define ELF_TYPES_H_ 13 | 14 | //Rename some data types so it's easier to see what they do 15 | /* 16 | typedef unsigned int Elf32_Addr; // Program address, 4 bytes, aligned at 4 bytes 17 | typedef unsigned short Elf32_Half; // Medium integer, 2 bytes, aligned at 2 bytes 18 | typedef unsigned int Elf32_Offset; // File Offset, 4 bytes, aligned at 4 bytes 19 | typedef int Elf32_Sword; //Signed int, 4 bytes, aligned at 4 bytes 20 | typedef unsigned int ELF32_Word; // Unsigned int, 4 bytes, aligned at 4 bytes 21 | */ 22 | 23 | /* 24 | ELF files are made of some combonation of these sections 25 | Only the File_Header is garantueed to be present and at the top 26 | The others can be in any order, or not present at all 27 | */ 28 | 29 | ///////////////////////////////////////////////////////////////////////////////////////////////// FILE HEADER 30 | 31 | //Make a nice union for the Magic Number Header 32 | struct Magic_numbers_struct 33 | { 34 | unsigned char magic_1; 35 | unsigned char magic_2; 36 | unsigned char magic_3; 37 | unsigned char magic_4; 38 | unsigned char file_class; //how this file can be used 39 | unsigned char data_encoding; //how to read the file 40 | unsigned char file_version; //better be CURRENT 41 | unsigned char padding; //start of 0 padding 42 | unsigned short size_of_ident; //should be 16? 43 | }; 44 | 45 | typedef union 46 | { 47 | Magic_numbers_struct fields; 48 | unsigned char bytes[16] ; 49 | }Magic_numbers; 50 | 51 | //Every ELF file needs one of these 52 | typedef struct 53 | { 54 | Magic_numbers identity; //the magic ELF header number to identify itself. The union below 55 | unsigned short type; // the type of file, selected from Object_file_type enum below 56 | unsigned short machine; //what sort of processor was made on, from Machine_type enum below 57 | unsigned int version; //Version of ELF, from Version_type enum below 58 | unsigned int entry; //Location of program entry point, if there is one 59 | unsigned int phoff; //program header offset (struct below), in bytes 60 | unsigned int shoff; //section header offset (struct below), in bytes 61 | unsigned int flags; //anything extra info about the Machine 62 | unsigned short elf_header_size; //The size of this header, in bytes 63 | unsigned short program_header_entry_size; //the size of one entry in the program header. They all have to match 64 | unsigned short program_headar_entry_number; //how many entries in the program header. Can be zero 65 | unsigned short section_header_entry_size; //the size of one entry in the section header. They all have to match 66 | unsigned short section_header_entry_number; //how many entries in the program header. Can be zero 67 | unsigned short section_header_str_index; //index of section header that that goes with the string table 68 | 69 | }File_Header; 70 | 71 | //These are the different types of objects the file can be 72 | enum Object_file_type 73 | { 74 | OFT_NONE, //none? 75 | OFT_RELOCATABLE, //static link 76 | OFT_EXECUTABLE, //run 77 | OFT_SHARED_OBJECT, //dynamic link 78 | OFT_CORE, //core dump 79 | OFT_LOPROC = 0xff00, //Processor specific, don't expect to use 80 | OFT_HIPROC = 0xffff, //Processor spepcific, don't expect to use. 81 | OFT_Object_file_type_CNT = 7 82 | }; 83 | 84 | //These are the different machines the ELF file can originate from 85 | enum Machine_type 86 | { 87 | MT_NONE, //none? 88 | MT_M32, //AT&T WE 32100 89 | MT_SPARC, //SPARC 90 | MT_E_368, //Intel 80386 91 | MT_E_68k, //Motorola 68000 92 | MT_E_88k, //Motorola 88000 93 | MT_E_860, //Intel 80860 94 | MT_E_MIPS, //MIPS RS3000 95 | MT_Machine_type_CNT 96 | }; 97 | 98 | //The type of ELF. There's only 1... 99 | enum Version_type 100 | { 101 | VT_NONE, //none? 102 | VT_CURRENT, //current 103 | VT_Version_type_CNT 104 | }; 105 | 106 | //What sort of architecture was it made for? 107 | enum Class_type 108 | { 109 | CT_CLASS_NONE, 110 | CT_CLASS_32, 111 | CT_CLASS_64, 112 | CT_Class_type_CNT 113 | }; 114 | 115 | //how to read the data 116 | enum Data_type 117 | { 118 | DT_DATA_NONE, 119 | DT_DATA_2LSB, 120 | DT_DATA_2MSB, 121 | DT_Data_type_CNT 122 | }; 123 | 124 | ///////////////////////////////////////////////////////////////////////////////////////////// SECTION HEADER 125 | 126 | //only linkables require this 127 | //There are one of these for every section in the ELF File 128 | typedef struct 129 | { 130 | unsigned int name; //the name of a section 131 | unsigned int type; //Section type from the enum below 132 | unsigned int flags; //special flags 133 | unsigned int address; //if this section goes in a memory image, this is where to put it 134 | unsigned int offset; //offset from the start of the ELF file to this section 135 | unsigned int size; //size of the section 136 | unsigned int link; // section header table index link, and is dependend on type 137 | unsigned int info; //extra info about the type if needed 138 | unsigned int addrelign; // alignment constraint for this section, 0 or 1 mean none 139 | unsigned int entry_size; 140 | 141 | }Section_Header; 142 | 143 | //For that last part of the magic header 144 | enum Special_section_indexes 145 | { 146 | SSI_UNDEF, 147 | SSI_LORESERVE = 0xff00, 148 | SSI_LOPROC = 0xff00, 149 | SSI_HIPROC = 0xff1f, 150 | SSI_ABS = 0xfff1, 151 | SSI_COMMON = 0xfff2, 152 | SSI_HIRESERVE = 0xffff, 153 | SSI_Special_section_indexes_CNT = 7 154 | }; 155 | 156 | // possible types this section coule be 157 | enum Section_types 158 | { 159 | ST_NULL, //invactive or invalid and makes all other fields in the Section header go to 0 160 | ST_PROGBITS, //This section is holding data for the program 161 | ST_SYMTAB, // Symbols table 162 | ST_STRTAB, //string table. ELF can have multiple of these 163 | ST_RELA, //relocation entries with addins 164 | ST_HASH, //symbol has table. Must be present for dynamic linking 165 | ST_DYNAMIC, //info for dynamic linking. Can only be one 166 | ST_NOTE, //Just random notes 167 | ST_NOBITS, //Treat like PROGBITS, but takes up no actual space? 168 | ST_REL, //Relcation entries without addins 169 | ST_SHLIB, //Extra crap 170 | ST_DYNDYM, //sybomls table 171 | ST_LOPROC = 0x70000000, //processor specific 172 | ST_HIPROC = 0x7fffffff, //processor specific 173 | ST_LOUSER = 0x80000000, 174 | ST_HIUSER = 0xffffffff, 175 | ST_Section_types_CNT = 16 176 | }; 177 | 178 | enum Section_flags 179 | { 180 | SF_WRITE = 0x1, //data here should be writable during execution 181 | SF_ALLOC = 0x2, //This section will need some memory to run 182 | SF_EXECINSTR = 0x3, //This section contains machine instructions 183 | SF_MASKPROC = 0xf0000000, //processor specific 184 | SF_Section_flags_CNT = 4 185 | }; 186 | 187 | ///////////////////////////////////////////////////////////////////////////////// SYMBOL TABLE ENTRY 188 | 189 | #define ELF32_symbol_table_bind(i) ((i)>>4) 190 | #define ELF32_symbol_table_type(i) ((i)&0xf) 191 | #define ELF32_symbol_table_info(b,t) ((b)<<4)+((t)&0xf) 192 | 193 | typedef struct 194 | { 195 | unsigned int name; //index into a string table 196 | unsigned int value; //can vary 197 | unsigned int size; //size of associated data, or could be 0 198 | unsigned char info; //the above macros will extract data out of this 199 | unsigned char other; //always 0... 200 | unsigned short section_header_index; //the index into section header table that his entry relates to 201 | 202 | }Symbol_Table_Entry; 203 | 204 | 205 | //Thes are the outputes of the Bind macro above 206 | enum Symbol_table_bind 207 | { 208 | STB_LOCAL = 0, //symbols are only visible inside this ELF file 209 | STB_GLOBAL = 1, //symbols will be seen by other files being linked 210 | STB_WEAK = 2, //Similar to global 211 | STB_LOPROC = 13, //processor specific 212 | STB_HIPROC = 15, //processor specific 213 | STB_Symbol_table_bind_CNT = 5 214 | }; 215 | 216 | enum Symbol_table_type 217 | { 218 | STT_NOTYPE, //No symbol type 219 | STT_OBJECT, //the symbol is for a data object (variable, array, etc.) 220 | STT_FUNC, // the symbol is for a function or executable code 221 | STT_SECTION, //the symbol is for a section 222 | STT_FILE, // name of the source file associated with this object file 223 | STT_LOPROC = 13, //processor specific 224 | STT_HIPROC = 15, //processor specific 225 | STT_Symbol_table_type_CNT = 7 226 | }; 227 | 228 | ///////////////////////////////////////////////////////////////////// RELOCATION ENTRY 229 | 230 | //relocation is connecting symbolic referencees with symbolic definitions. 231 | // like linking a function call to the definition of a function. 232 | 233 | #define ELF32_R_SYM(i) ((i)>>8) 234 | #define ELF32_R_TYPE(i) ((unsigned char)(i)) 235 | #define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t)) 236 | 237 | typedef struct 238 | { 239 | unsigned int offset; // 240 | unsigned int info; 241 | }Relocation_Entry; 242 | 243 | typedef struct 244 | { 245 | unsigned int offset; 246 | unsigned int info; 247 | signed int addned; 248 | 249 | }Relocation_Entry_Addend; 250 | 251 | enum Relocation_type 252 | { 253 | RT_386_NONE, 254 | RT_386_32, 255 | RT_386_PC32, 256 | RT_386_GOT32, 257 | RT_386_PLT32, 258 | RT_386_COPY, 259 | RT_386_GLOB_DAT, 260 | RT_386_JMP_SLOT, 261 | RT_386_RELATIVE, 262 | RT_386_GOTOFF, 263 | RT_386_GOTPC, 264 | RT_Relocation_type_CNT 265 | }; 266 | 267 | //////////////////////////////////////////////////////////// PROGRAM HEADER 268 | 269 | //Only executables require this 270 | typedef struct 271 | { 272 | unsigned int type; //what kind of segment. Enum below 273 | unsigned int offset; //offset from start of file that this segment resides 274 | unsigned int virtual_address; //virtual address of where this segment resides 275 | unsigned int physical_address; //physical address, if needed 276 | unsigned int file_size; //number of bytes in segment. can be 0. 277 | unsigned int memory_size; //number of bytes in memory image of segment. can be 0. 278 | unsigned int flags; //relevent flags. Enum below 279 | unsigned int align; //how many bytes to align to 280 | }Program_Header; 281 | 282 | enum Segment_Types 283 | { 284 | SGT_NULL, //nothing. Ignore this entry 285 | SGT_LOAD, // loadable segment 286 | SGT_DYNAMIC, //dynamic linking info 287 | SGT_INTERP, // path name to an interpreter 288 | SGT_NOTE, //extra info 289 | SGT_SHLIB, //Unused 290 | SGT_PHDR, //tells the location and size of the program header 291 | SGT_LOPROC = 0x70000000, //processor 292 | SGT_HIPROC = 0x7fffffff //processor 293 | }; 294 | 295 | #endif //ELF_TYEPS_H_ -------------------------------------------------------------------------------- /src/libc/ELF_reader: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zandr0id/TwilightOS/7225a4feb82702ef1569fa6d201b8af2db210e56/src/libc/ELF_reader -------------------------------------------------------------------------------- /src/libc/ELF_reader.cpp: -------------------------------------------------------------------------------- 1 | #include "ELF_Types.h" 2 | #include 3 | #include 4 | 5 | int main(int argc, char ** argv) 6 | { 7 | 8 | File_Header Elf_header; 9 | std::ifstream elf_file; 10 | elf_file.open(argv[1], std::ios::in | std::ios::binary); 11 | 12 | char magic[64]; 13 | 14 | elf_file.read(magic, 64); 15 | 16 | for (int i = 0; i<64; i++) 17 | { 18 | printf("%u ",magic[i]); 19 | } 20 | 21 | printf("\n"); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /src/libc/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zandr0id/TwilightOS/7225a4feb82702ef1569fa6d201b8af2db210e56/src/libc/test -------------------------------------------------------------------------------- /src/libc/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int inc(int &num) 4 | { 5 | return ++num; 6 | } 7 | 8 | int main() 9 | { 10 | int number = 0; 11 | while(number <= 10) 12 | { 13 | std::cout << number << " "; 14 | inc(number); 15 | } 16 | std::cout << std::endl; 17 | return 0; 18 | } 19 | --------------------------------------------------------------------------------