├── .gitignore ├── LICENSE ├── include ├── common │ ├── graphicscontext.h │ └── types.h ├── drivers │ ├── amd_am79c973.h │ ├── ata.h │ ├── driver.h │ ├── keyboard.h │ ├── mouse.h │ └── vga.h ├── gdt.h ├── gui │ ├── desktop.h │ ├── widget.h │ └── window.h ├── hardwarecommunication │ ├── interrupts.h │ ├── pci.h │ └── port.h ├── memorymanagement.h ├── multitasking.h ├── net │ ├── arp.h │ ├── etherframe.h │ ├── icmp.h │ ├── ipv4.h │ ├── tcp.h │ └── udp.h └── syscalls.h ├── linker.ld ├── makefile └── src ├── drivers ├── amd_am79c973.cpp ├── ata.cpp ├── driver.cpp ├── keyboard.cpp ├── mouse.cpp └── vga.cpp ├── gdt.cpp ├── gui ├── desktop.cpp ├── widget.cpp └── window.cpp ├── hardwarecommunication ├── interrupts.cpp ├── interruptstubs.s ├── pci.cpp └── port.cpp ├── kernel.cpp ├── loader.s ├── memorymanagement.cpp ├── multitasking.cpp ├── net ├── arp.cpp ├── etherframe.cpp ├── icmp.cpp ├── ipv4.cpp ├── tcp.cpp └── udp.cpp └── syscalls.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | *.smod 19 | 20 | # Compiled Static libraries 21 | *.lai 22 | *.la 23 | *.a 24 | *.lib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | -------------------------------------------------------------------------------- /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 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 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 | {project} Copyright (C) {year} {fullname} 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 | -------------------------------------------------------------------------------- /include/common/graphicscontext.h: -------------------------------------------------------------------------------- 1 | #ifndef __MYOS__COMMON__GRAPHICSCONTEXT_H 2 | #define __MYOS__COMMON__GRAPHICSCONTEXT_H 3 | 4 | #include 5 | 6 | namespace myos 7 | { 8 | namespace common 9 | { 10 | typedef myos::drivers::VideoGraphicsArray GraphicsContext; 11 | } 12 | } 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/common/types.h: -------------------------------------------------------------------------------- 1 | #ifndef __MYOS__COMMON__TYPES_H 2 | #define __MYOS__COMMON__TYPES_H 3 | 4 | namespace myos 5 | { 6 | namespace common 7 | { 8 | 9 | typedef char int8_t; 10 | typedef unsigned char uint8_t; 11 | typedef short int16_t; 12 | typedef unsigned short uint16_t; 13 | typedef int int32_t; 14 | typedef unsigned int uint32_t; 15 | typedef long long int int64_t; 16 | typedef unsigned long long int uint64_t; 17 | 18 | typedef const char* string; 19 | typedef uint32_t size_t; 20 | } 21 | } 22 | 23 | #endif -------------------------------------------------------------------------------- /include/drivers/amd_am79c973.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__DRIVERS__AMD_AM79C973_H 3 | #define __MYOS__DRIVERS__AMD_AM79C973_H 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | namespace myos 14 | { 15 | namespace drivers 16 | { 17 | 18 | class amd_am79c973; 19 | 20 | class RawDataHandler 21 | { 22 | protected: 23 | amd_am79c973* backend; 24 | public: 25 | RawDataHandler(amd_am79c973* backend); 26 | ~RawDataHandler(); 27 | 28 | virtual bool OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size); 29 | void Send(common::uint8_t* buffer, common::uint32_t size); 30 | }; 31 | 32 | 33 | class amd_am79c973 : public Driver, public hardwarecommunication::InterruptHandler 34 | { 35 | struct InitializationBlock 36 | { 37 | common::uint16_t mode; 38 | unsigned reserved1 : 4; 39 | unsigned numSendBuffers : 4; 40 | unsigned reserved2 : 4; 41 | unsigned numRecvBuffers : 4; 42 | common::uint64_t physicalAddress : 48; 43 | common::uint16_t reserved3; 44 | common::uint64_t logicalAddress; 45 | common::uint32_t recvBufferDescrAddress; 46 | common::uint32_t sendBufferDescrAddress; 47 | } __attribute__((packed)); 48 | 49 | 50 | struct BufferDescriptor 51 | { 52 | common::uint32_t address; 53 | common::uint32_t flags; 54 | common::uint32_t flags2; 55 | common::uint32_t avail; 56 | } __attribute__((packed)); 57 | 58 | hardwarecommunication::Port16Bit MACAddress0Port; 59 | hardwarecommunication::Port16Bit MACAddress2Port; 60 | hardwarecommunication::Port16Bit MACAddress4Port; 61 | hardwarecommunication::Port16Bit registerDataPort; 62 | hardwarecommunication::Port16Bit registerAddressPort; 63 | hardwarecommunication::Port16Bit resetPort; 64 | hardwarecommunication::Port16Bit busControlRegisterDataPort; 65 | 66 | InitializationBlock initBlock; 67 | 68 | 69 | BufferDescriptor* sendBufferDescr; 70 | common::uint8_t sendBufferDescrMemory[2048+15]; 71 | common::uint8_t sendBuffers[2*1024+15][8]; 72 | common::uint8_t currentSendBuffer; 73 | 74 | BufferDescriptor* recvBufferDescr; 75 | common::uint8_t recvBufferDescrMemory[2048+15]; 76 | common::uint8_t recvBuffers[2*1024+15][8]; 77 | common::uint8_t currentRecvBuffer; 78 | 79 | 80 | RawDataHandler* handler; 81 | 82 | public: 83 | amd_am79c973(myos::hardwarecommunication::PeripheralComponentInterconnectDeviceDescriptor *dev, 84 | myos::hardwarecommunication::InterruptManager* interrupts); 85 | ~amd_am79c973(); 86 | 87 | void Activate(); 88 | int Reset(); 89 | common::uint32_t HandleInterrupt(common::uint32_t esp); 90 | 91 | void Send(common::uint8_t* buffer, int count); 92 | void Receive(); 93 | 94 | void SetHandler(RawDataHandler* handler); 95 | common::uint64_t GetMACAddress(); 96 | void SetIPAddress(common::uint32_t); 97 | common::uint32_t GetIPAddress(); 98 | }; 99 | 100 | 101 | 102 | } 103 | } 104 | 105 | 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /include/drivers/ata.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__DRIVERS__ATA_H 3 | #define __MYOS__DRIVERS__ATA_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace myos 10 | { 11 | namespace drivers 12 | { 13 | 14 | class AdvancedTechnologyAttachment 15 | { 16 | protected: 17 | bool master; 18 | hardwarecommunication::Port16Bit dataPort; 19 | hardwarecommunication::Port8Bit errorPort; 20 | hardwarecommunication::Port8Bit sectorCountPort; 21 | hardwarecommunication::Port8Bit lbaLowPort; 22 | hardwarecommunication::Port8Bit lbaMidPort; 23 | hardwarecommunication::Port8Bit lbaHiPort; 24 | hardwarecommunication::Port8Bit devicePort; 25 | hardwarecommunication::Port8Bit commandPort; 26 | hardwarecommunication::Port8Bit controlPort; 27 | public: 28 | 29 | AdvancedTechnologyAttachment(bool master, common::uint16_t portBase); 30 | ~AdvancedTechnologyAttachment(); 31 | 32 | void Identify(); 33 | void Read28(common::uint32_t sectorNum, int count = 512); 34 | void Write28(common::uint32_t sectorNum, common::uint8_t* data, common::uint32_t count); 35 | void Flush(); 36 | 37 | 38 | }; 39 | 40 | } 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/drivers/driver.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__DRIVERS__DRIVER_H 3 | #define __MYOS__DRIVERS__DRIVER_H 4 | 5 | namespace myos 6 | { 7 | namespace drivers 8 | { 9 | 10 | class Driver 11 | { 12 | public: 13 | Driver(); 14 | ~Driver(); 15 | 16 | virtual void Activate(); 17 | virtual int Reset(); 18 | virtual void Deactivate(); 19 | }; 20 | 21 | class DriverManager 22 | { 23 | public: 24 | Driver* drivers[265]; 25 | int numDrivers; 26 | 27 | public: 28 | DriverManager(); 29 | void AddDriver(Driver*); 30 | 31 | void ActivateAll(); 32 | 33 | }; 34 | 35 | } 36 | } 37 | 38 | 39 | #endif -------------------------------------------------------------------------------- /include/drivers/keyboard.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__DRIVERS__KEYBOARD_H 3 | #define __MYOS__DRIVERS__KEYBOARD_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace myos 11 | { 12 | namespace drivers 13 | { 14 | 15 | class KeyboardEventHandler 16 | { 17 | public: 18 | KeyboardEventHandler(); 19 | 20 | virtual void OnKeyDown(char); 21 | virtual void OnKeyUp(char); 22 | }; 23 | 24 | class KeyboardDriver : public myos::hardwarecommunication::InterruptHandler, public Driver 25 | { 26 | myos::hardwarecommunication::Port8Bit dataport; 27 | myos::hardwarecommunication::Port8Bit commandport; 28 | 29 | KeyboardEventHandler* handler; 30 | public: 31 | KeyboardDriver(myos::hardwarecommunication::InterruptManager* manager, KeyboardEventHandler *handler); 32 | ~KeyboardDriver(); 33 | virtual myos::common::uint32_t HandleInterrupt(myos::common::uint32_t esp); 34 | virtual void Activate(); 35 | }; 36 | 37 | } 38 | } 39 | 40 | #endif -------------------------------------------------------------------------------- /include/drivers/mouse.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__DRIVERS__MOUSE_H 3 | #define __MYOS__DRIVERS__MOUSE_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace myos 11 | { 12 | namespace drivers 13 | { 14 | 15 | class MouseEventHandler 16 | { 17 | public: 18 | MouseEventHandler(); 19 | 20 | virtual void OnActivate(); 21 | virtual void OnMouseDown(myos::common::uint8_t button); 22 | virtual void OnMouseUp(myos::common::uint8_t button); 23 | virtual void OnMouseMove(int x, int y); 24 | }; 25 | 26 | 27 | class MouseDriver : public myos::hardwarecommunication::InterruptHandler, public Driver 28 | { 29 | myos::hardwarecommunication::Port8Bit dataport; 30 | myos::hardwarecommunication::Port8Bit commandport; 31 | myos::common::uint8_t buffer[3]; 32 | myos::common::uint8_t offset; 33 | myos::common::uint8_t buttons; 34 | 35 | MouseEventHandler* handler; 36 | public: 37 | MouseDriver(myos::hardwarecommunication::InterruptManager* manager, MouseEventHandler* handler); 38 | ~MouseDriver(); 39 | virtual myos::common::uint32_t HandleInterrupt(myos::common::uint32_t esp); 40 | virtual void Activate(); 41 | }; 42 | 43 | } 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/drivers/vga.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef __MYOS__DRIVERS__VGA_H 4 | #define __MYOS__DRIVERS__VGA_H 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace myos 11 | { 12 | namespace drivers 13 | { 14 | 15 | class VideoGraphicsArray 16 | { 17 | protected: 18 | hardwarecommunication::Port8Bit miscPort; 19 | hardwarecommunication::Port8Bit crtcIndexPort; 20 | hardwarecommunication::Port8Bit crtcDataPort; 21 | hardwarecommunication::Port8Bit sequencerIndexPort; 22 | hardwarecommunication::Port8Bit sequencerDataPort; 23 | hardwarecommunication::Port8Bit graphicsControllerIndexPort; 24 | hardwarecommunication::Port8Bit graphicsControllerDataPort; 25 | hardwarecommunication::Port8Bit attributeControllerIndexPort; 26 | hardwarecommunication::Port8Bit attributeControllerReadPort; 27 | hardwarecommunication::Port8Bit attributeControllerWritePort; 28 | hardwarecommunication::Port8Bit attributeControllerResetPort; 29 | 30 | void WriteRegisters(common::uint8_t* registers); 31 | common::uint8_t* GetFrameBufferSegment(); 32 | 33 | virtual common::uint8_t GetColorIndex(common::uint8_t r, common::uint8_t g, common::uint8_t b); 34 | 35 | 36 | public: 37 | VideoGraphicsArray(); 38 | ~VideoGraphicsArray(); 39 | 40 | virtual bool SupportsMode(common::uint32_t width, common::uint32_t height, common::uint32_t colordepth); 41 | virtual bool SetMode(common::uint32_t width, common::uint32_t height, common::uint32_t colordepth); 42 | virtual void PutPixel(common::int32_t x, common::int32_t y, common::uint8_t r, common::uint8_t g, common::uint8_t b); 43 | virtual void PutPixel(common::int32_t x, common::int32_t y, common::uint8_t colorIndex); 44 | 45 | virtual void FillRectangle(common::uint32_t x, common::uint32_t y, common::uint32_t w, common::uint32_t h, common::uint8_t r, common::uint8_t g, common::uint8_t b); 46 | 47 | }; 48 | 49 | } 50 | } 51 | 52 | #endif -------------------------------------------------------------------------------- /include/gdt.h: -------------------------------------------------------------------------------- 1 | #ifndef __MYOS__GDT_H 2 | #define __MYOS__GDT_H 3 | 4 | #include 5 | 6 | namespace myos 7 | { 8 | 9 | class GlobalDescriptorTable 10 | { 11 | public: 12 | 13 | class SegmentDescriptor 14 | { 15 | private: 16 | myos::common::uint16_t limit_lo; 17 | myos::common::uint16_t base_lo; 18 | myos::common::uint8_t base_hi; 19 | myos::common::uint8_t type; 20 | myos::common::uint8_t limit_hi; 21 | myos::common::uint8_t base_vhi; 22 | 23 | public: 24 | SegmentDescriptor(myos::common::uint32_t base, myos::common::uint32_t limit, myos::common::uint8_t type); 25 | myos::common::uint32_t Base(); 26 | myos::common::uint32_t Limit(); 27 | } __attribute__((packed)); 28 | 29 | private: 30 | SegmentDescriptor nullSegmentSelector; 31 | SegmentDescriptor unusedSegmentSelector; 32 | SegmentDescriptor codeSegmentSelector; 33 | SegmentDescriptor dataSegmentSelector; 34 | 35 | public: 36 | 37 | GlobalDescriptorTable(); 38 | ~GlobalDescriptorTable(); 39 | 40 | myos::common::uint16_t CodeSegmentSelector(); 41 | myos::common::uint16_t DataSegmentSelector(); 42 | }; 43 | 44 | } 45 | 46 | #endif -------------------------------------------------------------------------------- /include/gui/desktop.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__GUI__DESKTOP_H 3 | #define __MYOS__GUI__DESKTOP_H 4 | 5 | #include 6 | #include 7 | 8 | namespace myos 9 | { 10 | namespace gui 11 | { 12 | 13 | class Desktop : public CompositeWidget, public myos::drivers::MouseEventHandler 14 | { 15 | protected: 16 | common::uint32_t MouseX; 17 | common::uint32_t MouseY; 18 | 19 | public: 20 | Desktop(common::int32_t w, common::int32_t h, 21 | common::uint8_t r, common::uint8_t g, common::uint8_t b); 22 | ~Desktop(); 23 | 24 | void Draw(common::GraphicsContext* gc); 25 | 26 | void OnMouseDown(myos::common::uint8_t button); 27 | void OnMouseUp(myos::common::uint8_t button); 28 | void OnMouseMove(int x, int y); 29 | }; 30 | 31 | } 32 | } 33 | 34 | 35 | #endif -------------------------------------------------------------------------------- /include/gui/widget.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__GUI__WIDGET_H 3 | #define __MYOS__GUI__WIDGET_H 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace myos 11 | { 12 | namespace gui 13 | { 14 | 15 | class Widget : public myos::drivers::KeyboardEventHandler 16 | { 17 | protected: 18 | Widget* parent; 19 | common::int32_t x; 20 | common::int32_t y; 21 | common::int32_t w; 22 | common::int32_t h; 23 | 24 | common::uint8_t r; 25 | common::uint8_t g; 26 | common::uint8_t b; 27 | bool Focussable; 28 | 29 | public: 30 | 31 | Widget(Widget* parent, 32 | common::int32_t x, common::int32_t y, common::int32_t w, common::int32_t h, 33 | common::uint8_t r, common::uint8_t g, common::uint8_t b); 34 | ~Widget(); 35 | 36 | virtual void GetFocus(Widget* widget); 37 | virtual void ModelToScreen(common::int32_t &x, common::int32_t& y); 38 | virtual bool ContainsCoordinate(common::int32_t x, common::int32_t y); 39 | 40 | virtual void Draw(common::GraphicsContext* gc); 41 | virtual void OnMouseDown(common::int32_t x, common::int32_t y, common::uint8_t button); 42 | virtual void OnMouseUp(common::int32_t x, common::int32_t y, common::uint8_t button); 43 | virtual void OnMouseMove(common::int32_t oldx, common::int32_t oldy, common::int32_t newx, common::int32_t newy); 44 | }; 45 | 46 | 47 | class CompositeWidget : public Widget 48 | { 49 | private: 50 | Widget* children[100]; 51 | int numChildren; 52 | Widget* focussedChild; 53 | 54 | public: 55 | CompositeWidget(Widget* parent, 56 | common::int32_t x, common::int32_t y, common::int32_t w, common::int32_t h, 57 | common::uint8_t r, common::uint8_t g, common::uint8_t b); 58 | ~CompositeWidget(); 59 | 60 | virtual void GetFocus(Widget* widget); 61 | virtual bool AddChild(Widget* child); 62 | 63 | virtual void Draw(common::GraphicsContext* gc); 64 | virtual void OnMouseDown(common::int32_t x, common::int32_t y, common::uint8_t button); 65 | virtual void OnMouseUp(common::int32_t x, common::int32_t y, common::uint8_t button); 66 | virtual void OnMouseMove(common::int32_t oldx, common::int32_t oldy, common::int32_t newx, common::int32_t newy); 67 | 68 | virtual void OnKeyDown(char); 69 | virtual void OnKeyUp(char); 70 | }; 71 | 72 | } 73 | } 74 | 75 | 76 | #endif -------------------------------------------------------------------------------- /include/gui/window.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__GUI__WINDOW_H 3 | #define __MYOS__GUI__WINDOW_H 4 | 5 | #include 6 | #include 7 | 8 | namespace myos 9 | { 10 | namespace gui 11 | { 12 | 13 | class Window : public CompositeWidget 14 | { 15 | protected: 16 | bool Dragging; 17 | 18 | public: 19 | Window(Widget* parent, 20 | common::int32_t x, common::int32_t y, common::int32_t w, common::int32_t h, 21 | common::uint8_t r, common::uint8_t g, common::uint8_t b); 22 | ~Window(); 23 | 24 | void OnMouseDown(common::int32_t x, common::int32_t y, common::uint8_t button); 25 | void OnMouseUp(common::int32_t x, common::int32_t y, common::uint8_t button); 26 | void OnMouseMove(common::int32_t oldx, common::int32_t oldy, common::int32_t newx, common::int32_t newy); 27 | 28 | }; 29 | } 30 | } 31 | 32 | #endif -------------------------------------------------------------------------------- /include/hardwarecommunication/interrupts.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__HARDWARECOMMUNICATION__INTERRUPTMANAGER_H 3 | #define __MYOS__HARDWARECOMMUNICATION__INTERRUPTMANAGER_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | namespace myos 12 | { 13 | namespace hardwarecommunication 14 | { 15 | 16 | class InterruptManager; 17 | 18 | class InterruptHandler 19 | { 20 | protected: 21 | myos::common::uint8_t InterruptNumber; 22 | InterruptManager* interruptManager; 23 | InterruptHandler(InterruptManager* interruptManager, myos::common::uint8_t InterruptNumber); 24 | ~InterruptHandler(); 25 | public: 26 | virtual myos::common::uint32_t HandleInterrupt(myos::common::uint32_t esp); 27 | }; 28 | 29 | 30 | class InterruptManager 31 | { 32 | friend class InterruptHandler; 33 | protected: 34 | 35 | static InterruptManager* ActiveInterruptManager; 36 | InterruptHandler* handlers[256]; 37 | TaskManager *taskManager; 38 | 39 | struct GateDescriptor 40 | { 41 | myos::common::uint16_t handlerAddressLowBits; 42 | myos::common::uint16_t gdt_codeSegmentSelector; 43 | myos::common::uint8_t reserved; 44 | myos::common::uint8_t access; 45 | myos::common::uint16_t handlerAddressHighBits; 46 | } __attribute__((packed)); 47 | 48 | static GateDescriptor interruptDescriptorTable[256]; 49 | 50 | struct InterruptDescriptorTablePointer 51 | { 52 | myos::common::uint16_t size; 53 | myos::common::uint32_t base; 54 | } __attribute__((packed)); 55 | 56 | myos::common::uint16_t hardwareInterruptOffset; 57 | static void SetInterruptDescriptorTableEntry(myos::common::uint8_t interrupt, 58 | myos::common::uint16_t codeSegmentSelectorOffset, void (*handler)(), 59 | myos::common::uint8_t DescriptorPrivilegeLevel, myos::common::uint8_t DescriptorType); 60 | 61 | 62 | static void InterruptIgnore(); 63 | 64 | static void HandleInterruptRequest0x00(); 65 | static void HandleInterruptRequest0x01(); 66 | static void HandleInterruptRequest0x02(); 67 | static void HandleInterruptRequest0x03(); 68 | static void HandleInterruptRequest0x04(); 69 | static void HandleInterruptRequest0x05(); 70 | static void HandleInterruptRequest0x06(); 71 | static void HandleInterruptRequest0x07(); 72 | static void HandleInterruptRequest0x08(); 73 | static void HandleInterruptRequest0x09(); 74 | static void HandleInterruptRequest0x0A(); 75 | static void HandleInterruptRequest0x0B(); 76 | static void HandleInterruptRequest0x0C(); 77 | static void HandleInterruptRequest0x0D(); 78 | static void HandleInterruptRequest0x0E(); 79 | static void HandleInterruptRequest0x0F(); 80 | static void HandleInterruptRequest0x31(); 81 | 82 | static void HandleInterruptRequest0x80(); 83 | 84 | static void HandleException0x00(); 85 | static void HandleException0x01(); 86 | static void HandleException0x02(); 87 | static void HandleException0x03(); 88 | static void HandleException0x04(); 89 | static void HandleException0x05(); 90 | static void HandleException0x06(); 91 | static void HandleException0x07(); 92 | static void HandleException0x08(); 93 | static void HandleException0x09(); 94 | static void HandleException0x0A(); 95 | static void HandleException0x0B(); 96 | static void HandleException0x0C(); 97 | static void HandleException0x0D(); 98 | static void HandleException0x0E(); 99 | static void HandleException0x0F(); 100 | static void HandleException0x10(); 101 | static void HandleException0x11(); 102 | static void HandleException0x12(); 103 | static void HandleException0x13(); 104 | 105 | static myos::common::uint32_t HandleInterrupt(myos::common::uint8_t interrupt, myos::common::uint32_t esp); 106 | myos::common::uint32_t DoHandleInterrupt(myos::common::uint8_t interrupt, myos::common::uint32_t esp); 107 | 108 | Port8BitSlow programmableInterruptControllerMasterCommandPort; 109 | Port8BitSlow programmableInterruptControllerMasterDataPort; 110 | Port8BitSlow programmableInterruptControllerSlaveCommandPort; 111 | Port8BitSlow programmableInterruptControllerSlaveDataPort; 112 | 113 | public: 114 | InterruptManager(myos::common::uint16_t hardwareInterruptOffset, myos::GlobalDescriptorTable* globalDescriptorTable, myos::TaskManager* taskManager); 115 | ~InterruptManager(); 116 | myos::common::uint16_t HardwareInterruptOffset(); 117 | void Activate(); 118 | void Deactivate(); 119 | }; 120 | 121 | } 122 | } 123 | 124 | #endif -------------------------------------------------------------------------------- /include/hardwarecommunication/pci.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__HARDWARECOMMUNICATION__PCI_H 3 | #define __MYOS__HARDWARECOMMUNICATION__PCI_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | namespace myos 13 | { 14 | namespace hardwarecommunication 15 | { 16 | 17 | enum BaseAddressRegisterType 18 | { 19 | MemoryMapping = 0, 20 | InputOutput = 1 21 | }; 22 | 23 | 24 | 25 | class BaseAddressRegister 26 | { 27 | public: 28 | bool prefetchable; 29 | myos::common::uint8_t* address; 30 | myos::common::uint32_t size; 31 | BaseAddressRegisterType type; 32 | }; 33 | 34 | 35 | 36 | class PeripheralComponentInterconnectDeviceDescriptor 37 | { 38 | public: 39 | myos::common::uint32_t portBase; 40 | myos::common::uint32_t interrupt; 41 | 42 | myos::common::uint16_t bus; 43 | myos::common::uint16_t device; 44 | myos::common::uint16_t function; 45 | 46 | myos::common::uint16_t vendor_id; 47 | myos::common::uint16_t device_id; 48 | 49 | myos::common::uint8_t class_id; 50 | myos::common::uint8_t subclass_id; 51 | myos::common::uint8_t interface_id; 52 | 53 | myos::common::uint8_t revision; 54 | 55 | PeripheralComponentInterconnectDeviceDescriptor(); 56 | ~PeripheralComponentInterconnectDeviceDescriptor(); 57 | 58 | }; 59 | 60 | 61 | class PeripheralComponentInterconnectController 62 | { 63 | Port32Bit dataPort; 64 | Port32Bit commandPort; 65 | 66 | public: 67 | PeripheralComponentInterconnectController(); 68 | ~PeripheralComponentInterconnectController(); 69 | 70 | myos::common::uint32_t Read(myos::common::uint16_t bus, myos::common::uint16_t device, myos::common::uint16_t function, myos::common::uint32_t registeroffset); 71 | void Write(myos::common::uint16_t bus, myos::common::uint16_t device, myos::common::uint16_t function, myos::common::uint32_t registeroffset, myos::common::uint32_t value); 72 | bool DeviceHasFunctions(myos::common::uint16_t bus, myos::common::uint16_t device); 73 | 74 | void SelectDrivers(myos::drivers::DriverManager* driverManager, myos::hardwarecommunication::InterruptManager* interrupts); 75 | myos::drivers::Driver* GetDriver(PeripheralComponentInterconnectDeviceDescriptor dev, myos::hardwarecommunication::InterruptManager* interrupts); 76 | PeripheralComponentInterconnectDeviceDescriptor GetDeviceDescriptor(myos::common::uint16_t bus, myos::common::uint16_t device, myos::common::uint16_t function); 77 | BaseAddressRegister GetBaseAddressRegister(myos::common::uint16_t bus, myos::common::uint16_t device, myos::common::uint16_t function, myos::common::uint16_t bar); 78 | }; 79 | 80 | } 81 | } 82 | 83 | #endif -------------------------------------------------------------------------------- /include/hardwarecommunication/port.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__HARDWARECOMMUNICATION__PORT_H 3 | #define __MYOS__HARDWARECOMMUNICATION__PORT_H 4 | 5 | #include 6 | 7 | namespace myos 8 | { 9 | namespace hardwarecommunication 10 | { 11 | 12 | class Port 13 | { 14 | protected: 15 | Port(myos::common::uint16_t portnumber); 16 | // FIXME: Must be virtual (currently isnt because the kernel has no memory management yet) 17 | ~Port(); 18 | myos::common::uint16_t portnumber; 19 | }; 20 | 21 | 22 | class Port8Bit : public Port 23 | { 24 | public: 25 | Port8Bit(myos::common::uint16_t portnumber); 26 | ~Port8Bit(); 27 | 28 | virtual myos::common::uint8_t Read(); 29 | virtual void Write(myos::common::uint8_t data); 30 | 31 | protected: 32 | static inline myos::common::uint8_t Read8(myos::common::uint16_t _port) 33 | { 34 | myos::common::uint8_t result; 35 | __asm__ volatile("inb %1, %0" : "=a" (result) : "Nd" (_port)); 36 | return result; 37 | } 38 | 39 | static inline void Write8(myos::common::uint16_t _port, myos::common::uint8_t _data) 40 | { 41 | __asm__ volatile("outb %0, %1" : : "a" (_data), "Nd" (_port)); 42 | } 43 | }; 44 | 45 | 46 | 47 | class Port8BitSlow : public Port8Bit 48 | { 49 | public: 50 | Port8BitSlow(myos::common::uint16_t portnumber); 51 | ~Port8BitSlow(); 52 | 53 | virtual void Write(myos::common::uint8_t data); 54 | protected: 55 | static inline void Write8Slow(myos::common::uint16_t _port, myos::common::uint8_t _data) 56 | { 57 | __asm__ volatile("outb %0, %1\njmp 1f\n1: jmp 1f\n1:" : : "a" (_data), "Nd" (_port)); 58 | } 59 | 60 | }; 61 | 62 | 63 | 64 | class Port16Bit : public Port 65 | { 66 | public: 67 | Port16Bit(myos::common::uint16_t portnumber); 68 | ~Port16Bit(); 69 | 70 | virtual myos::common::uint16_t Read(); 71 | virtual void Write(myos::common::uint16_t data); 72 | 73 | protected: 74 | static inline myos::common::uint16_t Read16(myos::common::uint16_t _port) 75 | { 76 | myos::common::uint16_t result; 77 | __asm__ volatile("inw %1, %0" : "=a" (result) : "Nd" (_port)); 78 | return result; 79 | } 80 | 81 | static inline void Write16(myos::common::uint16_t _port, myos::common::uint16_t _data) 82 | { 83 | __asm__ volatile("outw %0, %1" : : "a" (_data), "Nd" (_port)); 84 | } 85 | }; 86 | 87 | 88 | 89 | class Port32Bit : public Port 90 | { 91 | public: 92 | Port32Bit(myos::common::uint16_t portnumber); 93 | ~Port32Bit(); 94 | 95 | virtual myos::common::uint32_t Read(); 96 | virtual void Write(myos::common::uint32_t data); 97 | 98 | protected: 99 | static inline myos::common::uint32_t Read32(myos::common::uint16_t _port) 100 | { 101 | myos::common::uint32_t result; 102 | __asm__ volatile("inl %1, %0" : "=a" (result) : "Nd" (_port)); 103 | return result; 104 | } 105 | 106 | static inline void Write32(myos::common::uint16_t _port, myos::common::uint32_t _data) 107 | { 108 | __asm__ volatile("outl %0, %1" : : "a"(_data), "Nd" (_port)); 109 | } 110 | }; 111 | 112 | } 113 | } 114 | 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /include/memorymanagement.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__MEMORYMANAGEMENT_H 3 | #define __MYOS__MEMORYMANAGEMENT_H 4 | 5 | #include 6 | 7 | 8 | namespace myos 9 | { 10 | 11 | struct MemoryChunk 12 | { 13 | MemoryChunk *next; 14 | MemoryChunk *prev; 15 | bool allocated; 16 | common::size_t size; 17 | }; 18 | 19 | 20 | class MemoryManager 21 | { 22 | 23 | protected: 24 | MemoryChunk* first; 25 | public: 26 | 27 | static MemoryManager *activeMemoryManager; 28 | 29 | MemoryManager(common::size_t first, common::size_t size); 30 | ~MemoryManager(); 31 | 32 | void* malloc(common::size_t size); 33 | void free(void* ptr); 34 | }; 35 | } 36 | 37 | 38 | void* operator new(unsigned size); 39 | void* operator new[](unsigned size); 40 | 41 | // placement new 42 | void* operator new(unsigned size, void* ptr); 43 | void* operator new[](unsigned size, void* ptr); 44 | 45 | void operator delete(void* ptr); 46 | void operator delete[](void* ptr); 47 | 48 | 49 | #endif -------------------------------------------------------------------------------- /include/multitasking.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__MULTITASKING_H 3 | #define __MYOS__MULTITASKING_H 4 | 5 | #include 6 | #include 7 | 8 | namespace myos 9 | { 10 | 11 | struct CPUState 12 | { 13 | common::uint32_t eax; 14 | common::uint32_t ebx; 15 | common::uint32_t ecx; 16 | common::uint32_t edx; 17 | 18 | common::uint32_t esi; 19 | common::uint32_t edi; 20 | common::uint32_t ebp; 21 | 22 | /* 23 | common::uint32_t gs; 24 | common::uint32_t fs; 25 | common::uint32_t es; 26 | common::uint32_t ds; 27 | */ 28 | common::uint32_t error; 29 | 30 | common::uint32_t eip; 31 | common::uint32_t cs; 32 | common::uint32_t eflags; 33 | common::uint32_t esp; 34 | common::uint32_t ss; 35 | } __attribute__((packed)); 36 | 37 | 38 | class Task 39 | { 40 | friend class TaskManager; 41 | private: 42 | common::uint8_t stack[4096]; // 4 KiB 43 | CPUState* cpustate; 44 | public: 45 | Task(GlobalDescriptorTable *gdt, void entrypoint()); 46 | ~Task(); 47 | }; 48 | 49 | 50 | class TaskManager 51 | { 52 | private: 53 | Task* tasks[256]; 54 | int numTasks; 55 | int currentTask; 56 | public: 57 | TaskManager(); 58 | ~TaskManager(); 59 | bool AddTask(Task* task); 60 | CPUState* Schedule(CPUState* cpustate); 61 | }; 62 | 63 | 64 | 65 | } 66 | 67 | 68 | #endif -------------------------------------------------------------------------------- /include/net/arp.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__NET__ARP_H 3 | #define __MYOS__NET__ARP_H 4 | 5 | 6 | #include 7 | #include 8 | 9 | 10 | namespace myos 11 | { 12 | namespace net 13 | { 14 | 15 | struct AddressResolutionProtocolMessage 16 | { 17 | common::uint16_t hardwareType; 18 | common::uint16_t protocol; 19 | common::uint8_t hardwareAddressSize; // 6 20 | common::uint8_t protocolAddressSize; // 4 21 | common::uint16_t command; 22 | 23 | 24 | common::uint64_t srcMAC : 48; 25 | common::uint32_t srcIP; 26 | common::uint64_t dstMAC : 48; 27 | common::uint32_t dstIP; 28 | 29 | } __attribute__((packed)); 30 | 31 | 32 | 33 | class AddressResolutionProtocol : public EtherFrameHandler 34 | { 35 | 36 | common::uint32_t IPcache[128]; 37 | common::uint64_t MACcache[128]; 38 | int numCacheEntries; 39 | 40 | public: 41 | AddressResolutionProtocol(EtherFrameProvider* backend); 42 | ~AddressResolutionProtocol(); 43 | 44 | bool OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size); 45 | 46 | void RequestMACAddress(common::uint32_t IP_BE); 47 | common::uint64_t GetMACFromCache(common::uint32_t IP_BE); 48 | common::uint64_t Resolve(common::uint32_t IP_BE); 49 | void BroadcastMACAddress(common::uint32_t IP_BE); 50 | }; 51 | 52 | 53 | } 54 | } 55 | 56 | 57 | #endif -------------------------------------------------------------------------------- /include/net/etherframe.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__NET__ETHERFRAME_H 3 | #define __MYOS__NET__ETHERFRAME_H 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | namespace myos 12 | { 13 | namespace net 14 | { 15 | 16 | struct EtherFrameHeader 17 | { 18 | common::uint64_t dstMAC_BE : 48; 19 | common::uint64_t srcMAC_BE : 48; 20 | common::uint16_t etherType_BE; 21 | } __attribute__ ((packed)); 22 | 23 | typedef common::uint32_t EtherFrameFooter; 24 | 25 | 26 | 27 | class EtherFrameProvider; 28 | 29 | class EtherFrameHandler 30 | { 31 | protected: 32 | EtherFrameProvider* backend; 33 | common::uint16_t etherType_BE; 34 | 35 | public: 36 | EtherFrameHandler(EtherFrameProvider* backend, common::uint16_t etherType); 37 | ~EtherFrameHandler(); 38 | 39 | virtual bool OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size); 40 | void Send(common::uint64_t dstMAC_BE, common::uint8_t* etherframePayload, common::uint32_t size); 41 | common::uint32_t GetIPAddress(); 42 | }; 43 | 44 | 45 | class EtherFrameProvider : public myos::drivers::RawDataHandler 46 | { 47 | friend class EtherFrameHandler; 48 | protected: 49 | EtherFrameHandler* handlers[65535]; 50 | public: 51 | EtherFrameProvider(drivers::amd_am79c973* backend); 52 | ~EtherFrameProvider(); 53 | 54 | bool OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size); 55 | void Send(common::uint64_t dstMAC_BE, common::uint16_t etherType_BE, common::uint8_t* buffer, common::uint32_t size); 56 | 57 | common::uint64_t GetMACAddress(); 58 | common::uint32_t GetIPAddress(); 59 | }; 60 | 61 | 62 | 63 | } 64 | } 65 | 66 | 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /include/net/icmp.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__NET__ICMP_H 3 | #define __MYOS__NET__ICMP_H 4 | 5 | #include 6 | #include 7 | 8 | 9 | namespace myos 10 | { 11 | namespace net 12 | { 13 | 14 | 15 | struct InternetControlMessageProtocolMessage 16 | { 17 | common::uint8_t type; 18 | common::uint8_t code; 19 | 20 | common::uint16_t checksum; 21 | common::uint32_t data; 22 | 23 | } __attribute__((packed)); 24 | 25 | class InternetControlMessageProtocol : InternetProtocolHandler 26 | { 27 | public: 28 | InternetControlMessageProtocol(InternetProtocolProvider* backend); 29 | ~InternetControlMessageProtocol(); 30 | 31 | bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE, 32 | common::uint8_t* internetprotocolPayload, common::uint32_t size); 33 | void RequestEchoReply(common::uint32_t ip_be); 34 | }; 35 | 36 | 37 | } 38 | } 39 | 40 | 41 | #endif -------------------------------------------------------------------------------- /include/net/ipv4.h: -------------------------------------------------------------------------------- 1 | #ifndef __MYOS__NET__IPV4_H 2 | #define __MYOS__NET__IPV4_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace myos 9 | { 10 | namespace net 11 | { 12 | 13 | 14 | struct InternetProtocolV4Message 15 | { 16 | common::uint8_t headerLength : 4; 17 | common::uint8_t version : 4; 18 | common::uint8_t tos; 19 | common::uint16_t totalLength; 20 | 21 | common::uint16_t ident; 22 | common::uint16_t flagsAndOffset; 23 | 24 | common::uint8_t timeToLive; 25 | common::uint8_t protocol; 26 | common::uint16_t checksum; 27 | 28 | common::uint32_t srcIP; 29 | common::uint32_t dstIP; 30 | } __attribute__((packed)); 31 | 32 | 33 | 34 | class InternetProtocolProvider; 35 | 36 | class InternetProtocolHandler 37 | { 38 | protected: 39 | InternetProtocolProvider* backend; 40 | common::uint8_t ip_protocol; 41 | 42 | public: 43 | InternetProtocolHandler(InternetProtocolProvider* backend, common::uint8_t protocol); 44 | ~InternetProtocolHandler(); 45 | 46 | virtual bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE, 47 | common::uint8_t* internetprotocolPayload, common::uint32_t size); 48 | void Send(common::uint32_t dstIP_BE, common::uint8_t* internetprotocolPayload, common::uint32_t size); 49 | }; 50 | 51 | 52 | class InternetProtocolProvider : public EtherFrameHandler 53 | { 54 | friend class InternetProtocolHandler; 55 | protected: 56 | InternetProtocolHandler* handlers[255]; 57 | AddressResolutionProtocol* arp; 58 | common::uint32_t gatewayIP; 59 | common::uint32_t subnetMask; 60 | 61 | public: 62 | InternetProtocolProvider(EtherFrameProvider* backend, 63 | AddressResolutionProtocol* arp, 64 | common::uint32_t gatewayIP, common::uint32_t subnetMask); 65 | ~InternetProtocolProvider(); 66 | 67 | bool OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size); 68 | 69 | void Send(common::uint32_t dstIP_BE, common::uint8_t protocol, common::uint8_t* buffer, common::uint32_t size); 70 | 71 | static common::uint16_t Checksum(common::uint16_t* data, common::uint32_t lengthInBytes); 72 | }; 73 | } 74 | } 75 | 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /include/net/tcp.h: -------------------------------------------------------------------------------- 1 | #ifndef __MYOS__NET__TCP_H 2 | #define __MYOS__NET__TCP_H 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | namespace myos 11 | { 12 | namespace net 13 | { 14 | 15 | 16 | enum TransmissionControlProtocolSocketState 17 | { 18 | CLOSED, 19 | LISTEN, 20 | SYN_SENT, 21 | SYN_RECEIVED, 22 | 23 | ESTABLISHED, 24 | 25 | FIN_WAIT1, 26 | FIN_WAIT2, 27 | CLOSING, 28 | TIME_WAIT, 29 | 30 | CLOSE_WAIT 31 | //LAST_ACK 32 | }; 33 | 34 | enum TransmissionControlProtocolFlag 35 | { 36 | FIN = 1, 37 | SYN = 2, 38 | RST = 4, 39 | PSH = 8, 40 | ACK = 16, 41 | URG = 32, 42 | ECE = 64, 43 | CWR = 128, 44 | NS = 256 45 | }; 46 | 47 | 48 | struct TransmissionControlProtocolHeader 49 | { 50 | common::uint16_t srcPort; 51 | common::uint16_t dstPort; 52 | common::uint32_t sequenceNumber; 53 | common::uint32_t acknowledgementNumber; 54 | 55 | common::uint8_t reserved : 4; 56 | common::uint8_t headerSize32 : 4; 57 | common::uint8_t flags; 58 | 59 | common::uint16_t windowSize; 60 | common::uint16_t checksum; 61 | common::uint16_t urgentPtr; 62 | 63 | common::uint32_t options; 64 | } __attribute__((packed)); 65 | 66 | 67 | struct TransmissionControlProtocolPseudoHeader 68 | { 69 | common::uint32_t srcIP; 70 | common::uint32_t dstIP; 71 | common::uint16_t protocol; 72 | common::uint16_t totalLength; 73 | } __attribute__((packed)); 74 | 75 | 76 | class TransmissionControlProtocolSocket; 77 | class TransmissionControlProtocolProvider; 78 | 79 | 80 | 81 | class TransmissionControlProtocolHandler 82 | { 83 | public: 84 | TransmissionControlProtocolHandler(); 85 | ~TransmissionControlProtocolHandler(); 86 | virtual bool HandleTransmissionControlProtocolMessage(TransmissionControlProtocolSocket* socket, common::uint8_t* data, common::uint16_t size); 87 | }; 88 | 89 | 90 | 91 | class TransmissionControlProtocolSocket 92 | { 93 | friend class TransmissionControlProtocolProvider; 94 | protected: 95 | common::uint16_t remotePort; 96 | common::uint32_t remoteIP; 97 | common::uint16_t localPort; 98 | common::uint32_t localIP; 99 | common::uint32_t sequenceNumber; 100 | common::uint32_t acknowledgementNumber; 101 | 102 | TransmissionControlProtocolProvider* backend; 103 | TransmissionControlProtocolHandler* handler; 104 | 105 | TransmissionControlProtocolSocketState state; 106 | public: 107 | TransmissionControlProtocolSocket(TransmissionControlProtocolProvider* backend); 108 | ~TransmissionControlProtocolSocket(); 109 | virtual bool HandleTransmissionControlProtocolMessage(common::uint8_t* data, common::uint16_t size); 110 | virtual void Send(common::uint8_t* data, common::uint16_t size); 111 | virtual void Disconnect(); 112 | }; 113 | 114 | 115 | class TransmissionControlProtocolProvider : InternetProtocolHandler 116 | { 117 | protected: 118 | TransmissionControlProtocolSocket* sockets[65535]; 119 | common::uint16_t numSockets; 120 | common::uint16_t freePort; 121 | 122 | public: 123 | TransmissionControlProtocolProvider(InternetProtocolProvider* backend); 124 | ~TransmissionControlProtocolProvider(); 125 | 126 | virtual bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE, 127 | common::uint8_t* internetprotocolPayload, common::uint32_t size); 128 | 129 | virtual TransmissionControlProtocolSocket* Connect(common::uint32_t ip, common::uint16_t port); 130 | virtual void Disconnect(TransmissionControlProtocolSocket* socket); 131 | virtual void Send(TransmissionControlProtocolSocket* socket, common::uint8_t* data, common::uint16_t size, 132 | common::uint16_t flags = 0); 133 | 134 | virtual TransmissionControlProtocolSocket* Listen(common::uint16_t port); 135 | virtual void Bind(TransmissionControlProtocolSocket* socket, TransmissionControlProtocolHandler* handler); 136 | }; 137 | 138 | 139 | 140 | 141 | } 142 | } 143 | 144 | 145 | #endif 146 | -------------------------------------------------------------------------------- /include/net/udp.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__NET__UDP_H 3 | #define __MYOS__NET__UDP_H 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace myos 11 | { 12 | namespace net 13 | { 14 | 15 | struct UserDatagramProtocolHeader 16 | { 17 | common::uint16_t srcPort; 18 | common::uint16_t dstPort; 19 | common::uint16_t length; 20 | common::uint16_t checksum; 21 | } __attribute__((packed)); 22 | 23 | 24 | 25 | class UserDatagramProtocolSocket; 26 | class UserDatagramProtocolProvider; 27 | 28 | 29 | 30 | class UserDatagramProtocolHandler 31 | { 32 | public: 33 | UserDatagramProtocolHandler(); 34 | ~UserDatagramProtocolHandler(); 35 | virtual void HandleUserDatagramProtocolMessage(UserDatagramProtocolSocket* socket, common::uint8_t* data, common::uint16_t size); 36 | }; 37 | 38 | 39 | 40 | class UserDatagramProtocolSocket 41 | { 42 | friend class UserDatagramProtocolProvider; 43 | protected: 44 | common::uint16_t remotePort; 45 | common::uint32_t remoteIP; 46 | common::uint16_t localPort; 47 | common::uint32_t localIP; 48 | UserDatagramProtocolProvider* backend; 49 | UserDatagramProtocolHandler* handler; 50 | bool listening; 51 | public: 52 | UserDatagramProtocolSocket(UserDatagramProtocolProvider* backend); 53 | ~UserDatagramProtocolSocket(); 54 | virtual void HandleUserDatagramProtocolMessage(common::uint8_t* data, common::uint16_t size); 55 | virtual void Send(common::uint8_t* data, common::uint16_t size); 56 | virtual void Disconnect(); 57 | }; 58 | 59 | 60 | class UserDatagramProtocolProvider : InternetProtocolHandler 61 | { 62 | protected: 63 | UserDatagramProtocolSocket* sockets[65535]; 64 | common::uint16_t numSockets; 65 | common::uint16_t freePort; 66 | 67 | public: 68 | UserDatagramProtocolProvider(InternetProtocolProvider* backend); 69 | ~UserDatagramProtocolProvider(); 70 | 71 | virtual bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE, 72 | common::uint8_t* internetprotocolPayload, common::uint32_t size); 73 | 74 | virtual UserDatagramProtocolSocket* Connect(common::uint32_t ip, common::uint16_t port); 75 | virtual UserDatagramProtocolSocket* Listen(common::uint16_t port); 76 | virtual void Disconnect(UserDatagramProtocolSocket* socket); 77 | virtual void Send(UserDatagramProtocolSocket* socket, common::uint8_t* data, common::uint16_t size); 78 | 79 | virtual void Bind(UserDatagramProtocolSocket* socket, UserDatagramProtocolHandler* handler); 80 | }; 81 | 82 | 83 | } 84 | } 85 | 86 | 87 | 88 | #endif -------------------------------------------------------------------------------- /include/syscalls.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MYOS__SYSCALLS_H 3 | #define __MYOS__SYSCALLS_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace myos 10 | { 11 | 12 | class SyscallHandler : public hardwarecommunication::InterruptHandler 13 | { 14 | 15 | public: 16 | SyscallHandler(hardwarecommunication::InterruptManager* interruptManager, myos::common::uint8_t InterruptNumber); 17 | ~SyscallHandler(); 18 | 19 | virtual myos::common::uint32_t HandleInterrupt(myos::common::uint32_t esp); 20 | 21 | }; 22 | 23 | 24 | } 25 | 26 | 27 | #endif -------------------------------------------------------------------------------- /linker.ld: -------------------------------------------------------------------------------- 1 | ENTRY(loader) 2 | OUTPUT_FORMAT(elf32-i386) 3 | OUTPUT_ARCH(i386:i386) 4 | 5 | SECTIONS 6 | { 7 | . = 0x0100000; 8 | 9 | .text : 10 | { 11 | *(.multiboot) 12 | *(.text*) 13 | *(.rodata) 14 | } 15 | 16 | .data : 17 | { 18 | start_ctors = .; 19 | KEEP(*( .init_array )); 20 | KEEP(*(SORT_BY_INIT_PRIORITY( .init_array.* ))); 21 | end_ctors = .; 22 | 23 | *(.data) 24 | } 25 | 26 | .bss : 27 | { 28 | *(.bss) 29 | } 30 | 31 | /DISCARD/ : { *(.fini_array*) *(.comment) } 32 | } 33 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | 2 | # sudo apt-get install g++ binutils libc6-dev-i386 3 | # sudo apt-get install VirtualBox grub-legacy xorriso 4 | 5 | GCCPARAMS = -m32 -Iinclude -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -Wno-write-strings 6 | ASPARAMS = --32 7 | LDPARAMS = -melf_i386 8 | 9 | objects = obj/loader.o \ 10 | obj/gdt.o \ 11 | obj/memorymanagement.o \ 12 | obj/drivers/driver.o \ 13 | obj/hardwarecommunication/port.o \ 14 | obj/hardwarecommunication/interruptstubs.o \ 15 | obj/hardwarecommunication/interrupts.o \ 16 | obj/syscalls.o \ 17 | obj/multitasking.o \ 18 | obj/drivers/amd_am79c973.o \ 19 | obj/hardwarecommunication/pci.o \ 20 | obj/drivers/keyboard.o \ 21 | obj/drivers/mouse.o \ 22 | obj/drivers/vga.o \ 23 | obj/drivers/ata.o \ 24 | obj/gui/widget.o \ 25 | obj/gui/window.o \ 26 | obj/gui/desktop.o \ 27 | obj/net/etherframe.o \ 28 | obj/net/arp.o \ 29 | obj/net/ipv4.o \ 30 | obj/net/icmp.o \ 31 | obj/net/udp.o \ 32 | obj/net/tcp.o \ 33 | obj/kernel.o 34 | 35 | 36 | run: mykernel.iso 37 | (killall VirtualBox && sleep 1) || true 38 | VirtualBox --startvm 'My Operating System' & 39 | 40 | obj/%.o: src/%.cpp 41 | mkdir -p $(@D) 42 | gcc $(GCCPARAMS) -c -o $@ $< 43 | 44 | obj/%.o: src/%.s 45 | mkdir -p $(@D) 46 | as $(ASPARAMS) -o $@ $< 47 | 48 | mykernel.bin: linker.ld $(objects) 49 | ld $(LDPARAMS) -T $< -o $@ $(objects) 50 | 51 | mykernel.iso: mykernel.bin 52 | mkdir iso 53 | mkdir iso/boot 54 | mkdir iso/boot/grub 55 | cp mykernel.bin iso/boot/mykernel.bin 56 | echo 'set timeout=0' > iso/boot/grub/grub.cfg 57 | echo 'set default=0' >> iso/boot/grub/grub.cfg 58 | echo '' >> iso/boot/grub/grub.cfg 59 | echo 'menuentry "My Operating System" {' >> iso/boot/grub/grub.cfg 60 | echo ' multiboot /boot/mykernel.bin' >> iso/boot/grub/grub.cfg 61 | echo ' boot' >> iso/boot/grub/grub.cfg 62 | echo '}' >> iso/boot/grub/grub.cfg 63 | grub-mkrescue --output=mykernel.iso iso 64 | rm -rf iso 65 | 66 | install: mykernel.bin 67 | sudo cp $< /boot/mykernel.bin 68 | 69 | .PHONY: clean 70 | clean: 71 | rm -rf obj mykernel.bin mykernel.iso 72 | -------------------------------------------------------------------------------- /src/drivers/amd_am79c973.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace myos; 4 | using namespace myos::common; 5 | using namespace myos::drivers; 6 | using namespace myos::hardwarecommunication; 7 | 8 | 9 | 10 | 11 | RawDataHandler::RawDataHandler(amd_am79c973* backend) 12 | { 13 | this->backend = backend; 14 | backend->SetHandler(this); 15 | } 16 | 17 | RawDataHandler::~RawDataHandler() 18 | { 19 | backend->SetHandler(0); 20 | } 21 | 22 | bool RawDataHandler::OnRawDataReceived(uint8_t* buffer, uint32_t size) 23 | { 24 | return false; 25 | } 26 | 27 | void RawDataHandler::Send(uint8_t* buffer, uint32_t size) 28 | { 29 | backend->Send(buffer, size); 30 | } 31 | 32 | 33 | 34 | 35 | 36 | void printf(char*); 37 | void printfHex(uint8_t); 38 | 39 | 40 | amd_am79c973::amd_am79c973(PeripheralComponentInterconnectDeviceDescriptor *dev, InterruptManager* interrupts) 41 | : Driver(), 42 | InterruptHandler(interrupts, dev->interrupt + interrupts->HardwareInterruptOffset()), 43 | MACAddress0Port(dev->portBase), 44 | MACAddress2Port(dev->portBase + 0x02), 45 | MACAddress4Port(dev->portBase + 0x04), 46 | registerDataPort(dev->portBase + 0x10), 47 | registerAddressPort(dev->portBase + 0x12), 48 | resetPort(dev->portBase + 0x14), 49 | busControlRegisterDataPort(dev->portBase + 0x16) 50 | { 51 | this->handler = 0; 52 | currentSendBuffer = 0; 53 | currentRecvBuffer = 0; 54 | 55 | uint64_t MAC0 = MACAddress0Port.Read() % 256; 56 | uint64_t MAC1 = MACAddress0Port.Read() / 256; 57 | uint64_t MAC2 = MACAddress2Port.Read() % 256; 58 | uint64_t MAC3 = MACAddress2Port.Read() / 256; 59 | uint64_t MAC4 = MACAddress4Port.Read() % 256; 60 | uint64_t MAC5 = MACAddress4Port.Read() / 256; 61 | 62 | uint64_t MAC = MAC5 << 40 63 | | MAC4 << 32 64 | | MAC3 << 24 65 | | MAC2 << 16 66 | | MAC1 << 8 67 | | MAC0; 68 | 69 | // 32 bit mode 70 | registerAddressPort.Write(20); 71 | busControlRegisterDataPort.Write(0x102); 72 | 73 | // STOP reset 74 | registerAddressPort.Write(0); 75 | registerDataPort.Write(0x04); 76 | 77 | // initBlock 78 | initBlock.mode = 0x0000; // promiscuous mode = false 79 | initBlock.reserved1 = 0; 80 | initBlock.numSendBuffers = 3; 81 | initBlock.reserved2 = 0; 82 | initBlock.numRecvBuffers = 3; 83 | initBlock.physicalAddress = MAC; 84 | initBlock.reserved3 = 0; 85 | initBlock.logicalAddress = 0; 86 | 87 | sendBufferDescr = (BufferDescriptor*)((((uint32_t)&sendBufferDescrMemory[0]) + 15) & ~((uint32_t)0xF)); 88 | initBlock.sendBufferDescrAddress = (uint32_t)sendBufferDescr; 89 | recvBufferDescr = (BufferDescriptor*)((((uint32_t)&recvBufferDescrMemory[0]) + 15) & ~((uint32_t)0xF)); 90 | initBlock.recvBufferDescrAddress = (uint32_t)recvBufferDescr; 91 | 92 | for(uint8_t i = 0; i < 8; i++) 93 | { 94 | sendBufferDescr[i].address = (((uint32_t)&sendBuffers[i]) + 15 ) & ~(uint32_t)0xF; 95 | sendBufferDescr[i].flags = 0x7FF 96 | | 0xF000; 97 | sendBufferDescr[i].flags2 = 0; 98 | sendBufferDescr[i].avail = 0; 99 | 100 | recvBufferDescr[i].address = (((uint32_t)&recvBuffers[i]) + 15 ) & ~(uint32_t)0xF; 101 | recvBufferDescr[i].flags = 0xF7FF 102 | | 0x80000000; 103 | recvBufferDescr[i].flags2 = 0; 104 | sendBufferDescr[i].avail = 0; 105 | } 106 | 107 | registerAddressPort.Write(1); 108 | registerDataPort.Write( (uint32_t)(&initBlock) & 0xFFFF ); 109 | registerAddressPort.Write(2); 110 | registerDataPort.Write( ((uint32_t)(&initBlock) >> 16) & 0xFFFF ); 111 | 112 | } 113 | 114 | amd_am79c973::~amd_am79c973() 115 | { 116 | } 117 | 118 | void amd_am79c973::Activate() 119 | { 120 | registerAddressPort.Write(0); 121 | registerDataPort.Write(0x41); 122 | 123 | registerAddressPort.Write(4); 124 | uint32_t temp = registerDataPort.Read(); 125 | registerAddressPort.Write(4); 126 | registerDataPort.Write(temp | 0xC00); 127 | 128 | registerAddressPort.Write(0); 129 | registerDataPort.Write(0x42); 130 | } 131 | 132 | int amd_am79c973::Reset() 133 | { 134 | resetPort.Read(); 135 | resetPort.Write(0); 136 | return 10; 137 | } 138 | 139 | 140 | 141 | uint32_t amd_am79c973::HandleInterrupt(common::uint32_t esp) 142 | { 143 | registerAddressPort.Write(0); 144 | uint32_t temp = registerDataPort.Read(); 145 | 146 | if((temp & 0x8000) == 0x8000) printf("AMD am79c973 ERROR\n"); 147 | if((temp & 0x2000) == 0x2000) printf("AMD am79c973 COLLISION ERROR\n"); 148 | if((temp & 0x1000) == 0x1000) printf("AMD am79c973 MISSED FRAME\n"); 149 | if((temp & 0x0800) == 0x0800) printf("AMD am79c973 MEMORY ERROR\n"); 150 | if((temp & 0x0400) == 0x0400) Receive(); 151 | if((temp & 0x0200) == 0x0200) printf(" SENT"); 152 | 153 | // acknoledge 154 | registerAddressPort.Write(0); 155 | registerDataPort.Write(temp); 156 | 157 | if((temp & 0x0100) == 0x0100) printf("AMD am79c973 INIT DONE\n"); 158 | 159 | return esp; 160 | } 161 | 162 | 163 | void amd_am79c973::Send(uint8_t* buffer, int size) 164 | { 165 | int sendDescriptor = currentSendBuffer; 166 | currentSendBuffer = (currentSendBuffer + 1) % 8; 167 | 168 | if(size > 1518) 169 | size = 1518; 170 | 171 | for(uint8_t *src = buffer + size -1, 172 | *dst = (uint8_t*)(sendBufferDescr[sendDescriptor].address + size -1); 173 | src >= buffer; src--, dst--) 174 | *dst = *src; 175 | 176 | printf("\nSEND: "); 177 | for(int i = 14+20; i < (size>64?64:size); i++) 178 | { 179 | printfHex(buffer[i]); 180 | printf(" "); 181 | } 182 | 183 | sendBufferDescr[sendDescriptor].avail = 0; 184 | sendBufferDescr[sendDescriptor].flags2 = 0; 185 | sendBufferDescr[sendDescriptor].flags = 0x8300F000 186 | | ((uint16_t)((-size) & 0xFFF)); 187 | registerAddressPort.Write(0); 188 | registerDataPort.Write(0x48); 189 | } 190 | 191 | void amd_am79c973::Receive() 192 | { 193 | printf("\nRECV: "); 194 | 195 | for(; (recvBufferDescr[currentRecvBuffer].flags & 0x80000000) == 0; 196 | currentRecvBuffer = (currentRecvBuffer + 1) % 8) 197 | { 198 | if(!(recvBufferDescr[currentRecvBuffer].flags & 0x40000000) 199 | && (recvBufferDescr[currentRecvBuffer].flags & 0x03000000) == 0x03000000) 200 | 201 | { 202 | uint32_t size = recvBufferDescr[currentRecvBuffer].flags & 0xFFF; 203 | if(size > 64) // remove checksum 204 | size -= 4; 205 | 206 | uint8_t* buffer = (uint8_t*)(recvBufferDescr[currentRecvBuffer].address); 207 | 208 | for(int i = 14+20; i < (size>64?64:size); i++) 209 | { 210 | printfHex(buffer[i]); 211 | printf(" "); 212 | } 213 | 214 | if(handler != 0) 215 | if(handler->OnRawDataReceived(buffer, size)) 216 | Send(buffer, size); 217 | } 218 | 219 | recvBufferDescr[currentRecvBuffer].flags2 = 0; 220 | recvBufferDescr[currentRecvBuffer].flags = 0x8000F7FF; 221 | } 222 | } 223 | 224 | void amd_am79c973::SetHandler(RawDataHandler* handler) 225 | { 226 | this->handler = handler; 227 | } 228 | 229 | uint64_t amd_am79c973::GetMACAddress() 230 | { 231 | return initBlock.physicalAddress; 232 | } 233 | 234 | void amd_am79c973::SetIPAddress(uint32_t ip) 235 | { 236 | initBlock.logicalAddress = ip; 237 | } 238 | 239 | uint32_t amd_am79c973::GetIPAddress() 240 | { 241 | return initBlock.logicalAddress; 242 | } -------------------------------------------------------------------------------- /src/drivers/ata.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace myos; 4 | using namespace myos::common; 5 | using namespace myos::drivers; 6 | 7 | 8 | void printf(char* str); 9 | void printfHex(uint8_t); 10 | 11 | AdvancedTechnologyAttachment::AdvancedTechnologyAttachment(bool master, common::uint16_t portBase) 12 | : dataPort(portBase), 13 | errorPort(portBase + 0x1), 14 | sectorCountPort(portBase + 0x2), 15 | lbaLowPort(portBase + 0x3), 16 | lbaMidPort(portBase + 0x4), 17 | lbaHiPort(portBase + 0x5), 18 | devicePort(portBase + 0x6), 19 | commandPort(portBase + 0x7), 20 | controlPort(portBase + 0x206) 21 | { 22 | this->master = master; 23 | } 24 | 25 | AdvancedTechnologyAttachment::~AdvancedTechnologyAttachment() 26 | { 27 | } 28 | 29 | void AdvancedTechnologyAttachment::Identify() 30 | { 31 | devicePort.Write(master ? 0xA0 : 0xB0); 32 | controlPort.Write(0); 33 | 34 | devicePort.Write(0xA0); 35 | uint8_t status = commandPort.Read(); 36 | if(status == 0xFF) 37 | return; 38 | 39 | 40 | devicePort.Write(master ? 0xA0 : 0xB0); 41 | sectorCountPort.Write(0); 42 | lbaLowPort.Write(0); 43 | lbaMidPort.Write(0); 44 | lbaHiPort.Write(0); 45 | commandPort.Write(0xEC); // identify command 46 | 47 | 48 | status = commandPort.Read(); 49 | if(status == 0x00) 50 | return; 51 | 52 | while(((status & 0x80) == 0x80) 53 | && ((status & 0x01) != 0x01)) 54 | status = commandPort.Read(); 55 | 56 | if(status & 0x01) 57 | { 58 | printf("ERROR"); 59 | return; 60 | } 61 | 62 | for(int i = 0; i < 256; i++) 63 | { 64 | uint16_t data = dataPort.Read(); 65 | char *text = " \0"; 66 | text[0] = (data >> 8) & 0xFF; 67 | text[1] = data & 0xFF; 68 | printf(text); 69 | } 70 | printf("\n"); 71 | } 72 | 73 | void AdvancedTechnologyAttachment::Read28(common::uint32_t sectorNum, int count) 74 | { 75 | if(sectorNum > 0x0FFFFFFF) 76 | return; 77 | 78 | devicePort.Write( (master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) ); 79 | errorPort.Write(0); 80 | sectorCountPort.Write(1); 81 | lbaLowPort.Write( sectorNum & 0x000000FF ); 82 | lbaMidPort.Write( (sectorNum & 0x0000FF00) >> 8); 83 | lbaLowPort.Write( (sectorNum & 0x00FF0000) >> 16 ); 84 | commandPort.Write(0x20); 85 | 86 | uint8_t status = commandPort.Read(); 87 | while(((status & 0x80) == 0x80) 88 | && ((status & 0x01) != 0x01)) 89 | status = commandPort.Read(); 90 | 91 | if(status & 0x01) 92 | { 93 | printf("ERROR"); 94 | return; 95 | } 96 | 97 | 98 | printf("Reading ATA Drive: "); 99 | 100 | for(int i = 0; i < count; i += 2) 101 | { 102 | uint16_t wdata = dataPort.Read(); 103 | 104 | char *text = " \0"; 105 | text[0] = wdata & 0xFF; 106 | 107 | if(i+1 < count) 108 | text[1] = (wdata >> 8) & 0xFF; 109 | else 110 | text[1] = '\0'; 111 | 112 | printf(text); 113 | } 114 | 115 | for(int i = count + (count%2); i < 512; i += 2) 116 | dataPort.Read(); 117 | } 118 | 119 | void AdvancedTechnologyAttachment::Write28(common::uint32_t sectorNum, common::uint8_t* data, common::uint32_t count) 120 | { 121 | if(sectorNum > 0x0FFFFFFF) 122 | return; 123 | if(count > 512) 124 | return; 125 | 126 | 127 | devicePort.Write( (master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) ); 128 | errorPort.Write(0); 129 | sectorCountPort.Write(1); 130 | lbaLowPort.Write( sectorNum & 0x000000FF ); 131 | lbaMidPort.Write( (sectorNum & 0x0000FF00) >> 8); 132 | lbaLowPort.Write( (sectorNum & 0x00FF0000) >> 16 ); 133 | commandPort.Write(0x30); 134 | 135 | 136 | printf("Writing to ATA Drive: "); 137 | 138 | for(int i = 0; i < count; i += 2) 139 | { 140 | uint16_t wdata = data[i]; 141 | if(i+1 < count) 142 | wdata |= ((uint16_t)data[i+1]) << 8; 143 | dataPort.Write(wdata); 144 | 145 | char *text = " \0"; 146 | text[0] = (wdata >> 8) & 0xFF; 147 | text[1] = wdata & 0xFF; 148 | printf(text); 149 | } 150 | 151 | for(int i = count + (count%2); i < 512; i += 2) 152 | dataPort.Write(0x0000); 153 | 154 | } 155 | 156 | void AdvancedTechnologyAttachment::Flush() 157 | { 158 | devicePort.Write( master ? 0xE0 : 0xF0 ); 159 | commandPort.Write(0xE7); 160 | 161 | uint8_t status = commandPort.Read(); 162 | if(status == 0x00) 163 | return; 164 | 165 | while(((status & 0x80) == 0x80) 166 | && ((status & 0x01) != 0x01)) 167 | status = commandPort.Read(); 168 | 169 | if(status & 0x01) 170 | { 171 | printf("ERROR"); 172 | return; 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/drivers/driver.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace myos::drivers; 4 | 5 | Driver::Driver() 6 | { 7 | } 8 | 9 | Driver::~Driver() 10 | { 11 | } 12 | 13 | void Driver::Activate() 14 | { 15 | } 16 | 17 | int Driver::Reset() 18 | { 19 | return 0; 20 | } 21 | 22 | void Driver::Deactivate() 23 | { 24 | } 25 | 26 | 27 | 28 | 29 | DriverManager::DriverManager() 30 | { 31 | numDrivers = 0; 32 | } 33 | 34 | void DriverManager::AddDriver(Driver* drv) 35 | { 36 | drivers[numDrivers] = drv; 37 | numDrivers++; 38 | } 39 | 40 | void DriverManager::ActivateAll() 41 | { 42 | for(int i = 0; i < numDrivers; i++) 43 | drivers[i]->Activate(); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /src/drivers/keyboard.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos::common; 5 | using namespace myos::drivers; 6 | using namespace myos::hardwarecommunication; 7 | 8 | 9 | KeyboardEventHandler::KeyboardEventHandler() 10 | { 11 | } 12 | 13 | void KeyboardEventHandler::OnKeyDown(char) 14 | { 15 | } 16 | 17 | void KeyboardEventHandler::OnKeyUp(char) 18 | { 19 | } 20 | 21 | 22 | 23 | 24 | 25 | KeyboardDriver::KeyboardDriver(InterruptManager* manager, KeyboardEventHandler *handler) 26 | : InterruptHandler(manager, 0x21), 27 | dataport(0x60), 28 | commandport(0x64) 29 | { 30 | this->handler = handler; 31 | } 32 | 33 | KeyboardDriver::~KeyboardDriver() 34 | { 35 | } 36 | 37 | void printf(char*); 38 | void printfHex(uint8_t); 39 | 40 | void KeyboardDriver::Activate() 41 | { 42 | while(commandport.Read() & 0x1) 43 | dataport.Read(); 44 | commandport.Write(0xae); // activate interrupts 45 | commandport.Write(0x20); // command 0x20 = read controller command byte 46 | uint8_t status = (dataport.Read() | 1) & ~0x10; 47 | commandport.Write(0x60); // command 0x60 = set controller command byte 48 | dataport.Write(status); 49 | dataport.Write(0xf4); 50 | } 51 | 52 | uint32_t KeyboardDriver::HandleInterrupt(uint32_t esp) 53 | { 54 | uint8_t key = dataport.Read(); 55 | 56 | if(handler == 0) 57 | return esp; 58 | 59 | if(key < 0x80) 60 | { 61 | switch(key) 62 | { 63 | case 0x02: handler->OnKeyDown('1'); break; 64 | case 0x03: handler->OnKeyDown('2'); break; 65 | case 0x04: handler->OnKeyDown('3'); break; 66 | case 0x05: handler->OnKeyDown('4'); break; 67 | case 0x06: handler->OnKeyDown('5'); break; 68 | case 0x07: handler->OnKeyDown('6'); break; 69 | case 0x08: handler->OnKeyDown('7'); break; 70 | case 0x09: handler->OnKeyDown('8'); break; 71 | case 0x0A: handler->OnKeyDown('9'); break; 72 | case 0x0B: handler->OnKeyDown('0'); break; 73 | 74 | case 0x10: handler->OnKeyDown('q'); break; 75 | case 0x11: handler->OnKeyDown('w'); break; 76 | case 0x12: handler->OnKeyDown('e'); break; 77 | case 0x13: handler->OnKeyDown('r'); break; 78 | case 0x14: handler->OnKeyDown('t'); break; 79 | case 0x15: handler->OnKeyDown('z'); break; 80 | case 0x16: handler->OnKeyDown('u'); break; 81 | case 0x17: handler->OnKeyDown('i'); break; 82 | case 0x18: handler->OnKeyDown('o'); break; 83 | case 0x19: handler->OnKeyDown('p'); break; 84 | 85 | case 0x1E: handler->OnKeyDown('a'); break; 86 | case 0x1F: handler->OnKeyDown('s'); break; 87 | case 0x20: handler->OnKeyDown('d'); break; 88 | case 0x21: handler->OnKeyDown('f'); break; 89 | case 0x22: handler->OnKeyDown('g'); break; 90 | case 0x23: handler->OnKeyDown('h'); break; 91 | case 0x24: handler->OnKeyDown('j'); break; 92 | case 0x25: handler->OnKeyDown('k'); break; 93 | case 0x26: handler->OnKeyDown('l'); break; 94 | 95 | case 0x2C: handler->OnKeyDown('y'); break; 96 | case 0x2D: handler->OnKeyDown('x'); break; 97 | case 0x2E: handler->OnKeyDown('c'); break; 98 | case 0x2F: handler->OnKeyDown('v'); break; 99 | case 0x30: handler->OnKeyDown('b'); break; 100 | case 0x31: handler->OnKeyDown('n'); break; 101 | case 0x32: handler->OnKeyDown('m'); break; 102 | case 0x33: handler->OnKeyDown(','); break; 103 | case 0x34: handler->OnKeyDown('.'); break; 104 | case 0x35: handler->OnKeyDown('-'); break; 105 | 106 | case 0x1C: handler->OnKeyDown('\n'); break; 107 | case 0x39: handler->OnKeyDown(' '); break; 108 | 109 | default: 110 | { 111 | printf("KEYBOARD 0x"); 112 | printfHex(key); 113 | break; 114 | } 115 | } 116 | } 117 | return esp; 118 | } 119 | -------------------------------------------------------------------------------- /src/drivers/mouse.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | using namespace myos::common; 6 | using namespace myos::drivers; 7 | using namespace myos::hardwarecommunication; 8 | 9 | 10 | void printf(char*); 11 | 12 | MouseEventHandler::MouseEventHandler() 13 | { 14 | } 15 | 16 | void MouseEventHandler::OnActivate() 17 | { 18 | } 19 | 20 | void MouseEventHandler::OnMouseDown(uint8_t button) 21 | { 22 | } 23 | 24 | void MouseEventHandler::OnMouseUp(uint8_t button) 25 | { 26 | } 27 | 28 | void MouseEventHandler::OnMouseMove(int x, int y) 29 | { 30 | } 31 | 32 | 33 | 34 | 35 | 36 | MouseDriver::MouseDriver(InterruptManager* manager, MouseEventHandler* handler) 37 | : InterruptHandler(manager, 0x2C), 38 | dataport(0x60), 39 | commandport(0x64) 40 | { 41 | this->handler = handler; 42 | } 43 | 44 | MouseDriver::~MouseDriver() 45 | { 46 | } 47 | 48 | void MouseDriver::Activate() 49 | { 50 | offset = 0; 51 | buttons = 0; 52 | 53 | if(handler != 0) 54 | handler->OnActivate(); 55 | 56 | commandport.Write(0xA8); 57 | commandport.Write(0x20); // command 0x60 = read controller command byte 58 | uint8_t status = dataport.Read() | 2; 59 | commandport.Write(0x60); // command 0x60 = set controller command byte 60 | dataport.Write(status); 61 | 62 | commandport.Write(0xD4); 63 | dataport.Write(0xF4); 64 | dataport.Read(); 65 | } 66 | 67 | uint32_t MouseDriver::HandleInterrupt(uint32_t esp) 68 | { 69 | uint8_t status = commandport.Read(); 70 | if (!(status & 0x20)) 71 | return esp; 72 | 73 | buffer[offset] = dataport.Read(); 74 | 75 | if(handler == 0) 76 | return esp; 77 | 78 | offset = (offset + 1) % 3; 79 | 80 | if(offset == 0) 81 | { 82 | if(buffer[1] != 0 || buffer[2] != 0) 83 | { 84 | handler->OnMouseMove((int8_t)buffer[1], -((int8_t)buffer[2])); 85 | } 86 | 87 | for(uint8_t i = 0; i < 3; i++) 88 | { 89 | if((buffer[0] & (0x1<OnMouseUp(i+1); 93 | else 94 | handler->OnMouseDown(i+1); 95 | } 96 | } 97 | buttons = buffer[0]; 98 | } 99 | 100 | return esp; 101 | } 102 | -------------------------------------------------------------------------------- /src/drivers/vga.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos::common; 5 | using namespace myos::drivers; 6 | 7 | 8 | 9 | VideoGraphicsArray::VideoGraphicsArray() : 10 | miscPort(0x3c2), 11 | crtcIndexPort(0x3d4), 12 | crtcDataPort(0x3d5), 13 | sequencerIndexPort(0x3c4), 14 | sequencerDataPort(0x3c5), 15 | graphicsControllerIndexPort(0x3ce), 16 | graphicsControllerDataPort(0x3cf), 17 | attributeControllerIndexPort(0x3c0), 18 | attributeControllerReadPort(0x3c1), 19 | attributeControllerWritePort(0x3c0), 20 | attributeControllerResetPort(0x3da) 21 | { 22 | } 23 | 24 | VideoGraphicsArray::~VideoGraphicsArray() 25 | { 26 | } 27 | 28 | 29 | 30 | void VideoGraphicsArray::WriteRegisters(uint8_t* registers) 31 | { 32 | // misc 33 | miscPort.Write(*(registers++)); 34 | 35 | // sequencer 36 | for(uint8_t i = 0; i < 5; i++) 37 | { 38 | sequencerIndexPort.Write(i); 39 | sequencerDataPort.Write(*(registers++)); 40 | } 41 | 42 | // cathode ray tube controller 43 | crtcIndexPort.Write(0x03); 44 | crtcDataPort.Write(crtcDataPort.Read() | 0x80); 45 | crtcIndexPort.Write(0x11); 46 | crtcDataPort.Write(crtcDataPort.Read() & ~0x80); 47 | 48 | registers[0x03] = registers[0x03] | 0x80; 49 | registers[0x11] = registers[0x11] & ~0x80; 50 | 51 | for(uint8_t i = 0; i < 25; i++) 52 | { 53 | crtcIndexPort.Write(i); 54 | crtcDataPort.Write(*(registers++)); 55 | } 56 | 57 | // graphics controller 58 | for(uint8_t i = 0; i < 9; i++) 59 | { 60 | graphicsControllerIndexPort.Write(i); 61 | graphicsControllerDataPort.Write(*(registers++)); 62 | } 63 | 64 | // attribute controller 65 | for(uint8_t i = 0; i < 21; i++) 66 | { 67 | attributeControllerResetPort.Read(); 68 | attributeControllerIndexPort.Write(i); 69 | attributeControllerWritePort.Write(*(registers++)); 70 | } 71 | 72 | attributeControllerResetPort.Read(); 73 | attributeControllerIndexPort.Write(0x20); 74 | 75 | } 76 | 77 | bool VideoGraphicsArray::SupportsMode(uint32_t width, uint32_t height, uint32_t colordepth) 78 | { 79 | return width == 320 && height == 200 && colordepth == 8; 80 | } 81 | 82 | bool VideoGraphicsArray::SetMode(uint32_t width, uint32_t height, uint32_t colordepth) 83 | { 84 | if(!SupportsMode(width, height, colordepth)) 85 | return false; 86 | 87 | unsigned char g_320x200x256[] = 88 | { 89 | /* MISC */ 90 | 0x63, 91 | /* SEQ */ 92 | 0x03, 0x01, 0x0F, 0x00, 0x0E, 93 | /* CRTC */ 94 | 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F, 95 | 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 96 | 0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3, 97 | 0xFF, 98 | /* GC */ 99 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, 100 | 0xFF, 101 | /* AC */ 102 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 103 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 104 | 0x41, 0x00, 0x0F, 0x00, 0x00 105 | }; 106 | 107 | WriteRegisters(g_320x200x256); 108 | return true; 109 | } 110 | 111 | 112 | uint8_t* VideoGraphicsArray::GetFrameBufferSegment() 113 | { 114 | graphicsControllerIndexPort.Write(0x06); 115 | uint8_t segmentNumber = graphicsControllerDataPort.Read() & (3<<2); 116 | switch(segmentNumber) 117 | { 118 | default: 119 | case 0<<2: return (uint8_t*)0x00000; 120 | case 1<<2: return (uint8_t*)0xA0000; 121 | case 2<<2: return (uint8_t*)0xB0000; 122 | case 3<<2: return (uint8_t*)0xB8000; 123 | } 124 | } 125 | 126 | void VideoGraphicsArray::PutPixel(int32_t x, int32_t y, uint8_t colorIndex) 127 | { 128 | if(x < 0 || 320 <= x 129 | || y < 0 || 200 <= y) 130 | return; 131 | 132 | uint8_t* pixelAddress = GetFrameBufferSegment() + 320*y + x; 133 | *pixelAddress = colorIndex; 134 | } 135 | 136 | uint8_t VideoGraphicsArray::GetColorIndex(uint8_t r, uint8_t g, uint8_t b) 137 | { 138 | if(r == 0x00 && g == 0x00 && b == 0x00) return 0x00; // black 139 | if(r == 0x00 && g == 0x00 && b == 0xA8) return 0x01; // blue 140 | if(r == 0x00 && g == 0xA8 && b == 0x00) return 0x02; // green 141 | if(r == 0xA8 && g == 0x00 && b == 0x00) return 0x04; // red 142 | if(r == 0xFF && g == 0xFF && b == 0xFF) return 0x3F; // white 143 | return 0x00; 144 | } 145 | 146 | void VideoGraphicsArray::PutPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b) 147 | { 148 | PutPixel(x,y, GetColorIndex(r,g,b)); 149 | } 150 | 151 | void VideoGraphicsArray::FillRectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t r, uint8_t g, uint8_t b) 152 | { 153 | for(int32_t Y = y; Y < y+h; Y++) 154 | for(int32_t X = x; X < x+w; X++) 155 | PutPixel(X, Y, r, g, b); 156 | } 157 | 158 | -------------------------------------------------------------------------------- /src/gdt.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace myos; 4 | using namespace myos::common; 5 | 6 | 7 | GlobalDescriptorTable::GlobalDescriptorTable() 8 | : nullSegmentSelector(0, 0, 0), 9 | unusedSegmentSelector(0, 0, 0), 10 | codeSegmentSelector(0, 64*1024*1024, 0x9A), 11 | dataSegmentSelector(0, 64*1024*1024, 0x92) 12 | { 13 | uint32_t i[2]; 14 | i[1] = (uint32_t)this; 15 | i[0] = sizeof(GlobalDescriptorTable) << 16; 16 | asm volatile("lgdt (%0)": :"p" (((uint8_t *) i)+2)); 17 | } 18 | 19 | GlobalDescriptorTable::~GlobalDescriptorTable() 20 | { 21 | } 22 | 23 | uint16_t GlobalDescriptorTable::DataSegmentSelector() 24 | { 25 | return (uint8_t*)&dataSegmentSelector - (uint8_t*)this; 26 | } 27 | 28 | uint16_t GlobalDescriptorTable::CodeSegmentSelector() 29 | { 30 | return (uint8_t*)&codeSegmentSelector - (uint8_t*)this; 31 | } 32 | 33 | GlobalDescriptorTable::SegmentDescriptor::SegmentDescriptor(uint32_t base, uint32_t limit, uint8_t type) 34 | { 35 | uint8_t* target = (uint8_t*)this; 36 | 37 | if (limit <= 65536) 38 | { 39 | // 16-bit address space 40 | target[6] = 0x40; 41 | } 42 | else 43 | { 44 | // 32-bit address space 45 | // Now we have to squeeze the (32-bit) limit into 2.5 regiters (20-bit). 46 | // This is done by discarding the 12 least significant bits, but this 47 | // is only legal, if they are all ==1, so they are implicitly still there 48 | 49 | // so if the last bits aren't all 1, we have to set them to 1, but this 50 | // would increase the limit (cannot do that, because we might go beyond 51 | // the physical limit or get overlap with other segments) so we have to 52 | // compensate this by decreasing a higher bit (and might have up to 53 | // 4095 wasted bytes behind the used memory) 54 | 55 | if((limit & 0xFFF) != 0xFFF) 56 | limit = (limit >> 12)-1; 57 | else 58 | limit = limit >> 12; 59 | 60 | target[6] = 0xC0; 61 | } 62 | 63 | // Encode the limit 64 | target[0] = limit & 0xFF; 65 | target[1] = (limit >> 8) & 0xFF; 66 | target[6] |= (limit >> 16) & 0xF; 67 | 68 | // Encode the base 69 | target[2] = base & 0xFF; 70 | target[3] = (base >> 8) & 0xFF; 71 | target[4] = (base >> 16) & 0xFF; 72 | target[7] = (base >> 24) & 0xFF; 73 | 74 | // Type 75 | target[5] = type; 76 | } 77 | 78 | uint32_t GlobalDescriptorTable::SegmentDescriptor::Base() 79 | { 80 | uint8_t* target = (uint8_t*)this; 81 | 82 | uint32_t result = target[7]; 83 | result = (result << 8) + target[4]; 84 | result = (result << 8) + target[3]; 85 | result = (result << 8) + target[2]; 86 | 87 | return result; 88 | } 89 | 90 | uint32_t GlobalDescriptorTable::SegmentDescriptor::Limit() 91 | { 92 | uint8_t* target = (uint8_t*)this; 93 | 94 | uint32_t result = target[6] & 0xF; 95 | result = (result << 8) + target[1]; 96 | result = (result << 8) + target[0]; 97 | 98 | if((target[6] & 0xC0) == 0xC0) 99 | result = (result << 12) | 0xFFF; 100 | 101 | return result; 102 | } 103 | 104 | -------------------------------------------------------------------------------- /src/gui/desktop.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos; 5 | using namespace myos::common; 6 | using namespace myos::gui; 7 | 8 | 9 | Desktop::Desktop(common::int32_t w, common::int32_t h, 10 | common::uint8_t r, common::uint8_t g, common::uint8_t b) 11 | : CompositeWidget(0,0,0, w,h,r,g,b), 12 | MouseEventHandler() 13 | { 14 | MouseX = w/2; 15 | MouseY = h/2; 16 | } 17 | 18 | Desktop::~Desktop() 19 | { 20 | } 21 | 22 | void Desktop::Draw(common::GraphicsContext* gc) 23 | { 24 | CompositeWidget::Draw(gc); 25 | 26 | for(int i = 0; i < 4; i++) 27 | { 28 | gc -> PutPixel(MouseX-i, MouseY, 0xFF, 0xFF, 0xFF); 29 | gc -> PutPixel(MouseX+i, MouseY, 0xFF, 0xFF, 0xFF); 30 | gc -> PutPixel(MouseX, MouseY-i, 0xFF, 0xFF, 0xFF); 31 | gc -> PutPixel(MouseX, MouseY+i, 0xFF, 0xFF, 0xFF); 32 | } 33 | } 34 | 35 | void Desktop::OnMouseDown(myos::common::uint8_t button) 36 | { 37 | CompositeWidget::OnMouseDown(MouseX, MouseY, button); 38 | } 39 | 40 | void Desktop::OnMouseUp(myos::common::uint8_t button) 41 | { 42 | CompositeWidget::OnMouseUp(MouseX, MouseY, button); 43 | } 44 | 45 | void Desktop::OnMouseMove(int x, int y) 46 | { 47 | x /= 4; 48 | y /= 4; 49 | 50 | int32_t newMouseX = MouseX + x; 51 | if(newMouseX < 0) newMouseX = 0; 52 | if(newMouseX >= w) newMouseX = w - 1; 53 | 54 | int32_t newMouseY = MouseY + y; 55 | if(newMouseY < 0) newMouseY = 0; 56 | if(newMouseY >= h) newMouseY = h - 1; 57 | 58 | CompositeWidget::OnMouseMove(MouseX, MouseY, newMouseX, newMouseY); 59 | 60 | MouseX = newMouseX; 61 | MouseY = newMouseY; 62 | } 63 | -------------------------------------------------------------------------------- /src/gui/widget.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos::common; 5 | using namespace myos::gui; 6 | 7 | 8 | Widget::Widget(Widget* parent, int32_t x, int32_t y, int32_t w, int32_t h, 9 | uint8_t r, uint8_t g, uint8_t b) 10 | : KeyboardEventHandler() 11 | { 12 | this->parent = parent; 13 | this->x = x; 14 | this->y = y; 15 | this->w = w; 16 | this->h = h; 17 | this->r = r; 18 | this->g = g; 19 | this->b = b; 20 | this->Focussable = true; 21 | } 22 | 23 | Widget::~Widget() 24 | { 25 | } 26 | 27 | void Widget::GetFocus(Widget* widget) 28 | { 29 | if(parent != 0) 30 | parent->GetFocus(widget); 31 | } 32 | 33 | void Widget::ModelToScreen(common::int32_t &x, common::int32_t& y) 34 | { 35 | if(parent != 0) 36 | parent->ModelToScreen(x,y); 37 | x += this->x; 38 | y += this->y; 39 | } 40 | 41 | void Widget::Draw(GraphicsContext* gc) 42 | { 43 | int X = 0; 44 | int Y = 0; 45 | ModelToScreen(X,Y); 46 | gc->FillRectangle(X,Y,w,h, r,g,b); 47 | } 48 | 49 | void Widget::OnMouseDown(common::int32_t x, common::int32_t y, common::uint8_t button) 50 | { 51 | if(Focussable) 52 | GetFocus(this); 53 | } 54 | 55 | bool Widget::ContainsCoordinate(common::int32_t x, common::int32_t y) 56 | { 57 | return this->x <= x && x < this->x + this->w 58 | && this->y <= y && y < this->y + this->h; 59 | } 60 | 61 | void Widget::OnMouseUp(common::int32_t x, common::int32_t y, common::uint8_t button) 62 | { 63 | } 64 | 65 | void Widget::OnMouseMove(common::int32_t oldx, common::int32_t oldy, common::int32_t newx, common::int32_t newy) 66 | { 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | CompositeWidget::CompositeWidget(Widget* parent, 78 | common::int32_t x, common::int32_t y, common::int32_t w, common::int32_t h, 79 | common::uint8_t r, common::uint8_t g, common::uint8_t b) 80 | : Widget(parent, x,y,w,h, r,g,b) 81 | { 82 | focussedChild = 0; 83 | numChildren = 0; 84 | } 85 | 86 | CompositeWidget::~CompositeWidget() 87 | { 88 | } 89 | 90 | void CompositeWidget::GetFocus(Widget* widget) 91 | { 92 | this->focussedChild = widget; 93 | if(parent != 0) 94 | parent->GetFocus(this); 95 | } 96 | 97 | bool CompositeWidget::AddChild(Widget* child) 98 | { 99 | if(numChildren >= 100) 100 | return false; 101 | children[numChildren++] = child; 102 | return true; 103 | } 104 | 105 | 106 | void CompositeWidget::Draw(GraphicsContext* gc) 107 | { 108 | Widget::Draw(gc); 109 | for(int i = numChildren-1; i >= 0; --i) 110 | children[i]->Draw(gc); 111 | } 112 | 113 | 114 | void CompositeWidget::OnMouseDown(int32_t x, int32_t y, common::uint8_t button) 115 | { 116 | for(int i = 0; i < numChildren; ++i) 117 | if(children[i]->ContainsCoordinate(x - this->x, y - this->y)) 118 | { 119 | children[i]->OnMouseDown(x - this->x, y - this->y, button); 120 | break; 121 | } 122 | } 123 | 124 | void CompositeWidget::OnMouseUp(int32_t x, int32_t y, common::uint8_t button) 125 | { 126 | for(int i = 0; i < numChildren; ++i) 127 | if(children[i]->ContainsCoordinate(x - this->x, y - this->y)) 128 | { 129 | children[i]->OnMouseUp(x - this->x, y - this->y, button); 130 | break; 131 | } 132 | } 133 | 134 | void CompositeWidget::OnMouseMove(int32_t oldx, int32_t oldy, int32_t newx, int32_t newy) 135 | { 136 | int firstchild = -1; 137 | for(int i = 0; i < numChildren; ++i) 138 | if(children[i]->ContainsCoordinate(oldx - this->x, oldy - this->y)) 139 | { 140 | children[i]->OnMouseMove(oldx - this->x, oldy - this->y, newx - this->x, newy - this->y); 141 | firstchild = i; 142 | break; 143 | } 144 | 145 | for(int i = 0; i < numChildren; ++i) 146 | if(children[i]->ContainsCoordinate(newx - this->x, newy - this->y)) 147 | { 148 | if(firstchild != i) 149 | children[i]->OnMouseMove(oldx - this->x, oldy - this->y, newx - this->x, newy - this->y); 150 | break; 151 | } 152 | } 153 | 154 | 155 | void CompositeWidget::OnKeyDown(char str) 156 | { 157 | if(focussedChild != 0) 158 | focussedChild->OnKeyDown(str); 159 | } 160 | 161 | void CompositeWidget::OnKeyUp(char str) 162 | { 163 | if(focussedChild != 0) 164 | focussedChild->OnKeyUp(str); 165 | } 166 | 167 | -------------------------------------------------------------------------------- /src/gui/window.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos::common; 5 | using namespace myos::gui; 6 | 7 | Window::Window(Widget* parent, 8 | common::int32_t x, common::int32_t y, common::int32_t w, common::int32_t h, 9 | common::uint8_t r, common::uint8_t g, common::uint8_t b) 10 | : CompositeWidget(parent, x,y,w,h, r,g,b) 11 | { 12 | Dragging = false; 13 | } 14 | 15 | Window::~Window() 16 | { 17 | } 18 | 19 | void Window::OnMouseDown(common::int32_t x, common::int32_t y, common::uint8_t button) 20 | { 21 | Dragging = button == 1; 22 | CompositeWidget::OnMouseDown(x,y,button); 23 | } 24 | 25 | void Window::OnMouseUp(common::int32_t x, common::int32_t y, common::uint8_t button) 26 | { 27 | Dragging = false; 28 | CompositeWidget::OnMouseUp(x,y,button); 29 | } 30 | 31 | void Window::OnMouseMove(common::int32_t oldx, common::int32_t oldy, common::int32_t newx, common::int32_t newy) 32 | { 33 | if(Dragging) 34 | { 35 | this->x += newx-oldx; 36 | this->y += newy-oldy; 37 | } 38 | CompositeWidget::OnMouseMove(oldx,oldy,newx, newy); 39 | 40 | } 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/hardwarecommunication/interrupts.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace myos; 4 | using namespace myos::common; 5 | using namespace myos::hardwarecommunication; 6 | 7 | 8 | void printf(char* str); 9 | void printfHex(uint8_t); 10 | 11 | 12 | 13 | 14 | 15 | InterruptHandler::InterruptHandler(InterruptManager* interruptManager, uint8_t InterruptNumber) 16 | { 17 | this->InterruptNumber = InterruptNumber; 18 | this->interruptManager = interruptManager; 19 | interruptManager->handlers[InterruptNumber] = this; 20 | } 21 | 22 | InterruptHandler::~InterruptHandler() 23 | { 24 | if(interruptManager->handlers[InterruptNumber] == this) 25 | interruptManager->handlers[InterruptNumber] = 0; 26 | } 27 | 28 | uint32_t InterruptHandler::HandleInterrupt(uint32_t esp) 29 | { 30 | return esp; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | InterruptManager::GateDescriptor InterruptManager::interruptDescriptorTable[256]; 43 | InterruptManager* InterruptManager::ActiveInterruptManager = 0; 44 | 45 | 46 | 47 | 48 | void InterruptManager::SetInterruptDescriptorTableEntry(uint8_t interrupt, 49 | uint16_t CodeSegment, void (*handler)(), uint8_t DescriptorPrivilegeLevel, uint8_t DescriptorType) 50 | { 51 | // address of pointer to code segment (relative to global descriptor table) 52 | // and address of the handler (relative to segment) 53 | interruptDescriptorTable[interrupt].handlerAddressLowBits = ((uint32_t) handler) & 0xFFFF; 54 | interruptDescriptorTable[interrupt].handlerAddressHighBits = (((uint32_t) handler) >> 16) & 0xFFFF; 55 | interruptDescriptorTable[interrupt].gdt_codeSegmentSelector = CodeSegment; 56 | 57 | const uint8_t IDT_DESC_PRESENT = 0x80; 58 | interruptDescriptorTable[interrupt].access = IDT_DESC_PRESENT | ((DescriptorPrivilegeLevel & 3) << 5) | DescriptorType; 59 | interruptDescriptorTable[interrupt].reserved = 0; 60 | } 61 | 62 | 63 | InterruptManager::InterruptManager(uint16_t hardwareInterruptOffset, GlobalDescriptorTable* globalDescriptorTable, TaskManager* taskManager) 64 | : programmableInterruptControllerMasterCommandPort(0x20), 65 | programmableInterruptControllerMasterDataPort(0x21), 66 | programmableInterruptControllerSlaveCommandPort(0xA0), 67 | programmableInterruptControllerSlaveDataPort(0xA1) 68 | { 69 | this->taskManager = taskManager; 70 | this->hardwareInterruptOffset = hardwareInterruptOffset; 71 | uint32_t CodeSegment = globalDescriptorTable->CodeSegmentSelector(); 72 | 73 | const uint8_t IDT_INTERRUPT_GATE = 0xE; 74 | for(uint8_t i = 255; i > 0; --i) 75 | { 76 | SetInterruptDescriptorTableEntry(i, CodeSegment, &InterruptIgnore, 0, IDT_INTERRUPT_GATE); 77 | handlers[i] = 0; 78 | } 79 | SetInterruptDescriptorTableEntry(0, CodeSegment, &InterruptIgnore, 0, IDT_INTERRUPT_GATE); 80 | handlers[0] = 0; 81 | 82 | SetInterruptDescriptorTableEntry(0x00, CodeSegment, &HandleException0x00, 0, IDT_INTERRUPT_GATE); 83 | SetInterruptDescriptorTableEntry(0x01, CodeSegment, &HandleException0x01, 0, IDT_INTERRUPT_GATE); 84 | SetInterruptDescriptorTableEntry(0x02, CodeSegment, &HandleException0x02, 0, IDT_INTERRUPT_GATE); 85 | SetInterruptDescriptorTableEntry(0x03, CodeSegment, &HandleException0x03, 0, IDT_INTERRUPT_GATE); 86 | SetInterruptDescriptorTableEntry(0x04, CodeSegment, &HandleException0x04, 0, IDT_INTERRUPT_GATE); 87 | SetInterruptDescriptorTableEntry(0x05, CodeSegment, &HandleException0x05, 0, IDT_INTERRUPT_GATE); 88 | SetInterruptDescriptorTableEntry(0x06, CodeSegment, &HandleException0x06, 0, IDT_INTERRUPT_GATE); 89 | SetInterruptDescriptorTableEntry(0x07, CodeSegment, &HandleException0x07, 0, IDT_INTERRUPT_GATE); 90 | SetInterruptDescriptorTableEntry(0x08, CodeSegment, &HandleException0x08, 0, IDT_INTERRUPT_GATE); 91 | SetInterruptDescriptorTableEntry(0x09, CodeSegment, &HandleException0x09, 0, IDT_INTERRUPT_GATE); 92 | SetInterruptDescriptorTableEntry(0x0A, CodeSegment, &HandleException0x0A, 0, IDT_INTERRUPT_GATE); 93 | SetInterruptDescriptorTableEntry(0x0B, CodeSegment, &HandleException0x0B, 0, IDT_INTERRUPT_GATE); 94 | SetInterruptDescriptorTableEntry(0x0C, CodeSegment, &HandleException0x0C, 0, IDT_INTERRUPT_GATE); 95 | SetInterruptDescriptorTableEntry(0x0D, CodeSegment, &HandleException0x0D, 0, IDT_INTERRUPT_GATE); 96 | SetInterruptDescriptorTableEntry(0x0E, CodeSegment, &HandleException0x0E, 0, IDT_INTERRUPT_GATE); 97 | SetInterruptDescriptorTableEntry(0x0F, CodeSegment, &HandleException0x0F, 0, IDT_INTERRUPT_GATE); 98 | SetInterruptDescriptorTableEntry(0x10, CodeSegment, &HandleException0x10, 0, IDT_INTERRUPT_GATE); 99 | SetInterruptDescriptorTableEntry(0x11, CodeSegment, &HandleException0x11, 0, IDT_INTERRUPT_GATE); 100 | SetInterruptDescriptorTableEntry(0x12, CodeSegment, &HandleException0x12, 0, IDT_INTERRUPT_GATE); 101 | SetInterruptDescriptorTableEntry(0x13, CodeSegment, &HandleException0x13, 0, IDT_INTERRUPT_GATE); 102 | 103 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x00, CodeSegment, &HandleInterruptRequest0x00, 0, IDT_INTERRUPT_GATE); 104 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x01, CodeSegment, &HandleInterruptRequest0x01, 0, IDT_INTERRUPT_GATE); 105 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x02, CodeSegment, &HandleInterruptRequest0x02, 0, IDT_INTERRUPT_GATE); 106 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x03, CodeSegment, &HandleInterruptRequest0x03, 0, IDT_INTERRUPT_GATE); 107 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x04, CodeSegment, &HandleInterruptRequest0x04, 0, IDT_INTERRUPT_GATE); 108 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x05, CodeSegment, &HandleInterruptRequest0x05, 0, IDT_INTERRUPT_GATE); 109 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x06, CodeSegment, &HandleInterruptRequest0x06, 0, IDT_INTERRUPT_GATE); 110 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x07, CodeSegment, &HandleInterruptRequest0x07, 0, IDT_INTERRUPT_GATE); 111 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x08, CodeSegment, &HandleInterruptRequest0x08, 0, IDT_INTERRUPT_GATE); 112 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x09, CodeSegment, &HandleInterruptRequest0x09, 0, IDT_INTERRUPT_GATE); 113 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0A, CodeSegment, &HandleInterruptRequest0x0A, 0, IDT_INTERRUPT_GATE); 114 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0B, CodeSegment, &HandleInterruptRequest0x0B, 0, IDT_INTERRUPT_GATE); 115 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0C, CodeSegment, &HandleInterruptRequest0x0C, 0, IDT_INTERRUPT_GATE); 116 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0D, CodeSegment, &HandleInterruptRequest0x0D, 0, IDT_INTERRUPT_GATE); 117 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0E, CodeSegment, &HandleInterruptRequest0x0E, 0, IDT_INTERRUPT_GATE); 118 | SetInterruptDescriptorTableEntry(hardwareInterruptOffset + 0x0F, CodeSegment, &HandleInterruptRequest0x0F, 0, IDT_INTERRUPT_GATE); 119 | 120 | SetInterruptDescriptorTableEntry( 0x80, CodeSegment, &HandleInterruptRequest0x80, 0, IDT_INTERRUPT_GATE); 121 | 122 | programmableInterruptControllerMasterCommandPort.Write(0x11); 123 | programmableInterruptControllerSlaveCommandPort.Write(0x11); 124 | 125 | // remap 126 | programmableInterruptControllerMasterDataPort.Write(hardwareInterruptOffset); 127 | programmableInterruptControllerSlaveDataPort.Write(hardwareInterruptOffset+8); 128 | 129 | programmableInterruptControllerMasterDataPort.Write(0x04); 130 | programmableInterruptControllerSlaveDataPort.Write(0x02); 131 | 132 | programmableInterruptControllerMasterDataPort.Write(0x01); 133 | programmableInterruptControllerSlaveDataPort.Write(0x01); 134 | 135 | programmableInterruptControllerMasterDataPort.Write(0x00); 136 | programmableInterruptControllerSlaveDataPort.Write(0x00); 137 | 138 | InterruptDescriptorTablePointer idt_pointer; 139 | idt_pointer.size = 256*sizeof(GateDescriptor) - 1; 140 | idt_pointer.base = (uint32_t)interruptDescriptorTable; 141 | asm volatile("lidt %0" : : "m" (idt_pointer)); 142 | } 143 | 144 | InterruptManager::~InterruptManager() 145 | { 146 | Deactivate(); 147 | } 148 | 149 | uint16_t InterruptManager::HardwareInterruptOffset() 150 | { 151 | return hardwareInterruptOffset; 152 | } 153 | 154 | void InterruptManager::Activate() 155 | { 156 | if(ActiveInterruptManager != 0) 157 | ActiveInterruptManager->Deactivate(); 158 | 159 | ActiveInterruptManager = this; 160 | asm("sti"); 161 | } 162 | 163 | void InterruptManager::Deactivate() 164 | { 165 | if(ActiveInterruptManager == this) 166 | { 167 | ActiveInterruptManager = 0; 168 | asm("cli"); 169 | } 170 | } 171 | 172 | uint32_t InterruptManager::HandleInterrupt(uint8_t interrupt, uint32_t esp) 173 | { 174 | if(ActiveInterruptManager != 0) 175 | return ActiveInterruptManager->DoHandleInterrupt(interrupt, esp); 176 | return esp; 177 | } 178 | 179 | 180 | uint32_t InterruptManager::DoHandleInterrupt(uint8_t interrupt, uint32_t esp) 181 | { 182 | if(handlers[interrupt] != 0) 183 | { 184 | esp = handlers[interrupt]->HandleInterrupt(esp); 185 | } 186 | else if(interrupt != hardwareInterruptOffset) 187 | { 188 | printf("UNHANDLED INTERRUPT 0x"); 189 | printfHex(interrupt); 190 | } 191 | 192 | if(interrupt == hardwareInterruptOffset) 193 | { 194 | esp = (uint32_t)taskManager->Schedule((CPUState*)esp); 195 | } 196 | 197 | // hardware interrupts must be acknowledged 198 | if(hardwareInterruptOffset <= interrupt && interrupt < hardwareInterruptOffset+16) 199 | { 200 | programmableInterruptControllerMasterCommandPort.Write(0x20); 201 | if(hardwareInterruptOffset + 8 <= interrupt) 202 | programmableInterruptControllerSlaveCommandPort.Write(0x20); 203 | } 204 | 205 | return esp; 206 | } 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | -------------------------------------------------------------------------------- /src/hardwarecommunication/interruptstubs.s: -------------------------------------------------------------------------------- 1 | 2 | 3 | .set IRQ_BASE, 0x20 4 | 5 | .section .text 6 | 7 | .extern _ZN4myos21hardwarecommunication16InterruptManager15HandleInterruptEhj 8 | 9 | 10 | .macro HandleException num 11 | .global _ZN4myos21hardwarecommunication16InterruptManager19HandleException\num\()Ev 12 | _ZN4myos21hardwarecommunication16InterruptManager19HandleException\num\()Ev: 13 | movb $\num, (interruptnumber) 14 | jmp int_bottom 15 | .endm 16 | 17 | 18 | .macro HandleInterruptRequest num 19 | .global _ZN4myos21hardwarecommunication16InterruptManager26HandleInterruptRequest\num\()Ev 20 | _ZN4myos21hardwarecommunication16InterruptManager26HandleInterruptRequest\num\()Ev: 21 | movb $\num + IRQ_BASE, (interruptnumber) 22 | pushl $0 23 | jmp int_bottom 24 | .endm 25 | 26 | 27 | HandleException 0x00 28 | HandleException 0x01 29 | HandleException 0x02 30 | HandleException 0x03 31 | HandleException 0x04 32 | HandleException 0x05 33 | HandleException 0x06 34 | HandleException 0x07 35 | HandleException 0x08 36 | HandleException 0x09 37 | HandleException 0x0A 38 | HandleException 0x0B 39 | HandleException 0x0C 40 | HandleException 0x0D 41 | HandleException 0x0E 42 | HandleException 0x0F 43 | HandleException 0x10 44 | HandleException 0x11 45 | HandleException 0x12 46 | HandleException 0x13 47 | 48 | HandleInterruptRequest 0x00 49 | HandleInterruptRequest 0x01 50 | HandleInterruptRequest 0x02 51 | HandleInterruptRequest 0x03 52 | HandleInterruptRequest 0x04 53 | HandleInterruptRequest 0x05 54 | HandleInterruptRequest 0x06 55 | HandleInterruptRequest 0x07 56 | HandleInterruptRequest 0x08 57 | HandleInterruptRequest 0x09 58 | HandleInterruptRequest 0x0A 59 | HandleInterruptRequest 0x0B 60 | HandleInterruptRequest 0x0C 61 | HandleInterruptRequest 0x0D 62 | HandleInterruptRequest 0x0E 63 | HandleInterruptRequest 0x0F 64 | HandleInterruptRequest 0x31 65 | 66 | HandleInterruptRequest 0x80 67 | 68 | 69 | int_bottom: 70 | 71 | # save registers 72 | #pusha 73 | #pushl %ds 74 | #pushl %es 75 | #pushl %fs 76 | #pushl %gs 77 | 78 | pushl %ebp 79 | pushl %edi 80 | pushl %esi 81 | 82 | pushl %edx 83 | pushl %ecx 84 | pushl %ebx 85 | pushl %eax 86 | 87 | # load ring 0 segment register 88 | #cld 89 | #mov $0x10, %eax 90 | #mov %eax, %eds 91 | #mov %eax, %ees 92 | 93 | # call C++ Handler 94 | pushl %esp 95 | push (interruptnumber) 96 | call _ZN4myos21hardwarecommunication16InterruptManager15HandleInterruptEhj 97 | #add %esp, 6 98 | mov %eax, %esp # switch the stack 99 | 100 | # restore registers 101 | popl %eax 102 | popl %ebx 103 | popl %ecx 104 | popl %edx 105 | 106 | popl %esi 107 | popl %edi 108 | popl %ebp 109 | #pop %gs 110 | #pop %fs 111 | #pop %es 112 | #pop %ds 113 | #popa 114 | 115 | add $4, %esp 116 | 117 | .global _ZN4myos21hardwarecommunication16InterruptManager15InterruptIgnoreEv 118 | _ZN4myos21hardwarecommunication16InterruptManager15InterruptIgnoreEv: 119 | 120 | iret 121 | 122 | 123 | .data 124 | interruptnumber: .byte 0 125 | -------------------------------------------------------------------------------- /src/hardwarecommunication/pci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace myos::common; 5 | using namespace myos::drivers; 6 | using namespace myos::hardwarecommunication; 7 | 8 | 9 | 10 | 11 | PeripheralComponentInterconnectDeviceDescriptor::PeripheralComponentInterconnectDeviceDescriptor() 12 | { 13 | } 14 | 15 | PeripheralComponentInterconnectDeviceDescriptor::~PeripheralComponentInterconnectDeviceDescriptor() 16 | { 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | PeripheralComponentInterconnectController::PeripheralComponentInterconnectController() 26 | : dataPort(0xCFC), 27 | commandPort(0xCF8) 28 | { 29 | } 30 | 31 | PeripheralComponentInterconnectController::~PeripheralComponentInterconnectController() 32 | { 33 | } 34 | 35 | uint32_t PeripheralComponentInterconnectController::Read(uint16_t bus, uint16_t device, uint16_t function, uint32_t registeroffset) 36 | { 37 | uint32_t id = 38 | 0x1 << 31 39 | | ((bus & 0xFF) << 16) 40 | | ((device & 0x1F) << 11) 41 | | ((function & 0x07) << 8) 42 | | (registeroffset & 0xFC); 43 | commandPort.Write(id); 44 | uint32_t result = dataPort.Read(); 45 | return result >> (8* (registeroffset % 4)); 46 | } 47 | 48 | void PeripheralComponentInterconnectController::Write(uint16_t bus, uint16_t device, uint16_t function, uint32_t registeroffset, uint32_t value) 49 | { 50 | uint32_t id = 51 | 0x1 << 31 52 | | ((bus & 0xFF) << 16) 53 | | ((device & 0x1F) << 11) 54 | | ((function & 0x07) << 8) 55 | | (registeroffset & 0xFC); 56 | commandPort.Write(id); 57 | dataPort.Write(value); 58 | } 59 | 60 | bool PeripheralComponentInterconnectController::DeviceHasFunctions(common::uint16_t bus, common::uint16_t device) 61 | { 62 | return Read(bus, device, 0, 0x0E) & (1<<7); 63 | } 64 | 65 | 66 | void printf(char* str); 67 | void printfHex(uint8_t); 68 | 69 | void PeripheralComponentInterconnectController::SelectDrivers(DriverManager* driverManager, myos::hardwarecommunication::InterruptManager* interrupts) 70 | { 71 | for(int bus = 0; bus < 8; bus++) 72 | { 73 | for(int device = 0; device < 32; device++) 74 | { 75 | int numFunctions = DeviceHasFunctions(bus, device) ? 8 : 1; 76 | for(int function = 0; function < numFunctions; function++) 77 | { 78 | PeripheralComponentInterconnectDeviceDescriptor dev = GetDeviceDescriptor(bus, device, function); 79 | 80 | if(dev.vendor_id == 0x0000 || dev.vendor_id == 0xFFFF) 81 | continue; 82 | 83 | 84 | for(int barNum = 0; barNum < 6; barNum++) 85 | { 86 | BaseAddressRegister bar = GetBaseAddressRegister(bus, device, function, barNum); 87 | if(bar.address && (bar.type == InputOutput)) 88 | dev.portBase = (uint32_t)bar.address; 89 | } 90 | 91 | Driver* driver = GetDriver(dev, interrupts); 92 | if(driver != 0) 93 | driverManager->AddDriver(driver); 94 | 95 | 96 | printf("PCI BUS "); 97 | printfHex(bus & 0xFF); 98 | 99 | printf(", DEVICE "); 100 | printfHex(device & 0xFF); 101 | 102 | printf(", FUNCTION "); 103 | printfHex(function & 0xFF); 104 | 105 | printf(" = VENDOR "); 106 | printfHex((dev.vendor_id & 0xFF00) >> 8); 107 | printfHex(dev.vendor_id & 0xFF); 108 | printf(", DEVICE "); 109 | printfHex((dev.device_id & 0xFF00) >> 8); 110 | printfHex(dev.device_id & 0xFF); 111 | printf("\n"); 112 | } 113 | } 114 | } 115 | } 116 | 117 | 118 | BaseAddressRegister PeripheralComponentInterconnectController::GetBaseAddressRegister(uint16_t bus, uint16_t device, uint16_t function, uint16_t bar) 119 | { 120 | BaseAddressRegister result; 121 | 122 | 123 | uint32_t headertype = Read(bus, device, function, 0x0E) & 0x7F; 124 | int maxBARs = 6 - (4*headertype); 125 | if(bar >= maxBARs) 126 | return result; 127 | 128 | 129 | uint32_t bar_value = Read(bus, device, function, 0x10 + 4*bar); 130 | result.type = (bar_value & 0x1) ? InputOutput : MemoryMapping; 131 | uint32_t temp; 132 | 133 | 134 | 135 | if(result.type == MemoryMapping) 136 | { 137 | 138 | switch((bar_value >> 1) & 0x3) 139 | { 140 | 141 | case 0: // 32 Bit Mode 142 | case 1: // 20 Bit Mode 143 | case 2: // 64 Bit Mode 144 | break; 145 | } 146 | 147 | } 148 | else // InputOutput 149 | { 150 | result.address = (uint8_t*)(bar_value & ~0x3); 151 | result.prefetchable = false; 152 | } 153 | 154 | 155 | return result; 156 | } 157 | 158 | 159 | 160 | Driver* PeripheralComponentInterconnectController::GetDriver(PeripheralComponentInterconnectDeviceDescriptor dev, InterruptManager* interrupts) 161 | { 162 | Driver* driver = 0; 163 | switch(dev.vendor_id) 164 | { 165 | case 0x1022: // AMD 166 | switch(dev.device_id) 167 | { 168 | case 0x2000: // am79c973 169 | printf("AMD am79c973 "); 170 | driver = (amd_am79c973*)MemoryManager::activeMemoryManager->malloc(sizeof(amd_am79c973)); 171 | if(driver != 0) 172 | new (driver) amd_am79c973(&dev, interrupts); 173 | else 174 | printf("instantiation failed"); 175 | return driver; 176 | break; 177 | } 178 | break; 179 | 180 | case 0x8086: // Intel 181 | break; 182 | } 183 | 184 | 185 | switch(dev.class_id) 186 | { 187 | case 0x03: // graphics 188 | switch(dev.subclass_id) 189 | { 190 | case 0x00: // VGA 191 | printf("VGA "); 192 | break; 193 | } 194 | break; 195 | } 196 | 197 | 198 | return driver; 199 | } 200 | 201 | 202 | 203 | PeripheralComponentInterconnectDeviceDescriptor PeripheralComponentInterconnectController::GetDeviceDescriptor(uint16_t bus, uint16_t device, uint16_t function) 204 | { 205 | PeripheralComponentInterconnectDeviceDescriptor result; 206 | 207 | result.bus = bus; 208 | result.device = device; 209 | result.function = function; 210 | 211 | result.vendor_id = Read(bus, device, function, 0x00); 212 | result.device_id = Read(bus, device, function, 0x02); 213 | 214 | result.class_id = Read(bus, device, function, 0x0b); 215 | result.subclass_id = Read(bus, device, function, 0x0a); 216 | result.interface_id = Read(bus, device, function, 0x09); 217 | 218 | result.revision = Read(bus, device, function, 0x08); 219 | result.interrupt = Read(bus, device, function, 0x3c); 220 | 221 | return result; 222 | } 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | -------------------------------------------------------------------------------- /src/hardwarecommunication/port.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace myos::common; 4 | using namespace myos::hardwarecommunication; 5 | 6 | 7 | Port::Port(uint16_t portnumber) 8 | { 9 | this->portnumber = portnumber; 10 | } 11 | 12 | Port::~Port() 13 | { 14 | } 15 | 16 | 17 | 18 | 19 | 20 | Port8Bit::Port8Bit(uint16_t portnumber) 21 | : Port(portnumber) 22 | { 23 | } 24 | 25 | Port8Bit::~Port8Bit() 26 | { 27 | } 28 | 29 | void Port8Bit::Write(uint8_t data) 30 | { 31 | Write8(portnumber, data); 32 | } 33 | 34 | uint8_t Port8Bit::Read() 35 | { 36 | return Read8(portnumber); 37 | } 38 | 39 | 40 | 41 | 42 | 43 | Port8BitSlow::Port8BitSlow(uint16_t portnumber) 44 | : Port8Bit(portnumber) 45 | { 46 | } 47 | 48 | Port8BitSlow::~Port8BitSlow() 49 | { 50 | } 51 | 52 | void Port8BitSlow::Write(uint8_t data) 53 | { 54 | Write8Slow(portnumber, data); 55 | } 56 | 57 | 58 | 59 | 60 | 61 | Port16Bit::Port16Bit(uint16_t portnumber) 62 | : Port(portnumber) 63 | { 64 | } 65 | 66 | Port16Bit::~Port16Bit() 67 | { 68 | } 69 | 70 | void Port16Bit::Write(uint16_t data) 71 | { 72 | Write16(portnumber, data); 73 | } 74 | 75 | uint16_t Port16Bit::Read() 76 | { 77 | return Read16(portnumber); 78 | } 79 | 80 | 81 | 82 | 83 | 84 | Port32Bit::Port32Bit(uint16_t portnumber) 85 | : Port(portnumber) 86 | { 87 | } 88 | 89 | Port32Bit::~Port32Bit() 90 | { 91 | } 92 | 93 | void Port32Bit::Write(uint32_t data) 94 | { 95 | Write32(portnumber, data); 96 | } 97 | 98 | uint32_t Port32Bit::Read() 99 | { 100 | return Read32(portnumber); 101 | } 102 | 103 | -------------------------------------------------------------------------------- /src/kernel.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | // #define GRAPHICSMODE 27 | 28 | 29 | using namespace myos; 30 | using namespace myos::common; 31 | using namespace myos::drivers; 32 | using namespace myos::hardwarecommunication; 33 | using namespace myos::gui; 34 | using namespace myos::net; 35 | 36 | 37 | 38 | void printf(char* str) 39 | { 40 | static uint16_t* VideoMemory = (uint16_t*)0xb8000; 41 | 42 | static uint8_t x=0,y=0; 43 | 44 | for(int i = 0; str[i] != '\0'; ++i) 45 | { 46 | switch(str[i]) 47 | { 48 | case '\n': 49 | x = 0; 50 | y++; 51 | break; 52 | default: 53 | VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | str[i]; 54 | x++; 55 | break; 56 | } 57 | 58 | if(x >= 80) 59 | { 60 | x = 0; 61 | y++; 62 | } 63 | 64 | if(y >= 25) 65 | { 66 | for(y = 0; y < 25; y++) 67 | for(x = 0; x < 80; x++) 68 | VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | ' '; 69 | x = 0; 70 | y = 0; 71 | } 72 | } 73 | } 74 | 75 | void printfHex(uint8_t key) 76 | { 77 | char* foo = "00"; 78 | char* hex = "0123456789ABCDEF"; 79 | foo[0] = hex[(key >> 4) & 0xF]; 80 | foo[1] = hex[key & 0xF]; 81 | printf(foo); 82 | } 83 | void printfHex16(uint16_t key) 84 | { 85 | printfHex((key >> 8) & 0xFF); 86 | printfHex( key & 0xFF); 87 | } 88 | void printfHex32(uint32_t key) 89 | { 90 | printfHex((key >> 24) & 0xFF); 91 | printfHex((key >> 16) & 0xFF); 92 | printfHex((key >> 8) & 0xFF); 93 | printfHex( key & 0xFF); 94 | } 95 | 96 | 97 | 98 | 99 | 100 | class PrintfKeyboardEventHandler : public KeyboardEventHandler 101 | { 102 | public: 103 | void OnKeyDown(char c) 104 | { 105 | char* foo = " "; 106 | foo[0] = c; 107 | printf(foo); 108 | } 109 | }; 110 | 111 | class MouseToConsole : public MouseEventHandler 112 | { 113 | int8_t x, y; 114 | public: 115 | 116 | MouseToConsole() 117 | { 118 | uint16_t* VideoMemory = (uint16_t*)0xb8000; 119 | x = 40; 120 | y = 12; 121 | VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0x0F00) << 4 122 | | (VideoMemory[80*y+x] & 0xF000) >> 4 123 | | (VideoMemory[80*y+x] & 0x00FF); 124 | } 125 | 126 | virtual void OnMouseMove(int xoffset, int yoffset) 127 | { 128 | static uint16_t* VideoMemory = (uint16_t*)0xb8000; 129 | VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0x0F00) << 4 130 | | (VideoMemory[80*y+x] & 0xF000) >> 4 131 | | (VideoMemory[80*y+x] & 0x00FF); 132 | 133 | x += xoffset; 134 | if(x >= 80) x = 79; 135 | if(x < 0) x = 0; 136 | y += yoffset; 137 | if(y >= 25) y = 24; 138 | if(y < 0) y = 0; 139 | 140 | VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0x0F00) << 4 141 | | (VideoMemory[80*y+x] & 0xF000) >> 4 142 | | (VideoMemory[80*y+x] & 0x00FF); 143 | } 144 | 145 | }; 146 | 147 | class PrintfUDPHandler : public UserDatagramProtocolHandler 148 | { 149 | public: 150 | void HandleUserDatagramProtocolMessage(UserDatagramProtocolSocket* socket, common::uint8_t* data, common::uint16_t size) 151 | { 152 | char* foo = " "; 153 | for(int i = 0; i < size; i++) 154 | { 155 | foo[0] = data[i]; 156 | printf(foo); 157 | } 158 | } 159 | }; 160 | 161 | 162 | class PrintfTCPHandler : public TransmissionControlProtocolHandler 163 | { 164 | public: 165 | bool HandleTransmissionControlProtocolMessage(TransmissionControlProtocolSocket* socket, common::uint8_t* data, common::uint16_t size) 166 | { 167 | char* foo = " "; 168 | for(int i = 0; i < size; i++) 169 | { 170 | foo[0] = data[i]; 171 | printf(foo); 172 | } 173 | 174 | 175 | 176 | if(size > 9 177 | && data[0] == 'G' 178 | && data[1] == 'E' 179 | && data[2] == 'T' 180 | && data[3] == ' ' 181 | && data[4] == '/' 182 | && data[5] == ' ' 183 | && data[6] == 'H' 184 | && data[7] == 'T' 185 | && data[8] == 'T' 186 | && data[9] == 'P' 187 | ) 188 | { 189 | socket->Send((uint8_t*)"HTTP/1.1 200 OK\r\nServer: MyOS\r\nContent-Type: text/html\r\n\r\nMy Operating SystemMy Operating System http://www.AlgorithMan.de\r\n",184); 190 | socket->Disconnect(); 191 | } 192 | 193 | 194 | return true; 195 | } 196 | }; 197 | 198 | 199 | void sysprintf(char* str) 200 | { 201 | asm("int $0x80" : : "a" (4), "b" (str)); 202 | } 203 | 204 | void taskA() 205 | { 206 | while(true) 207 | sysprintf("A"); 208 | } 209 | 210 | void taskB() 211 | { 212 | while(true) 213 | sysprintf("B"); 214 | } 215 | 216 | 217 | 218 | 219 | 220 | 221 | typedef void (*constructor)(); 222 | extern "C" constructor start_ctors; 223 | extern "C" constructor end_ctors; 224 | extern "C" void callConstructors() 225 | { 226 | for(constructor* i = &start_ctors; i != &end_ctors; i++) 227 | (*i)(); 228 | } 229 | 230 | 231 | 232 | extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot_magic*/) 233 | { 234 | printf("Hello World! --- http://www.AlgorithMan.de\n"); 235 | 236 | GlobalDescriptorTable gdt; 237 | 238 | 239 | uint32_t* memupper = (uint32_t*)(((size_t)multiboot_structure) + 8); 240 | size_t heap = 10*1024*1024; 241 | MemoryManager memoryManager(heap, (*memupper)*1024 - heap - 10*1024); 242 | 243 | printf("heap: 0x"); 244 | printfHex((heap >> 24) & 0xFF); 245 | printfHex((heap >> 16) & 0xFF); 246 | printfHex((heap >> 8 ) & 0xFF); 247 | printfHex((heap ) & 0xFF); 248 | 249 | void* allocated = memoryManager.malloc(1024); 250 | printf("\nallocated: 0x"); 251 | printfHex(((size_t)allocated >> 24) & 0xFF); 252 | printfHex(((size_t)allocated >> 16) & 0xFF); 253 | printfHex(((size_t)allocated >> 8 ) & 0xFF); 254 | printfHex(((size_t)allocated ) & 0xFF); 255 | printf("\n"); 256 | 257 | TaskManager taskManager; 258 | /* 259 | Task task1(&gdt, taskA); 260 | Task task2(&gdt, taskB); 261 | taskManager.AddTask(&task1); 262 | taskManager.AddTask(&task2); 263 | */ 264 | 265 | InterruptManager interrupts(0x20, &gdt, &taskManager); 266 | SyscallHandler syscalls(&interrupts, 0x80); 267 | 268 | printf("Initializing Hardware, Stage 1\n"); 269 | 270 | #ifdef GRAPHICSMODE 271 | Desktop desktop(320,200, 0x00,0x00,0xA8); 272 | #endif 273 | 274 | DriverManager drvManager; 275 | 276 | #ifdef GRAPHICSMODE 277 | KeyboardDriver keyboard(&interrupts, &desktop); 278 | #else 279 | PrintfKeyboardEventHandler kbhandler; 280 | KeyboardDriver keyboard(&interrupts, &kbhandler); 281 | #endif 282 | drvManager.AddDriver(&keyboard); 283 | 284 | 285 | #ifdef GRAPHICSMODE 286 | MouseDriver mouse(&interrupts, &desktop); 287 | #else 288 | MouseToConsole mousehandler; 289 | MouseDriver mouse(&interrupts, &mousehandler); 290 | #endif 291 | drvManager.AddDriver(&mouse); 292 | 293 | PeripheralComponentInterconnectController PCIController; 294 | PCIController.SelectDrivers(&drvManager, &interrupts); 295 | 296 | #ifdef GRAPHICSMODE 297 | VideoGraphicsArray vga; 298 | #endif 299 | 300 | printf("Initializing Hardware, Stage 2\n"); 301 | drvManager.ActivateAll(); 302 | 303 | printf("Initializing Hardware, Stage 3\n"); 304 | 305 | #ifdef GRAPHICSMODE 306 | vga.SetMode(320,200,8); 307 | Window win1(&desktop, 10,10,20,20, 0xA8,0x00,0x00); 308 | desktop.AddChild(&win1); 309 | Window win2(&desktop, 40,15,30,30, 0x00,0xA8,0x00); 310 | desktop.AddChild(&win2); 311 | #endif 312 | 313 | 314 | /* 315 | printf("\nS-ATA primary master: "); 316 | AdvancedTechnologyAttachment ata0m(true, 0x1F0); 317 | ata0m.Identify(); 318 | 319 | printf("\nS-ATA primary slave: "); 320 | AdvancedTechnologyAttachment ata0s(false, 0x1F0); 321 | ata0s.Identify(); 322 | ata0s.Write28(0, (uint8_t*)"http://www.AlgorithMan.de", 25); 323 | ata0s.Flush(); 324 | ata0s.Read28(0, 25); 325 | 326 | printf("\nS-ATA secondary master: "); 327 | AdvancedTechnologyAttachment ata1m(true, 0x170); 328 | ata1m.Identify(); 329 | 330 | printf("\nS-ATA secondary slave: "); 331 | AdvancedTechnologyAttachment ata1s(false, 0x170); 332 | ata1s.Identify(); 333 | // third: 0x1E8 334 | // fourth: 0x168 335 | */ 336 | 337 | 338 | 339 | 340 | 341 | amd_am79c973* eth0 = (amd_am79c973*)(drvManager.drivers[2]); 342 | 343 | 344 | // IP Address 345 | uint8_t ip1 = 10, ip2 = 0, ip3 = 2, ip4 = 15; 346 | uint32_t ip_be = ((uint32_t)ip4 << 24) 347 | | ((uint32_t)ip3 << 16) 348 | | ((uint32_t)ip2 << 8) 349 | | (uint32_t)ip1; 350 | eth0->SetIPAddress(ip_be); 351 | EtherFrameProvider etherframe(eth0); 352 | AddressResolutionProtocol arp(ðerframe); 353 | 354 | 355 | // IP Address of the default gateway 356 | uint8_t gip1 = 10, gip2 = 0, gip3 = 2, gip4 = 2; 357 | uint32_t gip_be = ((uint32_t)gip4 << 24) 358 | | ((uint32_t)gip3 << 16) 359 | | ((uint32_t)gip2 << 8) 360 | | (uint32_t)gip1; 361 | 362 | uint8_t subnet1 = 255, subnet2 = 255, subnet3 = 255, subnet4 = 0; 363 | uint32_t subnet_be = ((uint32_t)subnet4 << 24) 364 | | ((uint32_t)subnet3 << 16) 365 | | ((uint32_t)subnet2 << 8) 366 | | (uint32_t)subnet1; 367 | 368 | InternetProtocolProvider ipv4(ðerframe, &arp, gip_be, subnet_be); 369 | InternetControlMessageProtocol icmp(&ipv4); 370 | UserDatagramProtocolProvider udp(&ipv4); 371 | TransmissionControlProtocolProvider tcp(&ipv4); 372 | 373 | 374 | interrupts.Activate(); 375 | 376 | printf("\n\n\n\n"); 377 | 378 | arp.BroadcastMACAddress(gip_be); 379 | 380 | 381 | PrintfTCPHandler tcphandler; 382 | TransmissionControlProtocolSocket* tcpsocket = tcp.Listen(1234); 383 | tcp.Bind(tcpsocket, &tcphandler); 384 | //tcpsocket->Send((uint8_t*)"Hello TCP!", 10); 385 | 386 | 387 | //icmp.RequestEchoReply(gip_be); 388 | 389 | //PrintfUDPHandler udphandler; 390 | //UserDatagramProtocolSocket* udpsocket = udp.Connect(gip_be, 1234); 391 | //udp.Bind(udpsocket, &udphandler); 392 | //udpsocket->Send((uint8_t*)"Hello UDP!", 10); 393 | 394 | //UserDatagramProtocolSocket* udpsocket = udp.Listen(1234); 395 | //udp.Bind(udpsocket, &udphandler); 396 | 397 | 398 | while(1) 399 | { 400 | #ifdef GRAPHICSMODE 401 | desktop.Draw(&vga); 402 | #endif 403 | } 404 | } 405 | -------------------------------------------------------------------------------- /src/loader.s: -------------------------------------------------------------------------------- 1 | .set MAGIC, 0x1badb002 2 | .set FLAGS, (1<<0 | 1<<1) 3 | .set CHECKSUM, -(MAGIC + FLAGS) 4 | 5 | .section .multiboot 6 | .long MAGIC 7 | .long FLAGS 8 | .long CHECKSUM 9 | 10 | 11 | .section .text 12 | .extern kernelMain 13 | .extern callConstructors 14 | .global loader 15 | 16 | 17 | loader: 18 | mov $kernel_stack, %esp 19 | call callConstructors 20 | push %eax 21 | push %ebx 22 | call kernelMain 23 | 24 | 25 | _stop: 26 | cli 27 | hlt 28 | jmp _stop 29 | 30 | 31 | .section .bss 32 | .space 2*1024*1024; # 2 MiB 33 | kernel_stack: 34 | 35 | -------------------------------------------------------------------------------- /src/memorymanagement.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos; 5 | using namespace myos::common; 6 | 7 | 8 | MemoryManager* MemoryManager::activeMemoryManager = 0; 9 | 10 | MemoryManager::MemoryManager(size_t start, size_t size) 11 | { 12 | activeMemoryManager = this; 13 | 14 | if(size < sizeof(MemoryChunk)) 15 | { 16 | first = 0; 17 | } 18 | else 19 | { 20 | first = (MemoryChunk*)start; 21 | 22 | first -> allocated = false; 23 | first -> prev = 0; 24 | first -> next = 0; 25 | first -> size = size - sizeof(MemoryChunk); 26 | } 27 | } 28 | 29 | MemoryManager::~MemoryManager() 30 | { 31 | if(activeMemoryManager == this) 32 | activeMemoryManager = 0; 33 | } 34 | 35 | void* MemoryManager::malloc(size_t size) 36 | { 37 | MemoryChunk *result = 0; 38 | 39 | for(MemoryChunk* chunk = first; chunk != 0 && result == 0; chunk = chunk->next) 40 | if(chunk->size > size && !chunk->allocated) 41 | result = chunk; 42 | 43 | if(result == 0) 44 | return 0; 45 | 46 | if(result->size >= size + sizeof(MemoryChunk) + 1) 47 | { 48 | MemoryChunk* temp = (MemoryChunk*)((size_t)result + sizeof(MemoryChunk) + size); 49 | 50 | temp->allocated = false; 51 | temp->size = result->size - size - sizeof(MemoryChunk); 52 | temp->prev = result; 53 | temp->next = result->next; 54 | if(temp->next != 0) 55 | temp->next->prev = temp; 56 | 57 | result->size = size; 58 | result->next = temp; 59 | } 60 | 61 | result->allocated = true; 62 | return (void*)(((size_t)result) + sizeof(MemoryChunk)); 63 | } 64 | 65 | void MemoryManager::free(void* ptr) 66 | { 67 | MemoryChunk* chunk = (MemoryChunk*)((size_t)ptr - sizeof(MemoryChunk)); 68 | 69 | chunk -> allocated = false; 70 | 71 | if(chunk->prev != 0 && !chunk->prev->allocated) 72 | { 73 | chunk->prev->next = chunk->next; 74 | chunk->prev->size += chunk->size + sizeof(MemoryChunk); 75 | if(chunk->next != 0) 76 | chunk->next->prev = chunk->prev; 77 | 78 | chunk = chunk->prev; 79 | } 80 | 81 | if(chunk->next != 0 && !chunk->next->allocated) 82 | { 83 | chunk->size += chunk->next->size + sizeof(MemoryChunk); 84 | chunk->next = chunk->next->next; 85 | if(chunk->next != 0) 86 | chunk->next->prev = chunk; 87 | } 88 | 89 | } 90 | 91 | 92 | 93 | 94 | void* operator new(unsigned size) 95 | { 96 | if(myos::MemoryManager::activeMemoryManager == 0) 97 | return 0; 98 | return myos::MemoryManager::activeMemoryManager->malloc(size); 99 | } 100 | 101 | void* operator new[](unsigned size) 102 | { 103 | if(myos::MemoryManager::activeMemoryManager == 0) 104 | return 0; 105 | return myos::MemoryManager::activeMemoryManager->malloc(size); 106 | } 107 | 108 | void* operator new(unsigned size, void* ptr) 109 | { 110 | return ptr; 111 | } 112 | 113 | void* operator new[](unsigned size, void* ptr) 114 | { 115 | return ptr; 116 | } 117 | 118 | void operator delete(void* ptr) 119 | { 120 | if(myos::MemoryManager::activeMemoryManager != 0) 121 | myos::MemoryManager::activeMemoryManager->free(ptr); 122 | } 123 | 124 | void operator delete[](void* ptr) 125 | { 126 | if(myos::MemoryManager::activeMemoryManager != 0) 127 | myos::MemoryManager::activeMemoryManager->free(ptr); 128 | } -------------------------------------------------------------------------------- /src/multitasking.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos; 5 | using namespace myos::common; 6 | 7 | 8 | Task::Task(GlobalDescriptorTable *gdt, void entrypoint()) 9 | { 10 | cpustate = (CPUState*)(stack + 4096 - sizeof(CPUState)); 11 | 12 | cpustate -> eax = 0; 13 | cpustate -> ebx = 0; 14 | cpustate -> ecx = 0; 15 | cpustate -> edx = 0; 16 | 17 | cpustate -> esi = 0; 18 | cpustate -> edi = 0; 19 | cpustate -> ebp = 0; 20 | 21 | /* 22 | cpustate -> gs = 0; 23 | cpustate -> fs = 0; 24 | cpustate -> es = 0; 25 | cpustate -> ds = 0; 26 | */ 27 | 28 | // cpustate -> error = 0; 29 | 30 | // cpustate -> esp = ; 31 | cpustate -> eip = (uint32_t)entrypoint; 32 | cpustate -> cs = gdt->CodeSegmentSelector(); 33 | // cpustate -> ss = ; 34 | cpustate -> eflags = 0x202; 35 | 36 | } 37 | 38 | Task::~Task() 39 | { 40 | } 41 | 42 | 43 | TaskManager::TaskManager() 44 | { 45 | numTasks = 0; 46 | currentTask = -1; 47 | } 48 | 49 | TaskManager::~TaskManager() 50 | { 51 | } 52 | 53 | bool TaskManager::AddTask(Task* task) 54 | { 55 | if(numTasks >= 256) 56 | return false; 57 | tasks[numTasks++] = task; 58 | return true; 59 | } 60 | 61 | CPUState* TaskManager::Schedule(CPUState* cpustate) 62 | { 63 | if(numTasks <= 0) 64 | return cpustate; 65 | 66 | if(currentTask >= 0) 67 | tasks[currentTask]->cpustate = cpustate; 68 | 69 | if(++currentTask >= numTasks) 70 | currentTask %= numTasks; 71 | return tasks[currentTask]->cpustate; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /src/net/arp.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace myos; 4 | using namespace myos::common; 5 | using namespace myos::net; 6 | using namespace myos::drivers; 7 | 8 | 9 | AddressResolutionProtocol::AddressResolutionProtocol(EtherFrameProvider* backend) 10 | : EtherFrameHandler(backend, 0x806) 11 | { 12 | numCacheEntries = 0; 13 | } 14 | 15 | AddressResolutionProtocol::~AddressResolutionProtocol() 16 | { 17 | } 18 | 19 | 20 | bool AddressResolutionProtocol::OnEtherFrameReceived(uint8_t* etherframePayload, uint32_t size) 21 | { 22 | if(size < sizeof(AddressResolutionProtocolMessage)) 23 | return false; 24 | 25 | AddressResolutionProtocolMessage* arp = (AddressResolutionProtocolMessage*)etherframePayload; 26 | if(arp->hardwareType == 0x0100) 27 | { 28 | 29 | if(arp->protocol == 0x0008 30 | && arp->hardwareAddressSize == 6 31 | && arp->protocolAddressSize == 4 32 | && arp->dstIP == backend->GetIPAddress()) 33 | { 34 | 35 | switch(arp->command) 36 | { 37 | 38 | case 0x0100: // request 39 | arp->command = 0x0200; 40 | arp->dstIP = arp->srcIP; 41 | arp->dstMAC = arp->srcMAC; 42 | arp->srcIP = backend->GetIPAddress(); 43 | arp->srcMAC = backend->GetMACAddress(); 44 | return true; 45 | break; 46 | 47 | case 0x0200: // response 48 | if(numCacheEntries < 128) 49 | { 50 | IPcache[numCacheEntries] = arp->srcIP; 51 | MACcache[numCacheEntries] = arp->srcMAC; 52 | numCacheEntries++; 53 | } 54 | break; 55 | } 56 | } 57 | 58 | } 59 | 60 | return false; 61 | } 62 | 63 | 64 | void AddressResolutionProtocol::BroadcastMACAddress(uint32_t IP_BE) 65 | { 66 | AddressResolutionProtocolMessage arp; 67 | arp.hardwareType = 0x0100; // ethernet 68 | arp.protocol = 0x0008; // ipv4 69 | arp.hardwareAddressSize = 6; // mac 70 | arp.protocolAddressSize = 4; // ipv4 71 | arp.command = 0x0200; // "response" 72 | 73 | arp.srcMAC = backend->GetMACAddress(); 74 | arp.srcIP = backend->GetIPAddress(); 75 | arp.dstMAC = Resolve(IP_BE); 76 | arp.dstIP = IP_BE; 77 | 78 | this->Send(arp.dstMAC, (uint8_t*)&arp, sizeof(AddressResolutionProtocolMessage)); 79 | 80 | } 81 | 82 | 83 | void AddressResolutionProtocol::RequestMACAddress(uint32_t IP_BE) 84 | { 85 | 86 | AddressResolutionProtocolMessage arp; 87 | arp.hardwareType = 0x0100; // ethernet 88 | arp.protocol = 0x0008; // ipv4 89 | arp.hardwareAddressSize = 6; // mac 90 | arp.protocolAddressSize = 4; // ipv4 91 | arp.command = 0x0100; // request 92 | 93 | arp.srcMAC = backend->GetMACAddress(); 94 | arp.srcIP = backend->GetIPAddress(); 95 | arp.dstMAC = 0xFFFFFFFFFFFF; // broadcast 96 | arp.dstIP = IP_BE; 97 | 98 | this->Send(arp.dstMAC, (uint8_t*)&arp, sizeof(AddressResolutionProtocolMessage)); 99 | 100 | } 101 | 102 | uint64_t AddressResolutionProtocol::GetMACFromCache(uint32_t IP_BE) 103 | { 104 | for(int i = 0; i < numCacheEntries; i++) 105 | if(IPcache[i] == IP_BE) 106 | return MACcache[i]; 107 | return 0xFFFFFFFFFFFF; // broadcast address 108 | } 109 | 110 | uint64_t AddressResolutionProtocol::Resolve(uint32_t IP_BE) 111 | { 112 | uint64_t result = GetMACFromCache(IP_BE); 113 | if(result == 0xFFFFFFFFFFFF) 114 | RequestMACAddress(IP_BE); 115 | 116 | while(result == 0xFFFFFFFFFFFF) // possible infinite loop 117 | result = GetMACFromCache(IP_BE); 118 | 119 | return result; 120 | } 121 | -------------------------------------------------------------------------------- /src/net/etherframe.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace myos; 4 | using namespace myos::common; 5 | using namespace myos::net; 6 | using namespace myos::drivers; 7 | 8 | 9 | 10 | EtherFrameHandler::EtherFrameHandler(EtherFrameProvider* backend, uint16_t etherType) 11 | { 12 | this->etherType_BE = ((etherType & 0x00FF) << 8) 13 | | ((etherType & 0xFF00) >> 8); 14 | this->backend = backend; 15 | backend->handlers[etherType_BE] = this; 16 | } 17 | 18 | EtherFrameHandler::~EtherFrameHandler() 19 | { 20 | if(backend->handlers[etherType_BE] == this) 21 | backend->handlers[etherType_BE] = 0; 22 | } 23 | 24 | bool EtherFrameHandler::OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size) 25 | { 26 | return false; 27 | } 28 | 29 | void EtherFrameHandler::Send(common::uint64_t dstMAC_BE, common::uint8_t* data, common::uint32_t size) 30 | { 31 | backend->Send(dstMAC_BE, etherType_BE, data, size); 32 | } 33 | 34 | uint32_t EtherFrameHandler::GetIPAddress() 35 | { 36 | return backend->GetIPAddress(); 37 | } 38 | 39 | 40 | 41 | 42 | EtherFrameProvider::EtherFrameProvider(amd_am79c973* backend) 43 | : RawDataHandler(backend) 44 | { 45 | for(uint32_t i = 0; i < 65535; i++) 46 | handlers[i] = 0; 47 | } 48 | 49 | EtherFrameProvider::~EtherFrameProvider() 50 | { 51 | } 52 | 53 | bool EtherFrameProvider::OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size) 54 | { 55 | if(size < sizeof(EtherFrameHeader)) 56 | return false; 57 | 58 | EtherFrameHeader* frame = (EtherFrameHeader*)buffer; 59 | bool sendBack = false; 60 | 61 | if(frame->dstMAC_BE == 0xFFFFFFFFFFFF 62 | || frame->dstMAC_BE == backend->GetMACAddress()) 63 | { 64 | if(handlers[frame->etherType_BE] != 0) 65 | sendBack = handlers[frame->etherType_BE]->OnEtherFrameReceived( 66 | buffer + sizeof(EtherFrameHeader), size - sizeof(EtherFrameHeader)); 67 | } 68 | 69 | if(sendBack) 70 | { 71 | frame->dstMAC_BE = frame->srcMAC_BE; 72 | frame->srcMAC_BE = backend->GetMACAddress(); 73 | } 74 | 75 | return sendBack; 76 | } 77 | 78 | void EtherFrameProvider::Send(common::uint64_t dstMAC_BE, common::uint16_t etherType_BE, common::uint8_t* buffer, common::uint32_t size) 79 | { 80 | uint8_t* buffer2 = (uint8_t*)MemoryManager::activeMemoryManager->malloc(sizeof(EtherFrameHeader) + size); 81 | EtherFrameHeader* frame = (EtherFrameHeader*)buffer2; 82 | 83 | frame->dstMAC_BE = dstMAC_BE; 84 | frame->srcMAC_BE = backend->GetMACAddress(); 85 | frame->etherType_BE = etherType_BE; 86 | 87 | uint8_t* src = buffer; 88 | uint8_t* dst = buffer2 + sizeof(EtherFrameHeader); 89 | for(uint32_t i = 0; i < size; i++) 90 | dst[i] = src[i]; 91 | 92 | backend->Send(buffer2, size + sizeof(EtherFrameHeader)); 93 | 94 | MemoryManager::activeMemoryManager->free(buffer2); 95 | } 96 | 97 | uint32_t EtherFrameProvider::GetIPAddress() 98 | { 99 | return backend->GetIPAddress(); 100 | } 101 | 102 | uint64_t EtherFrameProvider::GetMACAddress() 103 | { 104 | return backend->GetMACAddress(); 105 | } 106 | -------------------------------------------------------------------------------- /src/net/icmp.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos; 5 | using namespace myos::common; 6 | using namespace myos::net; 7 | 8 | 9 | InternetControlMessageProtocol::InternetControlMessageProtocol(InternetProtocolProvider* backend) 10 | : InternetProtocolHandler(backend, 0x01) 11 | { 12 | } 13 | 14 | InternetControlMessageProtocol::~InternetControlMessageProtocol() 15 | { 16 | } 17 | 18 | void printf(char*); 19 | void printfHex(uint8_t); 20 | bool InternetControlMessageProtocol::OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE, 21 | common::uint8_t* internetprotocolPayload, common::uint32_t size) 22 | { 23 | if(size < sizeof(InternetControlMessageProtocolMessage)) 24 | return false; 25 | 26 | InternetControlMessageProtocolMessage* msg = (InternetControlMessageProtocolMessage*)internetprotocolPayload; 27 | 28 | switch(msg->type) 29 | { 30 | 31 | case 0: 32 | printf("ping response from "); printfHex(srcIP_BE & 0xFF); 33 | printf("."); printfHex((srcIP_BE >> 8) & 0xFF); 34 | printf("."); printfHex((srcIP_BE >> 16) & 0xFF); 35 | printf("."); printfHex((srcIP_BE >> 24) & 0xFF); 36 | printf("\n"); 37 | break; 38 | 39 | case 8: 40 | msg->type = 0; 41 | msg->checksum = 0; 42 | msg->checksum = InternetProtocolProvider::Checksum((uint16_t*)msg, 43 | sizeof(InternetControlMessageProtocolMessage)); 44 | return true; 45 | } 46 | 47 | return false; 48 | } 49 | 50 | void InternetControlMessageProtocol::RequestEchoReply(uint32_t ip_be) 51 | { 52 | InternetControlMessageProtocolMessage icmp; 53 | icmp.type = 8; // ping 54 | icmp.code = 0; 55 | icmp.data = 0x3713; // 1337 56 | icmp.checksum = 0; 57 | icmp.checksum = InternetProtocolProvider::Checksum((uint16_t*)&icmp, 58 | sizeof(InternetControlMessageProtocolMessage)); 59 | 60 | InternetProtocolHandler::Send(ip_be, (uint8_t*)&icmp, sizeof(InternetControlMessageProtocolMessage)); 61 | } 62 | -------------------------------------------------------------------------------- /src/net/ipv4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace myos; 4 | using namespace myos::common; 5 | using namespace myos::net; 6 | 7 | 8 | 9 | 10 | InternetProtocolHandler::InternetProtocolHandler(InternetProtocolProvider* backend, uint8_t protocol) 11 | { 12 | this->backend = backend; 13 | this->ip_protocol = protocol; 14 | backend->handlers[protocol] = this; 15 | } 16 | 17 | InternetProtocolHandler::~InternetProtocolHandler() 18 | { 19 | if(backend->handlers[ip_protocol] == this) 20 | backend->handlers[ip_protocol] = 0; 21 | } 22 | 23 | bool InternetProtocolHandler::OnInternetProtocolReceived(uint32_t srcIP_BE, uint32_t dstIP_BE, 24 | uint8_t* internetprotocolPayload, uint32_t size) 25 | { 26 | return false; 27 | } 28 | 29 | void InternetProtocolHandler::Send(uint32_t dstIP_BE, uint8_t* internetprotocolPayload, uint32_t size) 30 | { 31 | backend->Send(dstIP_BE, ip_protocol, internetprotocolPayload, size); 32 | } 33 | 34 | 35 | 36 | 37 | InternetProtocolProvider::InternetProtocolProvider(EtherFrameProvider* backend, 38 | AddressResolutionProtocol* arp, 39 | uint32_t gatewayIP, uint32_t subnetMask) 40 | : EtherFrameHandler(backend, 0x800) 41 | { 42 | for(int i = 0; i < 255; i++) 43 | handlers[i] = 0; 44 | this->arp = arp; 45 | this->gatewayIP = gatewayIP; 46 | this->subnetMask = subnetMask; 47 | } 48 | 49 | InternetProtocolProvider::~InternetProtocolProvider() 50 | { 51 | } 52 | 53 | bool InternetProtocolProvider::OnEtherFrameReceived(uint8_t* etherframePayload, uint32_t size) 54 | { 55 | if(size < sizeof(InternetProtocolV4Message)) 56 | return false; 57 | 58 | InternetProtocolV4Message* ipmessage = (InternetProtocolV4Message*)etherframePayload; 59 | bool sendBack = false; 60 | 61 | if(ipmessage->dstIP == backend->GetIPAddress()) 62 | { 63 | int length = ipmessage->totalLength; 64 | if(length > size) 65 | length = size; 66 | 67 | if(handlers[ipmessage->protocol] != 0) 68 | sendBack = handlers[ipmessage->protocol]->OnInternetProtocolReceived( 69 | ipmessage->srcIP, ipmessage->dstIP, 70 | etherframePayload + 4*ipmessage->headerLength, length - 4*ipmessage->headerLength); 71 | 72 | } 73 | 74 | if(sendBack) 75 | { 76 | uint32_t temp = ipmessage->dstIP; 77 | ipmessage->dstIP = ipmessage->srcIP; 78 | ipmessage->srcIP = temp; 79 | 80 | ipmessage->timeToLive = 0x40; 81 | ipmessage->checksum = 0; 82 | ipmessage->checksum = Checksum((uint16_t*)ipmessage, 4*ipmessage->headerLength); 83 | } 84 | 85 | return sendBack; 86 | } 87 | 88 | 89 | void InternetProtocolProvider::Send(uint32_t dstIP_BE, uint8_t protocol, uint8_t* data, uint32_t size) 90 | { 91 | 92 | uint8_t* buffer = (uint8_t*)MemoryManager::activeMemoryManager->malloc(sizeof(InternetProtocolV4Message) + size); 93 | InternetProtocolV4Message *message = (InternetProtocolV4Message*)buffer; 94 | 95 | message->version = 4; 96 | message->headerLength = sizeof(InternetProtocolV4Message)/4; 97 | message->tos = 0; 98 | message->totalLength = size + sizeof(InternetProtocolV4Message); 99 | message->totalLength = ((message->totalLength & 0xFF00) >> 8) 100 | | ((message->totalLength & 0x00FF) << 8); 101 | message->ident = 0x0100; 102 | message->flagsAndOffset = 0x0040; 103 | message->timeToLive = 0x40; 104 | message->protocol = protocol; 105 | 106 | message->dstIP = dstIP_BE; 107 | message->srcIP = backend->GetIPAddress(); 108 | 109 | message->checksum = 0; 110 | message->checksum = Checksum((uint16_t*)message, sizeof(InternetProtocolV4Message)); 111 | 112 | uint8_t* databuffer = buffer + sizeof(InternetProtocolV4Message); 113 | for(int i = 0; i < size; i++) 114 | databuffer[i] = data[i]; 115 | 116 | uint32_t route = dstIP_BE; 117 | if((dstIP_BE & subnetMask) != (message->srcIP & subnetMask)) 118 | route = gatewayIP; 119 | 120 | 121 | backend->Send(arp->Resolve(route), this->etherType_BE, buffer, sizeof(InternetProtocolV4Message) + size); 122 | 123 | 124 | MemoryManager::activeMemoryManager->free(buffer); 125 | } 126 | 127 | 128 | uint16_t InternetProtocolProvider::Checksum(uint16_t* data, uint32_t lengthInBytes) 129 | { 130 | uint32_t temp = 0; 131 | 132 | for(int i = 0; i < lengthInBytes/2; i++) 133 | temp += ((data[i] & 0xFF00) >> 8) | ((data[i] & 0x00FF) << 8); 134 | 135 | if(lengthInBytes % 2) 136 | temp += ((uint16_t)((char*)data)[lengthInBytes-1]) << 8; 137 | 138 | while(temp & 0xFFFF0000) 139 | temp = (temp & 0xFFFF) + (temp >> 16); 140 | 141 | return ((~temp & 0xFF00) >> 8) | ((~temp & 0x00FF) << 8); 142 | } 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /src/net/tcp.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | using namespace myos; 6 | using namespace myos::common; 7 | using namespace myos::net; 8 | 9 | 10 | 11 | TransmissionControlProtocolHandler::TransmissionControlProtocolHandler() 12 | { 13 | } 14 | 15 | TransmissionControlProtocolHandler::~TransmissionControlProtocolHandler() 16 | { 17 | } 18 | 19 | bool TransmissionControlProtocolHandler::HandleTransmissionControlProtocolMessage(TransmissionControlProtocolSocket* socket, uint8_t* data, uint16_t size) 20 | { 21 | return true; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | TransmissionControlProtocolSocket::TransmissionControlProtocolSocket(TransmissionControlProtocolProvider* backend) 29 | { 30 | this->backend = backend; 31 | handler = 0; 32 | state = CLOSED; 33 | } 34 | 35 | TransmissionControlProtocolSocket::~TransmissionControlProtocolSocket() 36 | { 37 | } 38 | 39 | bool TransmissionControlProtocolSocket::HandleTransmissionControlProtocolMessage(uint8_t* data, uint16_t size) 40 | { 41 | if(handler != 0) 42 | return handler->HandleTransmissionControlProtocolMessage(this, data, size); 43 | return false; 44 | } 45 | 46 | void TransmissionControlProtocolSocket::Send(uint8_t* data, uint16_t size) 47 | { 48 | while(state != ESTABLISHED) 49 | { 50 | } 51 | backend->Send(this, data, size, PSH|ACK); 52 | } 53 | 54 | void TransmissionControlProtocolSocket::Disconnect() 55 | { 56 | backend->Disconnect(this); 57 | } 58 | 59 | 60 | 61 | 62 | 63 | TransmissionControlProtocolProvider::TransmissionControlProtocolProvider(InternetProtocolProvider* backend) 64 | : InternetProtocolHandler(backend, 0x06) 65 | { 66 | for(int i = 0; i < 65535; i++) 67 | sockets[i] = 0; 68 | numSockets = 0; 69 | freePort = 1024; 70 | } 71 | 72 | TransmissionControlProtocolProvider::~TransmissionControlProtocolProvider() 73 | { 74 | } 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | uint32_t bigEndian32(uint32_t x) 83 | { 84 | return ((x & 0xFF000000) >> 24) 85 | | ((x & 0x00FF0000) >> 8) 86 | | ((x & 0x0000FF00) << 8) 87 | | ((x & 0x000000FF) << 24); 88 | } 89 | 90 | 91 | 92 | bool TransmissionControlProtocolProvider::OnInternetProtocolReceived(uint32_t srcIP_BE, uint32_t dstIP_BE, 93 | uint8_t* internetprotocolPayload, uint32_t size) 94 | { 95 | 96 | if(size < 20) 97 | return false; 98 | TransmissionControlProtocolHeader* msg = (TransmissionControlProtocolHeader*)internetprotocolPayload; 99 | 100 | uint16_t localPort = msg->dstPort; 101 | uint16_t remotePort = msg->srcPort; 102 | 103 | TransmissionControlProtocolSocket* socket = 0; 104 | for(uint16_t i = 0; i < numSockets && socket == 0; i++) 105 | { 106 | if( sockets[i]->localPort == msg->dstPort 107 | && sockets[i]->localIP == dstIP_BE 108 | && sockets[i]->state == LISTEN 109 | && (((msg -> flags) & (SYN | ACK)) == SYN)) 110 | socket = sockets[i]; 111 | else if( sockets[i]->localPort == msg->dstPort 112 | && sockets[i]->localIP == dstIP_BE 113 | && sockets[i]->remotePort == msg->srcPort 114 | && sockets[i]->remoteIP == srcIP_BE) 115 | socket = sockets[i]; 116 | } 117 | 118 | 119 | 120 | bool reset = false; 121 | 122 | if(socket != 0 && msg->flags & RST) 123 | socket->state = CLOSED; 124 | 125 | 126 | if(socket != 0 && socket->state != CLOSED) 127 | { 128 | switch((msg -> flags) & (SYN | ACK | FIN)) 129 | { 130 | case SYN: 131 | if(socket -> state == LISTEN) 132 | { 133 | socket->state = SYN_RECEIVED; 134 | socket->remotePort = msg->srcPort; 135 | socket->remoteIP = srcIP_BE; 136 | socket->acknowledgementNumber = bigEndian32( msg->sequenceNumber ) + 1; 137 | socket->sequenceNumber = 0xbeefcafe; 138 | Send(socket, 0,0, SYN|ACK); 139 | socket->sequenceNumber++; 140 | } 141 | else 142 | reset = true; 143 | break; 144 | 145 | 146 | case SYN | ACK: 147 | if(socket->state == SYN_SENT) 148 | { 149 | socket->state = ESTABLISHED; 150 | socket->acknowledgementNumber = bigEndian32( msg->sequenceNumber ) + 1; 151 | socket->sequenceNumber++; 152 | Send(socket, 0,0, ACK); 153 | } 154 | else 155 | reset = true; 156 | break; 157 | 158 | 159 | case SYN | FIN: 160 | case SYN | FIN | ACK: 161 | reset = true; 162 | break; 163 | 164 | 165 | case FIN: 166 | case FIN|ACK: 167 | if(socket->state == ESTABLISHED) 168 | { 169 | socket->state = CLOSE_WAIT; 170 | socket->acknowledgementNumber++; 171 | Send(socket, 0,0, ACK); 172 | Send(socket, 0,0, FIN|ACK); 173 | } 174 | else if(socket->state == CLOSE_WAIT) 175 | { 176 | socket->state = CLOSED; 177 | } 178 | else if(socket->state == FIN_WAIT1 179 | || socket->state == FIN_WAIT2) 180 | { 181 | socket->state = CLOSED; 182 | socket->acknowledgementNumber++; 183 | Send(socket, 0,0, ACK); 184 | } 185 | else 186 | reset = true; 187 | break; 188 | 189 | 190 | case ACK: 191 | if(socket->state == SYN_RECEIVED) 192 | { 193 | socket->state = ESTABLISHED; 194 | return false; 195 | } 196 | else if(socket->state == FIN_WAIT1) 197 | { 198 | socket->state = FIN_WAIT2; 199 | return false; 200 | } 201 | else if(socket->state == CLOSE_WAIT) 202 | { 203 | socket->state = CLOSED; 204 | break; 205 | } 206 | 207 | if(msg->flags == ACK) 208 | break; 209 | 210 | // no break, because of piggybacking 211 | 212 | default: 213 | 214 | if(bigEndian32(msg->sequenceNumber) == socket->acknowledgementNumber) 215 | { 216 | reset = !(socket->HandleTransmissionControlProtocolMessage(internetprotocolPayload + msg->headerSize32*4, 217 | size - msg->headerSize32*4)); 218 | if(!reset) 219 | { 220 | int x = 0; 221 | for(int i = msg->headerSize32*4; i < size; i++) 222 | if(internetprotocolPayload[i] != 0) 223 | x = i; 224 | socket->acknowledgementNumber += x - msg->headerSize32*4 + 1; 225 | Send(socket, 0,0, ACK); 226 | } 227 | } 228 | else 229 | { 230 | // data in wrong order 231 | reset = true; 232 | } 233 | 234 | } 235 | } 236 | 237 | 238 | if(reset) 239 | { 240 | if(socket != 0) 241 | { 242 | Send(socket, 0,0, RST); 243 | } 244 | else 245 | { 246 | TransmissionControlProtocolSocket socket(this); 247 | socket.remotePort = msg->srcPort; 248 | socket.remoteIP = srcIP_BE; 249 | socket.localPort = msg->dstPort; 250 | socket.localIP = dstIP_BE; 251 | socket.sequenceNumber = bigEndian32(msg->acknowledgementNumber); 252 | socket.acknowledgementNumber = bigEndian32(msg->sequenceNumber) + 1; 253 | Send(&socket, 0,0, RST); 254 | } 255 | } 256 | 257 | 258 | if(socket != 0 && socket->state == CLOSED) 259 | for(uint16_t i = 0; i < numSockets && socket == 0; i++) 260 | if(sockets[i] == socket) 261 | { 262 | sockets[i] = sockets[--numSockets]; 263 | MemoryManager::activeMemoryManager->free(socket); 264 | break; 265 | } 266 | 267 | 268 | 269 | return false; 270 | } 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | // ------------------------------------------------------------------------------------------ 285 | 286 | 287 | 288 | 289 | void TransmissionControlProtocolProvider::Send(TransmissionControlProtocolSocket* socket, uint8_t* data, uint16_t size, uint16_t flags) 290 | { 291 | uint16_t totalLength = size + sizeof(TransmissionControlProtocolHeader); 292 | uint16_t lengthInclPHdr = totalLength + sizeof(TransmissionControlProtocolPseudoHeader); 293 | 294 | uint8_t* buffer = (uint8_t*)MemoryManager::activeMemoryManager->malloc(lengthInclPHdr); 295 | 296 | TransmissionControlProtocolPseudoHeader* phdr = (TransmissionControlProtocolPseudoHeader*)buffer; 297 | TransmissionControlProtocolHeader* msg = (TransmissionControlProtocolHeader*)(buffer + sizeof(TransmissionControlProtocolPseudoHeader)); 298 | uint8_t* buffer2 = buffer + sizeof(TransmissionControlProtocolHeader) 299 | + sizeof(TransmissionControlProtocolPseudoHeader); 300 | 301 | msg->headerSize32 = sizeof(TransmissionControlProtocolHeader)/4; 302 | msg->srcPort = socket->localPort; 303 | msg->dstPort = socket->remotePort; 304 | 305 | msg->acknowledgementNumber = bigEndian32( socket->acknowledgementNumber ); 306 | msg->sequenceNumber = bigEndian32( socket->sequenceNumber ); 307 | msg->reserved = 0; 308 | msg->flags = flags; 309 | msg->windowSize = 0xFFFF; 310 | msg->urgentPtr = 0; 311 | 312 | msg->options = ((flags & SYN) != 0) ? 0xB4050402 : 0; 313 | 314 | socket->sequenceNumber += size; 315 | 316 | for(int i = 0; i < size; i++) 317 | buffer2[i] = data[i]; 318 | 319 | phdr->srcIP = socket->localIP; 320 | phdr->dstIP = socket->remoteIP; 321 | phdr->protocol = 0x0600; 322 | phdr->totalLength = ((totalLength & 0x00FF) << 8) | ((totalLength & 0xFF00) >> 8); 323 | 324 | msg -> checksum = 0; 325 | msg -> checksum = InternetProtocolProvider::Checksum((uint16_t*)buffer, lengthInclPHdr); 326 | 327 | 328 | 329 | InternetProtocolHandler::Send(socket->remoteIP, (uint8_t*)msg, totalLength); 330 | MemoryManager::activeMemoryManager->free(buffer); 331 | } 332 | 333 | 334 | 335 | TransmissionControlProtocolSocket* TransmissionControlProtocolProvider::Connect(uint32_t ip, uint16_t port) 336 | { 337 | TransmissionControlProtocolSocket* socket = (TransmissionControlProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(TransmissionControlProtocolSocket)); 338 | 339 | if(socket != 0) 340 | { 341 | new (socket) TransmissionControlProtocolSocket(this); 342 | 343 | socket -> remotePort = port; 344 | socket -> remoteIP = ip; 345 | socket -> localPort = freePort++; 346 | socket -> localIP = backend->GetIPAddress(); 347 | 348 | socket -> remotePort = ((socket -> remotePort & 0xFF00)>>8) | ((socket -> remotePort & 0x00FF) << 8); 349 | socket -> localPort = ((socket -> localPort & 0xFF00)>>8) | ((socket -> localPort & 0x00FF) << 8); 350 | 351 | sockets[numSockets++] = socket; 352 | socket -> state = SYN_SENT; 353 | 354 | socket -> sequenceNumber = 0xbeefcafe; 355 | 356 | Send(socket, 0,0, SYN); 357 | } 358 | 359 | return socket; 360 | } 361 | 362 | 363 | 364 | void TransmissionControlProtocolProvider::Disconnect(TransmissionControlProtocolSocket* socket) 365 | { 366 | socket->state = FIN_WAIT1; 367 | Send(socket, 0,0, FIN + ACK); 368 | socket->sequenceNumber++; 369 | } 370 | 371 | 372 | TransmissionControlProtocolSocket* TransmissionControlProtocolProvider::Listen(uint16_t port) 373 | { 374 | TransmissionControlProtocolSocket* socket = (TransmissionControlProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(TransmissionControlProtocolSocket)); 375 | 376 | if(socket != 0) 377 | { 378 | new (socket) TransmissionControlProtocolSocket(this); 379 | 380 | socket -> state = LISTEN; 381 | socket -> localIP = backend->GetIPAddress(); 382 | socket -> localPort = ((port & 0xFF00)>>8) | ((port & 0x00FF) << 8); 383 | 384 | sockets[numSockets++] = socket; 385 | } 386 | 387 | return socket; 388 | } 389 | void TransmissionControlProtocolProvider::Bind(TransmissionControlProtocolSocket* socket, TransmissionControlProtocolHandler* handler) 390 | { 391 | socket->handler = handler; 392 | } 393 | 394 | 395 | 396 | 397 | 398 | -------------------------------------------------------------------------------- /src/net/udp.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos; 5 | using namespace myos::common; 6 | using namespace myos::net; 7 | 8 | 9 | 10 | UserDatagramProtocolHandler::UserDatagramProtocolHandler() 11 | { 12 | } 13 | 14 | UserDatagramProtocolHandler::~UserDatagramProtocolHandler() 15 | { 16 | } 17 | 18 | void UserDatagramProtocolHandler::HandleUserDatagramProtocolMessage(UserDatagramProtocolSocket* socket, uint8_t* data, uint16_t size) 19 | { 20 | } 21 | 22 | 23 | 24 | 25 | 26 | UserDatagramProtocolSocket::UserDatagramProtocolSocket(UserDatagramProtocolProvider* backend) 27 | { 28 | this->backend = backend; 29 | handler = 0; 30 | listening = false; 31 | } 32 | 33 | UserDatagramProtocolSocket::~UserDatagramProtocolSocket() 34 | { 35 | } 36 | 37 | void UserDatagramProtocolSocket::HandleUserDatagramProtocolMessage(uint8_t* data, uint16_t size) 38 | { 39 | if(handler != 0) 40 | handler->HandleUserDatagramProtocolMessage(this, data, size); 41 | } 42 | 43 | void UserDatagramProtocolSocket::Send(uint8_t* data, uint16_t size) 44 | { 45 | backend->Send(this, data, size); 46 | } 47 | 48 | void UserDatagramProtocolSocket::Disconnect() 49 | { 50 | backend->Disconnect(this); 51 | } 52 | 53 | 54 | 55 | 56 | 57 | UserDatagramProtocolProvider::UserDatagramProtocolProvider(InternetProtocolProvider* backend) 58 | : InternetProtocolHandler(backend, 0x11) 59 | { 60 | for(int i = 0; i < 65535; i++) 61 | sockets[i] = 0; 62 | numSockets = 0; 63 | freePort = 1024; 64 | } 65 | 66 | UserDatagramProtocolProvider::~UserDatagramProtocolProvider() 67 | { 68 | } 69 | 70 | bool UserDatagramProtocolProvider::OnInternetProtocolReceived(uint32_t srcIP_BE, uint32_t dstIP_BE, 71 | uint8_t* internetprotocolPayload, uint32_t size) 72 | { 73 | if(size < sizeof(UserDatagramProtocolHeader)) 74 | return false; 75 | 76 | UserDatagramProtocolHeader* msg = (UserDatagramProtocolHeader*)internetprotocolPayload; 77 | uint16_t localPort = msg->dstPort; 78 | uint16_t remotePort = msg->srcPort; 79 | 80 | 81 | UserDatagramProtocolSocket* socket = 0; 82 | for(uint16_t i = 0; i < numSockets && socket == 0; i++) 83 | { 84 | if( sockets[i]->localPort == msg->dstPort 85 | && sockets[i]->localIP == dstIP_BE 86 | && sockets[i]->listening) 87 | { 88 | socket = sockets[i]; 89 | socket->listening = false; 90 | socket->remotePort = msg->srcPort; 91 | socket->remoteIP = srcIP_BE; 92 | } 93 | 94 | else if( sockets[i]->localPort == msg->dstPort 95 | && sockets[i]->localIP == dstIP_BE 96 | && sockets[i]->remotePort == msg->srcPort 97 | && sockets[i]->remoteIP == srcIP_BE) 98 | socket = sockets[i]; 99 | } 100 | 101 | if(socket != 0) 102 | socket->HandleUserDatagramProtocolMessage(internetprotocolPayload + sizeof(UserDatagramProtocolHeader), 103 | size - sizeof(UserDatagramProtocolHeader)); 104 | 105 | return false; 106 | } 107 | 108 | 109 | UserDatagramProtocolSocket* UserDatagramProtocolProvider::Connect(uint32_t ip, uint16_t port) 110 | { 111 | UserDatagramProtocolSocket* socket = (UserDatagramProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(UserDatagramProtocolSocket)); 112 | 113 | if(socket != 0) 114 | { 115 | new (socket) UserDatagramProtocolSocket(this); 116 | 117 | socket -> remotePort = port; 118 | socket -> remoteIP = ip; 119 | socket -> localPort = freePort++; 120 | socket -> localIP = backend->GetIPAddress(); 121 | 122 | socket -> remotePort = ((socket -> remotePort & 0xFF00)>>8) | ((socket -> remotePort & 0x00FF) << 8); 123 | socket -> localPort = ((socket -> localPort & 0xFF00)>>8) | ((socket -> localPort & 0x00FF) << 8); 124 | 125 | sockets[numSockets++] = socket; 126 | } 127 | 128 | return socket; 129 | } 130 | 131 | 132 | 133 | UserDatagramProtocolSocket* UserDatagramProtocolProvider::Listen(uint16_t port) 134 | { 135 | UserDatagramProtocolSocket* socket = (UserDatagramProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(UserDatagramProtocolSocket)); 136 | 137 | if(socket != 0) 138 | { 139 | new (socket) UserDatagramProtocolSocket(this); 140 | 141 | socket -> listening = true; 142 | socket -> localPort = port; 143 | socket -> localIP = backend->GetIPAddress(); 144 | 145 | socket -> localPort = ((socket -> localPort & 0xFF00)>>8) | ((socket -> localPort & 0x00FF) << 8); 146 | 147 | sockets[numSockets++] = socket; 148 | } 149 | 150 | return socket; 151 | } 152 | 153 | 154 | void UserDatagramProtocolProvider::Disconnect(UserDatagramProtocolSocket* socket) 155 | { 156 | for(uint16_t i = 0; i < numSockets && socket == 0; i++) 157 | if(sockets[i] == socket) 158 | { 159 | sockets[i] = sockets[--numSockets]; 160 | MemoryManager::activeMemoryManager->free(socket); 161 | break; 162 | } 163 | } 164 | 165 | void UserDatagramProtocolProvider::Send(UserDatagramProtocolSocket* socket, uint8_t* data, uint16_t size) 166 | { 167 | uint16_t totalLength = size + sizeof(UserDatagramProtocolHeader); 168 | uint8_t* buffer = (uint8_t*)MemoryManager::activeMemoryManager->malloc(totalLength); 169 | uint8_t* buffer2 = buffer + sizeof(UserDatagramProtocolHeader); 170 | 171 | UserDatagramProtocolHeader* msg = (UserDatagramProtocolHeader*)buffer; 172 | 173 | msg->srcPort = socket->localPort; 174 | msg->dstPort = socket->remotePort; 175 | msg->length = ((totalLength & 0x00FF) << 8) | ((totalLength & 0xFF00) >> 8); 176 | 177 | for(int i = 0; i < size; i++) 178 | buffer2[i] = data[i]; 179 | 180 | msg -> checksum = 0; 181 | InternetProtocolHandler::Send(socket->remoteIP, buffer, totalLength); 182 | 183 | MemoryManager::activeMemoryManager->free(buffer); 184 | } 185 | 186 | void UserDatagramProtocolProvider::Bind(UserDatagramProtocolSocket* socket, UserDatagramProtocolHandler* handler) 187 | { 188 | socket->handler = handler; 189 | } 190 | 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /src/syscalls.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace myos; 5 | using namespace myos::common; 6 | using namespace myos::hardwarecommunication; 7 | 8 | SyscallHandler::SyscallHandler(InterruptManager* interruptManager, uint8_t InterruptNumber) 9 | : InterruptHandler(interruptManager, InterruptNumber + interruptManager->HardwareInterruptOffset()) 10 | { 11 | } 12 | 13 | SyscallHandler::~SyscallHandler() 14 | { 15 | } 16 | 17 | 18 | void printf(char*); 19 | 20 | uint32_t SyscallHandler::HandleInterrupt(uint32_t esp) 21 | { 22 | CPUState* cpu = (CPUState*)esp; 23 | 24 | 25 | switch(cpu->eax) 26 | { 27 | case 4: 28 | printf((char*)cpu->ebx); 29 | break; 30 | 31 | default: 32 | break; 33 | } 34 | 35 | 36 | return esp; 37 | } 38 | 39 | --------------------------------------------------------------------------------