diff options
74 files changed, 12559 insertions, 0 deletions
diff --git a/sos-code-article6.5/INSTALL b/sos-code-article6.5/INSTALL new file mode 100644 index 0000000..7c7d619 --- /dev/null +++ b/sos-code-article6.5/INSTALL @@ -0,0 +1,118 @@ + + SOS: A Simple Operating System + + Compilation/Installation/Test instructions + + +Compilation +=========== + +IMPORTANT +--------- + +Don't forget to run 'make clean' before 'make' after you have modified +any source or header file(s). + + +On a x86 host where grub is correctly installed +----------------------------------------------- + +Simply run 'make' + + +On a non-x86 host (without grub of course !) +-------------------------------------------- + +See extra/README + + +On an x86 host without Grub, or with a buggy Grub +------------------------------------------------- + +See extra/README + +How do I know I have a buggy grub installation ? Answer: in the qemu +PC emulator, Grub hangs while loading the kernel + + +Installation +============ + +Nothing special to do besides compiling + + +Test the SOS Kernel +=================== + +On a x86 real machine with Grub installed +----------------------------------------- + + 1st method + => Boot the sos.elf file (append 'kernel=<path_to>sos.elf' in the + menu.lst or type it on Grub's command line) from a hard disk, a + floppy, or from the network + + 2nd method + => Copy the file 'fd.img' to a floppy and boot from it + + +On a x86 real machine without Grub installed +-------------------------------------------- + + 1st method + => see extra/README to compile with the grub floppy image we provide, + copy the file 'fd.img' to a floppy, and boot from it + + 2nd method + => see extra/README to compile with the boot sector we provide (up to + article 2 only), copy the file 'extra/sos_bsect.img' to a floppy, + and boot from it + + +Inside a PC emulator (x86 and non-x86 hosts) +-------------------------------------------- + +Tested on both the bochs emulator (x86/linux, sparc/solaris and +ppc/linux hosts, 'apt-get install bochs-x vgabios' on debian +testing/unstable), and the qemu system emulator (with libsdl +installed: 'apt-get install libsdl1.2-dev' on debian +testing/unstable). + + 1/ Grub is installed on the host (x86 hosts only) + - - - - - - - - - - - - - - - - - - - - - - - - - + + bochs: boot from the file 'fd.img'. Example of a ~/.bochsrc: + floppya: 1_44=/home/d2/sos/fd.img, status=inserted + romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xf0000 + vgaromimage: /usr/share/vgabios/vgabios.bin + megs:63 # 63 Mo de RAM + + qemu: run 'qemu -fda fd.img' + If grub hangs while loading the kernel, please go to method 2/ + + 2/ Grub is not installed (all hosts) + - - - - - - - - - - - - - - - - - - + + See extra/README to generate a floppy image with the Grub floppy + image we provide, and: + + bochs: boot from the file 'fd.img' + + qemu: run 'qemu -fda fd.img' + + 3/ Bonus: boot with the bootsector we provide (all hosts, up to art. 2 ONLY !) + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + See extra/README to generate a floppy image with the boot sector we + provide, and: + + bochs: boot from the file 'extra/sos_bsect.img' + + qemu: run 'qemu -fda extra/sos_qemu.img' + + NOTE: After article 2, this way of booting is not supported: please + use the method 2/ above. + + +-- +David Decotigny diff --git a/sos-code-article6.5/LICENSE b/sos-code-article6.5/LICENSE new file mode 100644 index 0000000..60549be --- /dev/null +++ b/sos-code-article6.5/LICENSE @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) 19yy <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/sos-code-article6.5/Makefile b/sos-code-article6.5/Makefile new file mode 100644 index 0000000..926f30b --- /dev/null +++ b/sos-code-article6.5/Makefile @@ -0,0 +1,83 @@ +## Copyright (C) 2004,2005 The SOS Team +## +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 2 +## of the License, or (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, +## USA. + +CC=i586-elf-gcc +LD=i586-elf-ld +CFLAGS = -Wall -nostdinc -ffreestanding -DKERNEL_SOS +LDFLAGS = --warn-common -nostdlib +OBJECTS = bootstrap/multiboot.o \ + hwcore/idt.o hwcore/gdt.o \ + hwcore/exception.o hwcore/exception_wrappers.o \ + hwcore/irq.o hwcore/irq_wrappers.o hwcore/i8259.o \ + hwcore/paging.o \ + hwcore/i8254.o drivers/x86_videomem.o drivers/bochs.o \ + hwcore/cpu_context.o hwcore/cpu_context_switch.o \ + sos/kmem_vmm.o sos/kmem_slab.o sos/kmalloc.o \ + sos/physmem.o sos/klibc.o \ + sos/thread.o sos/kwaitq.o \ + sos/time.o sos/sched.o sos/ksynch.o \ + sos/assert.o sos/main.o sos/mouse_sim.o + +KERNEL_OBJ = sos.elf +MULTIBOOT_IMAGE = sos.iso +PWD := $(shell pwd) + +# Main target +all: $(MULTIBOOT_IMAGE) + +$(MULTIBOOT_IMAGE): $(KERNEL_OBJ) + # ./support/build_image.sh $@ $< + if [ ! -e cdrom/boot/grub/stage2_eltorito ]; then \ + mkdir -p cdrom/boot/grub; \ + echo "Please copy grub's stage2_eltorito to cdrom/boot/grub."; \ + exit -1; \ + fi + cp $(KERNEL_OBJ) cdrom + echo timeout 0 > cdrom/boot/grub/menu.lst + echo title Simple OS >> cdrom/boot/grub/menu.lst + echo kernel /$(KERNEL_OBJ) >> cdrom/boot/grub/menu.lst + genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 \ + -boot-info-table -input-charset ascii -A SOS -o $(MULTIBOOT_IMAGE) cdrom + +$(KERNEL_OBJ): $(OBJECTS) ./support/sos.lds + $(LD) $(LDFLAGS) -T ./support/sos.lds -o $@ $(OBJECTS) + -nm -C $@ | cut -d ' ' -f 1,3 > sos.map + size $@ + +-include .mkvars + +# Create objects from C source code +%.o: %.c + $(CC) -I$(PWD) -c $< $(CFLAGS) -o $@ + +# Create objects from assembler (.S) source code +%.o: %.S + $(CC) -I$(PWD) -c $< $(CFLAGS) -DASM_SOURCE=1 -o $@ + +FORCE: + @ + +# Clean directory +clean: + $(RM) *.iso *.o mtoolsrc *~ menu.txt *.img *.elf *.bin *.map + $(RM) *.log *.out bochs* + $(RM) bootstrap/*.o bootstrap/*~ + $(RM) drivers/*.o drivers/*~ + $(RM) hwcore/*.o hwcore/*~ + $(RM) sos/*.o sos/*~ + $(RM) support/*~ + $(RM) extra/*~ diff --git a/sos-code-article6.5/README b/sos-code-article6.5/README new file mode 100644 index 0000000..efbbc89 --- /dev/null +++ b/sos-code-article6.5/README @@ -0,0 +1,90 @@ + + SOS: A Simple Operating System + + +This is SOS, a Simple Operating System for i386-family +processors. This is as simple as possible to show a way to program a +basic Operating System on real common hardware (PC). The code should +be easily readable and understandable thanks to frequent comments, and +references to external documentation. We chose to implement the basic +features of an OS, thus making design decisions targetting towards +simplicity of understanding, covering most of the OS classical +concepts, but not aiming at proposing yet another full-fledged +competitive OS (Linux is quite good at it). However, for those who +would like to propose some enhancements, we are open to any code +suggestions (patches only, please). And yes, there might be bugs in +the code, so please send us any bug report, and/or patches ! + +The OS comes as a set of articles (in french) to be published in the +journal "Linux Magazine France". Each month, the part of the code +related to the current article's theme is released (see VERSION file), +and the resulting OS can be successfully compiled and run, by booting +it from a floppy on a real machine (tested AMD k7, Cyrix and Intel P4 +pentiums), or through an x86 emulator (bochs or qemu). The resulting +OS is available as a multiboot compliant ELF kernel (sos.elf) and as a +floppy image (fd.img). It provides a very very very basic demo whose +aim is to understand how everything works, not to animate sprites on +the screen with 5:1 dolby sound. + +The initial technical features and lack-of-features of the OS are: + - monolithic kernel, fully interruptible, non-preemptible (big kernel + lock), target machines = i386 PC or better + - compiles on any host where the gcc/binutils toolchain (target + i586-gnu) is available. Can be tested on real i486/pentium + hardware, or on any host that can run an i486/pentium PC emulator + (bochs or qemu) + - kernel loaded by grub, or by a sample bootsector (up to article 2 + ONLY) + - clear separation of physical memory and virtual memory concepts, + even inside the kernel: no identity-mapping of the physical memory + inside the kernel (allows to move virtual mappings of kernel pages + at run-time, eg to free ISA DMA pages, and to avercome the 4G RAM + barrier) + - slab-type kernel memory allocation + - no swap, no reverse mapping + - VERY simple drivers: keyboard, x86 video memory, IDE disks + - logical devices: partitions, FAT filesystem, "hard-coded" + mountpoints only (~ MSDOS) + - no network stack + - user-level features: ELF loader (no shared libraries), processes, + user threads (kernel-level scheduling only), mmap API, basic VFS + +To understand where to look at for what, here is a brief description: + - Makefile: the (ONLY) makefile of the OS. Targets are basically + 'all' and 'clean' + - bootstrap/ directory: code to load the kernel. Both the stuff + needed for a multiboot-compliant loader (eg grub) AND a bootsector + are provided. The bootsector may only be used up to article 2. + - sos/ directory: the entry routine for the kernel (main.c), various + systemwide header files, a set of common useful C routines + ("nano-klibc"), and kernel subsystems (kernel memory management, + etc...) + - hwcore/ directory: Low-level CPU- and kernel-related routines + (interrupt/exception management, translation tables and segment + registers, ...) + - drivers/ directory: basic kernel drivers for various (non CPU) + devices (keyboard, x86 video memory, bochs 0xe9 port, ...). Used + mainly for debugging + - support/ directory: scripts and configuration files to build the + floppy images + - extra/ directory: a set of configuration files to be customized for + non-x86 host installations (yes, we primarily develop SOS on a ppc, for + the x86 target of course), or for grub-less installations. See + README file in this directory. + +The code is licensed under the terms of the GNU GPL version 2 (see +LICENSE file). + +Enjoy ! + + David Decotigny, Thomas Petazzoni, the Kos team + http://sos.enix.org/ + http://david.decotigny.free.fr/ + http://kos.enix.org/~thomas/ + http://kos.enix.org/ + + +-- +David Decotigny + +PS: Made with a Mac. diff --git a/sos-code-article6.5/VERSION b/sos-code-article6.5/VERSION new file mode 100644 index 0000000..aeb9822 --- /dev/null +++ b/sos-code-article6.5/VERSION @@ -0,0 +1,13 @@ +SOS -- Simple OS +Copyright (C) 2003,2004,2005 The SOS Team (David Decotigny & Thomas Petazzoni) + +Version "Article 6 (2nd part [FIFO])" -- Basic kernel thread and + synchronization API with basic + FIFO scheduler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + See the LICENSE file included in the distribution. diff --git a/sos-code-article6.5/bootstrap/multiboot.S b/sos-code-article6.5/bootstrap/multiboot.S new file mode 100644 index 0000000..546d947 --- /dev/null +++ b/sos-code-article6.5/bootstrap/multiboot.S @@ -0,0 +1,85 @@ +/* Copyright (C) 1999 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + + +/* The operating system is booted by Grub, so we almost have nothing + to do to boot it. We only have to conform to the Multiboot + standard, as defined by the Grub documentation */ + +#define ASM 1 +/* The multiboot.h header contains a lot of multiboot standard + definitions */ +#include "multiboot.h" + + /* The multiboot header itself. It must come first. */ +.section ".multiboot" + /* Multiboot header must be aligned on a 4-byte boundary */ + .align 4 +multiboot_header: + /* magic= */ .long MULTIBOOT_HEADER_MAGIC + /* flags= */ .long MULTIBOOT_HEADER_FLAGS + /* checksum= */ .long -(MULTIBOOT_HEADER_MAGIC \ + +MULTIBOOT_HEADER_FLAGS) + /* header_addr= */ .long multiboot_header + /* load_addr= */ .long __b_load + /* load_end_addr=*/ .long __e_load + /* bss_end_addr= */ .long __e_kernel + /* entry_addr= */ .long multiboot_entry + +/* Here is the beginning of the code of our operating system */ +.text + +.globl start, _start +start: +_start: +multiboot_entry: + /* Set up a stack */ + movl $(stack + MULTIBOOT_STACK_SIZE), %ebp + movl %ebp, %esp + + /* Set EFLAGS to 0 */ + pushl $0 + /* pop stack into the EFLAGS register */ + popf + + /* Push the magic and the address on the stack, so that they + will be the parameters of the cmain function */ + pushl %ebx + pushl %eax + + /* Call the cmain function (os.c) */ + call EXT_C(sos_main) + + /* Should never get there */ +loop: + hlt + jmp loop + +/* Here is the stack */ +.section ".init_stack", "aw", @nobits +.size stack, MULTIBOOT_STACK_SIZE +stack: + .space MULTIBOOT_STACK_SIZE + +/* Some data characterizing the stack addresses */ +.data + .globl bootstrap_stack_bottom +bootstrap_stack_bottom: .long stack + + .globl bootstrap_stack_size +bootstrap_stack_size: .long MULTIBOOT_STACK_SIZE diff --git a/sos-code-article6.5/bootstrap/multiboot.h b/sos-code-article6.5/bootstrap/multiboot.h new file mode 100644 index 0000000..ee9cdbd --- /dev/null +++ b/sos-code-article6.5/bootstrap/multiboot.h @@ -0,0 +1,133 @@ +#ifndef __MULTIBOOT_H__ +#define __MULTIBOOT_H__ + +/* multiboot.h - the header for Multiboot */ +/* Copyright (C) 1999 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + +/* Macros. */ + +/* The magic number for the Multiboot header. */ +#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 + +/* The flags for the Multiboot header. */ +#define MULTIBOOT_HEADER_FLAGS 0x00010003 + +/* The magic number passed by a Multiboot-compliant boot loader. */ +#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 + +/* The size of our stack (16KB). */ +#define MULTIBOOT_STACK_SIZE 0x4000 + +#define MULTIBOOT_CMDLINE 4 +#define MULTIBOOT_MODS 8 + +/* C symbol format. HAVE_ASM_USCORE is defined by configure. */ +#ifdef HAVE_ASM_USCORE +# define EXT_C(sym) _ ## sym +#else +# define EXT_C(sym) sym +#endif + +#ifndef ASM +/* Do not include here in the assembler sources. */ + +#include <sos/types.h> + +/* The address of the stack of the bootstrap thread */ +extern sos_vaddr_t bootstrap_stack_bottom; +extern sos_size_t bootstrap_stack_size; + +/* Types. */ + +/* The Multiboot header. */ +typedef struct multiboot_header +{ + unsigned long magic; + unsigned long flags; + unsigned long checksum; + unsigned long header_addr; + unsigned long load_addr; + unsigned long load_end_addr; + unsigned long bss_end_addr; + unsigned long entry_addr; +} multiboot_header_t; + +/* The symbol table for a.out. */ +typedef struct aout_symbol_table +{ + unsigned long tabsize; + unsigned long strsize; + unsigned long addr; + unsigned long reserved; +} aout_symbol_table_t; + +/* The section header table for ELF. */ +typedef struct elf_section_header_table +{ + unsigned long num; + unsigned long size; + unsigned long addr; + unsigned long shndx; +} elf_section_header_table_t; + +/* The Multiboot information. */ +typedef struct multiboot_info +{ + unsigned long flags; + unsigned long mem_lower; + unsigned long mem_upper; + unsigned long boot_device; + unsigned long cmdline; + unsigned long mods_count; + unsigned long mods_addr; + union + { + aout_symbol_table_t aout_sym; + elf_section_header_table_t elf_sec; + } u; + unsigned long mmap_length; + unsigned long mmap_addr; + unsigned long drives_length; + unsigned long drives_addr; +} multiboot_info_t; + +/* The module structure. */ +typedef struct module +{ + unsigned long mod_start; + unsigned long mod_end; + unsigned long string; + unsigned long reserved; +} module_t; + +/* The memory map. Be careful that the offset 0 is base_addr_low + but no size. */ +typedef struct memory_map +{ + unsigned long size; + unsigned long base_addr_low; + unsigned long base_addr_high; + unsigned long length_low; + unsigned long length_high; + unsigned long type; +} memory_map_t; + +void dump_multiboot_info(multiboot_info_t *mbi); + +#endif /* ! ASM */ + +#endif /* __MULTIBOOT_H__ */ diff --git a/sos-code-article6.5/cdrom/boot/grub/stage2_eltorito b/sos-code-article6.5/cdrom/boot/grub/stage2_eltorito Binary files differnew file mode 100644 index 0000000..6d82f08 --- /dev/null +++ b/sos-code-article6.5/cdrom/boot/grub/stage2_eltorito diff --git a/sos-code-article6.5/drivers/bochs.c b/sos-code-article6.5/drivers/bochs.c new file mode 100644 index 0000000..f5b0bfb --- /dev/null +++ b/sos-code-article6.5/drivers/bochs.c @@ -0,0 +1,123 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include <hwcore/ioports.h> +#include <sos/klibc.h> + +#include "bochs.h" + +/* This is a special hack that is only useful when running the + operating system under the Bochs emulator. */ +#define SOS_BOCHS_IOPORT 0xe9 + +sos_ret_t sos_bochs_setup(void) +{ + return SOS_OK; +} + +#define _putchar(chr) \ + outb((chr), SOS_BOCHS_IOPORT) + +sos_ret_t sos_bochs_putchar(char c) +{ + _putchar(c); + + return SOS_OK; +} + +sos_ret_t sos_bochs_putstring(const char* str) +{ + for ( ; str && (*str != '\0') ; str++) + _putchar(*str); + + return SOS_OK; +} + +sos_ret_t sos_bochs_puthex(unsigned val, int nbytes) +{ + unsigned c; + +#define BOCHS_PRTHEX(q) \ + ({ unsigned char r; if ((q) >= 10) r='a'+(q)-10; \ + else r='0'+(q); _putchar(r); }) + + switch (nbytes) + { + case 4: + c = (val >> 24) & 0xff; + BOCHS_PRTHEX((c >> 4)&0xf); + BOCHS_PRTHEX(c&0xf); + case 3: + c = (val >> 16) & 0xff; + BOCHS_PRTHEX((c >> 4)&0xf); + BOCHS_PRTHEX(c&0xf); + case 2: + c = (val >> 8) & 0xff; + BOCHS_PRTHEX((c >> 4)&0xf); + BOCHS_PRTHEX(c&0xf); + case 1: + c = val & 0xff; + BOCHS_PRTHEX((c >> 4)&0xf); + BOCHS_PRTHEX(c&0xf); + } + + return SOS_OK; +} + + +sos_ret_t sos_bochs_hexdump(const void* addr, int nbytes) +{ + int offs; + for (offs = 0 ; offs < nbytes ; offs++) + { + const unsigned char *c; + + if ((offs % 16) == 0) + { + sos_bochs_putstring("0x"); + sos_bochs_puthex(offs, 4); + } + + if ((offs % 8) == 0) + sos_bochs_putstring(" "); + + c = (const unsigned char*)(addr + offs); + sos_bochs_puthex(*c, 1); + sos_bochs_putstring(" "); + + if (((offs + 1) % 16) == 0) + sos_bochs_putstring("\n"); + } + + if (offs % 16) + sos_bochs_putstring("\n"); + + return SOS_OK; +} + + +sos_ret_t sos_bochs_printf(const char *format, /* args */...) +{ + char buff[256]; + va_list ap; + + va_start(ap, format); + vsnprintf(buff, sizeof(buff), format, ap); + va_end(ap); + + return sos_bochs_putstring(buff); +} diff --git a/sos-code-article6.5/drivers/bochs.h b/sos-code-article6.5/drivers/bochs.h new file mode 100644 index 0000000..348506b --- /dev/null +++ b/sos-code-article6.5/drivers/bochs.h @@ -0,0 +1,55 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_BOCHS_H_ +#define _SOS_BOCHS_H_ + +/** + * @file bochs.h + * + * If you compiled Bochs with the --enable-e9-hack, then any character + * printed to the 0xE9 I/O port is printed to the xterm that is + * running Bochs. This may appear to be a detail, but in fact, this + * functionnality is *VERY* precious for debugging purposes. This + * """driver""" handles this feature. + */ + +#include <sos/errno.h> +#include <sos/types.h> + +sos_ret_t sos_bochs_setup(void); + +sos_ret_t sos_bochs_putchar(char c); + +sos_ret_t sos_bochs_putstring(const char* str); + +/** Print the least signficant 32 (nbytes == 4), 24 (nbytes == 3), 16 + (nbytes == 2) or 8 (nbytes == 1) bits of val in hexadecimal. */ +sos_ret_t sos_bochs_puthex(unsigned val, int nbytes); + +/** hexdump-style pretty printing */ +sos_ret_t sos_bochs_hexdump(const void* addr, int nbytes); + +/** + * Print the formatted string. Very restricted version of printf(3): + * 1/ can print max 255 chars, 2/ supports only %d/%i, %c, %s, %x + * without any support for flag charachters (eg %08x). + */ +sos_ret_t sos_bochs_printf(const char *format, /* args */...) + __attribute__ ((format (printf, 1, 2))); + +#endif diff --git a/sos-code-article6.5/drivers/x86_videomem.c b/sos-code-article6.5/drivers/x86_videomem.c new file mode 100644 index 0000000..9b9aafc --- /dev/null +++ b/sos-code-article6.5/drivers/x86_videomem.c @@ -0,0 +1,127 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include <sos/klibc.h> +#include <hwcore/ioports.h> + +#include "x86_videomem.h" + +/* The text video memory starts at address 0xB8000. Odd bytes are the + ASCII value of the character, even bytes are attribute for the + preceding character. */ +#define VIDEO 0xb8000 + + +/* Console screen size */ +#define LINES 25 +#define COLUMNS 80 + + +/** The structure of a character element in the video memory. @see + http://webster.cs.ucr.edu/AoA DOS edition chapter 23 */ +typedef struct { + unsigned char character; + unsigned char attribute; +} __attribute__ ((packed)) x86_video_mem[LINES*COLUMNS]; + + + +/** The base pointer for the video memory */ +static volatile x86_video_mem *video = (volatile x86_video_mem*)VIDEO; + +sos_ret_t sos_x86_videomem_setup(void) +{ + /* + * Hide cursor. @see Ralf Brown's interrupt (and port) list + * http://www-2.cs.cmu.edu/~ralf/files.html + */ +#define CRT_REG_INDEX 0x3d4 +#define CRT_REG_DATA 0x3d5 + + /* CRT index port => ask for access to register 0xa ("cursor + start") */ + outb(0x0a, CRT_REG_INDEX); + + /* (RBIL Tables 708 & 654) CRT Register 0xa => bit 5 = cursor OFF */ + outb(1 << 5, CRT_REG_DATA); + + return SOS_OK; +} + + +sos_ret_t sos_x86_videomem_cls(unsigned char attribute) +{ + /* Clears the screen */ + int i; + for(i = 0 ; i < LINES*COLUMNS ; i++) + { + (*video)[i].character = 0; + (*video)[i].attribute = attribute; + } + + return SOS_OK; +} + + +sos_ret_t sos_x86_videomem_putstring(unsigned char row, unsigned char col, + unsigned char attribute, + const char *str) +{ + unsigned video_offs = row*COLUMNS + col; + + if (video_offs >= LINES*COLUMNS) + return -SOS_EINVAL; + + for ( ; str && *str && (video_offs < LINES*COLUMNS) ; str++, video_offs++) + { + (*video)[video_offs].character = (unsigned char)*str; + (*video)[video_offs].attribute = attribute; + } + + return SOS_OK; +} + + +sos_ret_t sos_x86_videomem_putchar(unsigned char row, unsigned char col, + unsigned char attribute, + unsigned char c) +{ + unsigned video_offs = row*COLUMNS + col; + + if (video_offs >= LINES*COLUMNS) + return -SOS_EINVAL; + + (*video)[video_offs].character = c; + (*video)[video_offs].attribute = attribute; + + return SOS_OK; +} + + +sos_ret_t sos_x86_videomem_printf(unsigned char row, unsigned char col, + unsigned char attribute, + const char *format, /* args */...) +{ + char buff[256]; + va_list ap; + + va_start(ap, format); + vsnprintf(buff, sizeof(buff), format, ap); + va_end(ap); + + return sos_x86_videomem_putstring(row, col, attribute, buff); +} diff --git a/sos-code-article6.5/drivers/x86_videomem.h b/sos-code-article6.5/drivers/x86_videomem.h new file mode 100644 index 0000000..e4b2f91 --- /dev/null +++ b/sos-code-article6.5/drivers/x86_videomem.h @@ -0,0 +1,97 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_X86_VIDEOMEM_H_ +#define _SOS_X86_VIDEOMEM_H_ + +/** + * @file x86_videomem.h + * + * On x86 PC platforms, the text mode screen memory (and CGA/EGA/VGA + * too) is mapped into physical memory. This file handles access to + * this screen, supposed to be set in text-mode, through this memory + * area. All the functions below print the characters directly to the + * memory, without interpreting the escaped characters (such as \n, + * \r...) + */ + +#include <sos/errno.h> + +/** + * x86 video attributes + * See http://webster.cs.ucr.edu/AoA/DOS/ch23/CH23-1.html + */ +/* Normal and Dark/Light foreground */ +#define SOS_X86_VIDEO_FG_BLACK 0 +#define SOS_X86_VIDEO_FG_DKGRAY 8 +#define SOS_X86_VIDEO_FG_BLUE 1 +#define SOS_X86_VIDEO_FG_LTBLUE 9 +#define SOS_X86_VIDEO_FG_GREEN 2 +#define SOS_X86_VIDEO_FG_LTGREEN 10 +#define SOS_X86_VIDEO_FG_CYAN 3 +#define SOS_X86_VIDEO_FG_LTCYAN 11 +#define SOS_X86_VIDEO_FG_RED 4 +#define SOS_X86_VIDEO_FG_LTRED 12 +#define SOS_X86_VIDEO_FG_MAGENTA 5 +#define SOS_X86_VIDEO_FG_LTMAGENTA 13 +#define SOS_X86_VIDEO_FG_BROWN 6 +#define SOS_X86_VIDEO_FG_YELLOW 14 +#define SOS_X86_VIDEO_FG_LTGRAY 7 +#define SOS_X86_VIDEO_FG_WHITE 15 +/* Background */ +#define SOS_X86_VIDEO_BG_BLACK (0 << 4) +#define SOS_X86_VIDEO_BG_BLUE (1 << 4) +#define SOS_X86_VIDEO_BG_GREEN (2 << 4) +#define SOS_X86_VIDEO_BG_CYAN (3 << 4) +#define SOS_X86_VIDEO_BG_RED (4 << 4) +#define SOS_X86_VIDEO_BG_MAGENTA (5 << 4) +#define SOS_X86_VIDEO_BG_BROWN (6 << 4) +#define SOS_X86_VIDEO_BG_LTGRAY (7 << 4) +/* Blinking */ +#define SOS_X86_VIDEO_FG_BLINKING (1 << 7) + + +/** Setup the video RAM mapping and clear the screen */ +sos_ret_t sos_x86_videomem_setup(void); + +/** Clears the screen and set the background color as given by + attribute */ +sos_ret_t sos_x86_videomem_cls(unsigned char attribute); + +/** Print the string on the scren with the given attribute. Does not + handle scrolling */ +sos_ret_t sos_x86_videomem_putstring(unsigned char row, unsigned char col, + unsigned char attribute, + const char *str); + +/** Print the character on the scren with the given attribute. Does not + handle scrolling */ +sos_ret_t sos_x86_videomem_putchar(unsigned char row, unsigned char col, + unsigned char attribute, + unsigned char c); + +/** + * Print the formatted string. Very restricted version of printf(3): + * 1/ can print max 255 chars, 2/ supports only %d/%i, %c, %s, %x + * without any support for flag charachters (eg %08x). + */ +sos_ret_t sos_x86_videomem_printf(unsigned char row, unsigned char col, + unsigned char attribute, + const char *format, /* args */...) + __attribute__ ((format (printf, 4, 5))); + +#endif /* _SOS_X86_VIDEOMEM_H_ */ diff --git a/sos-code-article6.5/extra/Makefile b/sos-code-article6.5/extra/Makefile new file mode 100644 index 0000000..f858aa6 --- /dev/null +++ b/sos-code-article6.5/extra/Makefile @@ -0,0 +1,40 @@ +OBJCOPY=objcopy + +all: sos_qemu.img + +-include ../.mkvars + +# The image is the simple concatenation of the boot sector and the kernel +# It may be use in bochs or on a real floppy, but NOT in qemu (see below) +sos_bsect.img: bsect.bin sos.bin + cat $^ > $@ + @echo "[31mYou can use the $@ image in bochs or on a real floppy (NOT qemu)[m" + +# For qemu, the trick is to tell it we have *more* than 1440 sectors (720kB). +# Rtherwise the qemu disk geometry will be configured to be that of a 720kB +# floppy, while our boot sector assumes it to be 1.44MB +sos_qemu.img: sos_bsect.img + # Padding with 0s after the bsect/kernel image + cat $< /dev/zero | dd of=$@ bs=1k count=1440 + @echo "[31mYou can use the $@ image in qemu, bochs, or on a real floppy[m" + +# we extract the boot sector from the main ELF binary +bsect.bin: sos_bsect.elf + $(OBJCOPY) -v -O binary -j .bootsect $< $@ + +# we extract the kernel code from the main ELF binary +sos.bin: sos_bsect.elf + $(OBJCOPY) -v -O binary -R .bootsect $< $@ + +# The main ELF binary contains the boot sector and the kernel code +# linked together (hence we deal with a SINGLE image that we split +# above) because they share some symbol definitions +sos_bsect.elf: bootsect.o compile_kernel + $(LD) --warn-common -T ./sos_bsect.lds -o $@ \ + bootsect.o $(wildcard ../hwcore/*.o ../drivers/*.o ../sos/*.o) + +compile_kernel: + $(MAKE) -C .. + +clean: + $(RM) *.img *.elf *.bin *~ *.o *.out diff --git a/sos-code-article6.5/extra/README b/sos-code-article6.5/extra/README new file mode 100644 index 0000000..0272f1f --- /dev/null +++ b/sos-code-article6.5/extra/README @@ -0,0 +1,73 @@ + +Contents of the extra/ directory +================================ + +Data and configuration files to support generation of sos on non-x86 +and/or grub-less hosts: + - dot.mkvars: file to copy as .mkvars in the root directory to + compile on a non-x86 host, and to generate the grub floppy image on + a grub-less host + - grub.img.gz: compressed image of a Grub floppy (without any + kernel). Used by dot.mkvars. + - mtoolsrc: file needed by .mkvars to compile a the floppy image + +Support of a sos-specific boot sector: + - Makefile: rules to compile sos_bsect.img, the floppy image with the + boot sector and the Sos + - bootsect.S: x86 Sos boot sector (GNU as). Depends on sos_bsect.lds + - sos_bsect.lds: ld script to bind the boot sector with the remaining + of the kernel + +Misc: + - qemu-port-e9.diff: patch over qemu to support the bochs "port 0xe9 hack" + + +What you can do with these files +================================ + + +*** Compile SOS from another architecture: +------------------------------------------ + - compile a cross-compiler for the i586-gnu target. This involves + compiling the binutils and gcc. Here are example configuration + options for them: + binutils (replace sparc-cun-solaris with your arch): + ../binutils-2.13/configure --prefix=/udd/ddecotig/temp_dd/xgcc/host-sparc-solaris7/stow/binutils-2.11 --host=sparc-sun-solaris2.7 i586-gnu + make && make install + gcc (ditto): + CFLAGS="-O2 -Dinhibit_libc" ../gcc-3.2/configure --target=i586-gnu --prefix=/udd/ddecotig/temp_dd/xgcc/host-sparc-solaris7/stow/gcc-3.2 --with-as=/udd/ddecotig/temp_dd/xgcc/host-sparc-solaris7/bin/as --with-ld=/udd/ddecotig/temp_dd/xgcc/host-sparc-solaris7/bin/ld --with-gnu-as --with-gnu-ld --enable-languages=c --disable-shared --disable-multilib --disable-nls --enable-threads=single + make && make install + - compile the mtools + - copy dot.mkvars to the root directory of SOS, as ".mkvars" + - customize the CC/LD/... variables to suit your cross-compiler + installatioon + - now you may run make from the SOS root directory, it should + generate the Grub boot floppy image. The following warning is + normal: + .mkvars:16: attention : écrasement des commandes pour la cible « grub-sos.img » + Makefile:92: attention : anciennes commandes ignorées pour la cible « grub-sos.img » + + +*** To compile SOS from an x86 where grub is not or incorrectly installed: +-------------------------------------------------------------------------- + - copy dot.mkvars to the root directory of SOS, as ".mkvars" + - customize the CC/LD/... variables to suit your cross-compiler + installatioon + - now you may run make from the SOS root directory, it should + generate the Grub boot floppy image. The following warning is + normal: + .mkvars:16: attention : écrasement des commandes pour la cible « grub-sos.img » + Makefile:92: attention : anciennes commandes ignorées pour la cible « grub-sos.img » + + +*** To compile SOS with its own bootloader: +------------------------------------------- + - for cross-architecture compilation: see above + - cd to this extra/ directory + - run 'make' + - the floppy image is: sos_bsect.img + NOTE : SOS will not boot correctly this way after article 2 ! + + +-- +David Decotigny diff --git a/sos-code-article6.5/extra/bootsect.S b/sos-code-article6.5/extra/bootsect.S new file mode 100644 index 0000000..8d6ef8f --- /dev/null +++ b/sos-code-article6.5/extra/bootsect.S @@ -0,0 +1,406 @@ + +/* + * @(#) $Id: bootsect.S,v 1.8 2004/11/20 16:00:11 d2 Exp $ + * Description : Bootsecteur en syntaxe AT&T + * Auteurs : Thomas Petazzoni & Fabrice Gautier & Emmanuel Marty + * Jerome Petazzoni & Bernard Cassagne & coffeeman + * David Decotigny + * Bug reports to kos-misc@enix.org + */ + +/* + * But global de ce bootsecteur : + * + * - Initialiser la becane + * - Charger le kernel + * - Passer en mode protege + * - Executer le kernel + * + * Taille restante : Je vous rappelle qu'un bootsecteur ne peut faire + * qu'au maximum 512 octets dont 2 octets obligatoires 0xAA55. Sur + * les 510 octets reellement utilisables, il reste 3 octets dispo (60 + * si on decide d'enlever le BPB un jour) !!! + * + * thomas_petazzoni : - detection des codes d'erreurs de chargement + * David_Decotigny : - Passage en GNU as + * David_Decotigny : - Chargement du noyau au-dela du 1er Mega (taille + * max = 0x9e000 octets = 632ko), pour avoir le + * meme noyau sous grub et avec le bootsecteur + */ + + /* + * Sequence d'operations : + * - Le BIOS charge le bootsect en 0x7c00 (BOOT_ADRESS). On choisit + * la representation 0x7c0:0000 pour que le .org 0 reste valide + * - Le bootsect se deplace de lui-meme en 0x9f000 (COPY_ADRESS). On + * choisit la representation 0x9f00:0000 pour que le .org 0 reste + * valide + * - Le bootsect verifie que le processeur est du type 386+ + * - Il charge le noyau depuis la disquette en memoire a partir de + * 0x1000 (LOAD_ADRESS). Le noyau peut au max tenir sur + * SECTORS_TO_LOAD secteurs + * - Il passe en pmode flat (apres ouverture a20) + * - Il recopie le noyau (situe en LOAD_ADRESS) vers son adresse + * finale (FINAL_ADDRESS = 2Mo). La recopie se fait sur tout l'espace + * LOAD_ADRESS ---> COPY_ADRESS, c'est a dire sur 0x9e000 octets = + * 632ko. Le noyau peut donc au max faire 632ko. Le nombre max de + * secteurs de disquette qu'on peut charger est donc 1264 + */ + + +/* La taille de la pile */ +#define BOOT_STACK_SIZE 0x4000 + + .file "bootsect.S" + + /* Tout est place dans une seule section */ + .section ".bootsect" + + /* L'essentiel du bootsector (sauf les 1eres instructions) + sont a un offset 0. On fait en sorte que le compilo soit + d'accord la-dessus. Quand on a des adresse realm exotiques + (0x7c00, 0x9f000, ...), on s'arrange toujours pour avoir un + offset de 0 => on choisira le segment adapte (0x7c0, + 0x9f00, ...). Il ne faut pas oublier le ld -Ttext 0 */ + .org 0 + + /* Pour que gas genere du 16bits, afin que ca marche en realm */ + .code16 + +#define SECTORS_TO_LOAD 128 /* 64 ko */ /* MAX=1264 */ + +/* + * Parametres de la disquette. Comme c'est chiant de faire une + * procedure de detection auto, et que ca prend de la place, on fait + * ca "a la main". Par exemple, une DD 720 Ko a 9 secteurs/piste, une + * 1.44 Mo a 18 secteurs/pistes + */ +#define CYLS 80 +#define HEADS 1 +#define SECTS 18 + +#define BOOT_ADRESS 0x07C00 /* Adresse de demarrage (lineaire) */ +#define BOOT_SEG (BOOT_ADRESS>>4) /* Segment de Boot */ +#define BOOT_SIZE 512 /* Taille bu bootsecteur */ +#define COPY_ADRESS 0x9F000 /* La ou on va copier le + bootsecteur (lineaire) */ +#define COPY_SEG (COPY_ADRESS>>4) /* Segment de la ou on va + copier le bootsecteur */ +#define LOAD_ADRESS 0x01000 /* 1er chargement du systeme */ +#define LOAD_SEG (LOAD_ADRESS>>4) /* Segment du 1er chargement du */ +#define MAX_KERN_LEN COPY_ADRESS-LOAD_ADRESS /* Taille noyau maxi */ + +/* IMPORTANT : Cette valeur DOIT etre identique a l'adresse presente + dans sos.lds ! */ +#define FINAL_ADDRESS 0x200000 /* Adresse finale (physique de 0 a 4G) + ou est charge le noyau */ + +#define OP16 .byte 0x66 ; +#define OP32 .byte 0x66 ; + +/* + * Procedure qui vide le buffer clavier. + */ +#define WAITKB \ + 1: ;\ + .word 0xeb ;\ + .word 0xeb ;\ + inb $0x64, %al ;\ + andb $0x2, %al ;\ + jnz 1b + + /* Le point d'entree dans le bootsect */ +.globl _bsect +_bsect: + + /* + * La portion qui suit est situee a un offset 0x7c00 en + * memoire. Attention donc aux references memoire dans cette + * partie. On choisit de rester en offset 0 (.org 0), mais on + * charge correctement les segments a 0x7c0. + */ + + movw $BOOT_SEG, %ax /* le bootsecteur est a 0x7C00 en lineaire */ + movw %ax, %ds /* on le copie a l'adresse COPY_ADRESS */ + xorw %si, %si /* comme cette adresse est la plus haute de la mem */ + xorw %di, %di /* on pourra charger un kernel + gros */ + movw $(BOOT_SIZE>>1), %cx + movw $COPY_SEG, %ax + movw %ax, %es + cld + rep ; movsw + + /* on continue a executer le bootsecteur, mais maintenant a + partir de 0x9F000, qu'on represente sous la forme + 0x9f00:offset */ + ljmp $COPY_SEG, $here + + /* + * A partir de maintenant, on est a un offset 0 en memoire + * (segment 0x9f00), conformement a ce que veut le compilo. + */ +here: + movw %ax, %ds + + /* Petite pile temporaire (1k - 3.84k en RAM ; les adresses 0-1k + correspondent au vecteur d'interruptions). */ + movw %ax, %ss + movw $(LOAD_ADRESS - 0x10), %sp + + /* Efface l'ecran */ + movb $0x0, %ah + movb $0x3, %al + int $0x10 + + /* Affiche les messages d'attente */ + movw $loadkern, %si + call message + movw $check, %si + call message + +check386: + /* + * la attention, plus complexe : on teste si le proc est un + * 386+ pour cela, on va essayer de modifier les bits 12 ? 14 + * du registre E-flag si la modification reste, alors le proc + * est un 386+, sinon, c'est =< 286 + * + * Merci a Emmanuel Marty pour la compatibilite avec les 386 + * "pre-jurassique" + */ + + pushf /* on sauvegarde le E-Flag */ + movb $0x70, %ah + pushw %ax + popf + pushf + popw %ax + orb %ah, %ah + je no386 /* si la modif n'est pas valable, alors on saute a + no386 */ + popf /* on les restaure ? la fin ... */ + + /* Message de confirmation de 386+ et d'attente */ + movw $found386, %si + call message + movw $loading, %si + call message + +/* Copie du noyau disquette => RAM a partir de 0x1000 + L'adresse de destination est définie par es:0, où es vaut + initialement 0x100 (ie correspond alors à l'adresse 256*16, soit 4 + ko). Chaque itération incrémente ce registre es de 32, ce qui + correspond à un bond de 32*16 en mémoire, soit la taille d'un + secteur. De cette façon, puisqu'on joue sur les segments plutôt que + sur les offsets, la taille du noyau n'est pas limitée à 64 ko. Elle + est limitée par contre à la taille de la mémoire disponible sous + les 1Mo, \ie 640 ko (0x9f000 - 0x1000). */ +copyKernel: + /* Chargement du noyau en LOAD_SEG:0 */ + /* 3 iterateurs : + - load_size : le nbre de secteurs a charger + - cl : le secteur ou on en est pour le + cylindre en cours (<= SECTS) + - dh : la tete en cours (0/1) + */ + movb $0, %dl + movw $LOAD_SEG, %ax + movw %ax, %es + + xorw %bx, %bx + xorw %dx, %dx + movw $1, %cx /* premier secteur */ + +.nextsector: /* prochain secteur */ + incb %cl /* en incrementant CL */ + cmpb $SECTS, %cl /* si CL =< SECTS (=nbre de secteurs/pistes) + alors on charge */ + jbe .sector + movb $1, %cl /* sinon on revient au secteur 1 */ + incb %dh /* mais sur l'autre tete */ + cmpb $1, %dh /* on recompare, si DH =< 1 */ + je .sector /* on charge */ + movb $0, %dh /* sinon on repasse a la tete 0 */ + incb %ch /* mais on change de cylindre */ + +.sector: + pushw %es + movw $0x0201, %ax /* service 0x2, chargement 0x1 seecteur */ + int $0x13 /* Go ! */ + jc halt /* erreur */ + popw %ax + addw $32, %ax /* on a charge un secteur, donc on doit + charger 512 bytes plus loin */ + movw %ax, %es /* on avance donc le segment du buffer de + 32bytes, ie 1 secteur en RAM (car 32*16=512) */ + + movw $(0x0E*256+'.'), %ax /* affiche un point */ + int $0x10 + + decw (load_size) /* et on repart pour le prochain secteur + tant qu'on n'a pas fini ! */ + jnz .nextsector + +after: + movw $0x03f2, %dx + inb %dx, %al /* stoppe le moteur */ + andb $0x0f, %al + outb %al, %dx + + cli /* on interdit les interruptions */ + +fincopie: + pushw %cs + popw %ds + + /* on ouvre la porte A20 */ + WAITKB /* on vide le buffer */ + movb $0xd1, %al /* on met a jour le port */ + outb %al, $0x64 + WAITKB + movb $0xdf, %al /* bit 2 = ouverture/fermeture */ + outb %al, $0x60 + + /* + * init gdt + */ +InitGDT: + /* Préparation du flat mode */ + lgdt gdtr + +GoPMode: + /* Passage en mode protégé */ + movl %cr0, %eax + orb $1, %al /* set PE bit to 1 */ + movl %eax, %cr0 + + /* we are not yet in Pmode jump 'in' pmode clearing prefetch + * queue and loading a new selector */ + movw $0x10, %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs + +/* + * Code 32 bits ============================================================ + */ + .code32 + +JumpToHere32: /* Se deplace a l'endroit actuel, en passant en 32bits + et en utilisant la gdt, et vide la prefetch queue */ + .byte 0x66 /* Prefixe 32bits : en realite, jusqu'au jmp, on est + encore en 16 bits */ + ljmp $0x8, $(COPY_ADRESS+(Here32)) +Here32: + /* Et voila : On est en 32 bits vrai */ + +MoveKernelToFinalAddr: /* Deplace le noyau (en LOAD_ADDRESS) vers sa + destination finale (FINAL_ADDRESS) */ + movl $0x10, %eax + movl %eax, %ds /* Seg Src = DSeg */ + movl %eax, %es /* Sed Dest = DSeg */ + cld + movl $LOAD_ADRESS, %esi /* On commence la copie au debut du noyau */ + movl $FINAL_ADDRESS, %edi /* On copie vers cette adresse */ + movl $MAX_KERN_LEN, %ecx /* Taille recopie */ + shrl $2, %ecx + rep + movsl + +LaunchKernel: + /* Met en place une pile au niveau du symbole "stack" */ + movl %eax, %ss + movl $(stack + BOOT_STACK_SIZE), %ebp + movl %ebp, %esp + + /* Saut vers le noyau. La GDT est en place (flat mode), les + * selecteurs aussi, a20 est ouverte, et les interruptions sont + * cli + pas de idt. Le PIC n'est pas programme */ + ljmp $0x8, $sos_main + +/* + * Utilities ============================================================ + */ + .code16 + +message: + lodsb /* charge ds:si dans al et incremente si */ + orb %al, %al /* si al = 0 */ + jz 1f + movb $0x0e, %ah /* service 0Eh (affichage d'un caractere) */ + movw $0x0007, %bx /* Parametres : blanc sur fond noir */ + int $0x10 /* Appel de l'interruption 10h */ + jmp message /* On repart au début ... */ + 1: ret /* si la chaine est finie alors on retourne + dans la fonction appelante */ + +halt: + pushw %cs + popw %es + movw $haltmsg, %si + call message + cli + 1: jmp 1b + ret + +no386: + movw $need386, %si + call message + call halt + + /* + * GDT + */ + +gdt: +gdtr: +NULL_Desc: + .word (EndGDT)-(gdt)-1 /* Taille GDT */ + .long (gdt)+COPY_ADRESS +unused: + .word 0 + +CS_Desc: /* 0x8 */ + .word 0xFFFF, 0 + .byte 0, 0x9B, 0xCF, 0 + +DS_Desc: /* 0x10 */ + .word 0xFFFF, 0 + .byte 0, 0x93, 0xCF, 0 + +EndGDT: + + /* quelques messages */ + +loadkern: .string "-= S O S =- : The Simple Operating System \r\n" +check: .string "Checking for a 386+ processor... " +found386: .string " [OK]\r\n" +need386: .string " [FAILED]\r\n" +diskerror: .string "Disk Error\r\n" +loading: .string "Loading... " +haltmsg: .string "System Halted\r\n" + +/*** Les code/données du boot secteur se terminent ICI. le marqueur de + * fin (aa55) est ajouté automatiquement par le script ld + * sos_bsect.lds ***/ + +/* La pile de 16k qu'on utilise au niveau de LaunchKernel se trouve + declaree avec le noyau, dans sa section ".init_stack", cad HORS du boot + secteur ! (sinon ca depasserait 512B, forcément). On aurait pu la + définir directement dans le sos_bsect.lds, ou dans un fichier .c + auxiliaire pour plus de clarté */ +/* Here is the stack */ +.section ".init_stack", "aw", @nobits +.p2align 4 +.size stack, BOOT_STACK_SIZE +stack: + .space BOOT_STACK_SIZE + +/* Some data characterizing the stack addresses */ +.data + .globl bootstrap_stack_bottom +bootstrap_stack_bottom: .long stack + + .globl bootstrap_stack_size +bootstrap_stack_size: .long BOOT_STACK_SIZE diff --git a/sos-code-article6.5/extra/dot.mkvars b/sos-code-article6.5/extra/dot.mkvars new file mode 100644 index 0000000..1f7dca5 --- /dev/null +++ b/sos-code-article6.5/extra/dot.mkvars @@ -0,0 +1,29 @@ +# For cross-compilation and/or installations without grub available, +# copy this file as .mkvars to the root directory of the SOS sources, +# and customize the CC/LD/... variables. You still need the mtools +# installed and running + +CC := i586-gnu-gcc +LD := i586-gnu-ld +OBJCOPY := i586-gnu-objcopy +CFLAGS += -O3 + +# Configuration of mtools +MTOOLSRC = extra/mtoolsrc +export MTOOLSRC + +$(MULTIBOOT_IMAGE): $(KERNEL_OBJ) menu.txt + gzip -dc < extra/grub.img.gz > $@ + mcopy menu.txt v:/boot/grub/ + mmd v:/system + mcopy sos.elf v:/system/sos.elf + +menu.txt: + echo timeout 0 > $@ + echo default 0 >> $@ + echo title SOS >> $@ + echo "root (fd0)" >> $@ + echo kernel /system/sos.elf >> $@ + +runbochs: all + echo c | bochs -q diff --git a/sos-code-article6.5/extra/grub.img.gz b/sos-code-article6.5/extra/grub.img.gz Binary files differnew file mode 100644 index 0000000..4f98e74 --- /dev/null +++ b/sos-code-article6.5/extra/grub.img.gz diff --git a/sos-code-article6.5/extra/mtoolsrc b/sos-code-article6.5/extra/mtoolsrc new file mode 100644 index 0000000..df1a26e --- /dev/null +++ b/sos-code-article6.5/extra/mtoolsrc @@ -0,0 +1,2 @@ +# For older versions of mtools, you may have to remove "filter" +drive v: file="fd.img" 1.44M filter diff --git a/sos-code-article6.5/extra/qemu-port-e9.diff b/sos-code-article6.5/extra/qemu-port-e9.diff new file mode 100644 index 0000000..d8be044 --- /dev/null +++ b/sos-code-article6.5/extra/qemu-port-e9.diff @@ -0,0 +1,73 @@ +--- Makefile.target 17 Mar 2004 23:46:04 -0000 1.19 ++++ Makefile.target 18 Mar 2004 14:20:29 -0000 +@@ -217,7 +217,8 @@ + # must use static linking to avoid leaving stuff in virtual address space + VL_OBJS=vl.o osdep.o block.o monitor.o \ + ide.o ne2000.o pckbd.o vga.o sb16.o dma.o oss.o \ +- fdc.o mc146818rtc.o serial.o i8259.o i8254.o pc.o ++ fdc.o mc146818rtc.o serial.o i8259.o i8254.o pc.o \ ++ port-e9.o + ifeq ($(TARGET_ARCH), ppc) + VL_OBJS+= hw.o + endif +--- hw/pc.c 14 Mar 2004 21:46:48 -0000 1.2 ++++ hw/pc.c 18 Mar 2004 14:20:29 -0000 +@@ -371,6 +371,7 @@ + SB16_init(); + + fdctrl_init(6, 2, 0, 0x3f0, fd_table); ++ port_e9_init(); + + cmos_init(ram_size, boot_device); + } +--- /dev/null 2003-01-30 11:24:37.000000000 +0100 ++++ port-e9.c 2004-03-18 15:18:52.660493187 +0100 +@@ -0,0 +1,38 @@ ++/* ++ * QEMU Port 0xe9 hack ++ * ++ * Copyright (c) 2000-2004 E. Marty, the bochs team, D. Decotigny ++ * ++ * Permission is hereby granted, free of charge, to any person obtaining a copy ++ * of this software and associated documentation files (the "Software"), to deal ++ * in the Software without restriction, including without limitation the rights ++ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ++ * copies of the Software, and to permit persons to whom the Software is ++ * furnished to do so, subject to the following conditions: ++ * ++ * The above copyright notice and this permission notice shall be included in ++ * all copies or substantial portions of the Software. ++ * ++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ++ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ++ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN ++ * THE SOFTWARE. ++ */ ++#include <stdio.h> ++#include <unistd.h> ++#include <inttypes.h> ++ ++#include "vl.h" ++ ++static void bochs_e9_write(void *opaque, uint32_t address, uint32_t data) ++{ ++ write(fileno(stdout), &data, 1); ++} ++ ++void port_e9_init () ++{ ++ register_ioport_write(0xe9, 1, 1, bochs_e9_write, NULL); ++} +--- vl.h 17 Mar 2004 23:17:16 -0000 1.14 ++++ vl.h 18 Mar 2004 14:29:06 -0000 +@@ -268,4 +268,7 @@ + void term_flush(void); + void term_print_help(void); + ++/* port-e9.c */ ++void port_e9_init(void); ++ + #endif /* VL_H */ diff --git a/sos-code-article6.5/extra/sos_bsect.lds b/sos-code-article6.5/extra/sos_bsect.lds new file mode 100644 index 0000000..ac42f23 --- /dev/null +++ b/sos-code-article6.5/extra/sos_bsect.lds @@ -0,0 +1,61 @@ +/* Copyright (C) 2004, David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +SECTIONS +{ + /* *********************************************** + * The bootsector is here. We link it against the remaining of the kernel + * in order to automatically figure out its size that must be loaded + * from file to memory (see the load_size definition below) + */ + + /* If we use one, we put the boot sector here. We don't set its + * address to 0x7c000 (aka 0x7c00:0), since it reloads itself to + * 0x9f000, causing the 0x7c000 address to be meaningless too. So we + * chose to pretend that the address is 0x0, and to make a little + * address arithmetic in bootsect.S */ + .bootsect 0x0 : + { + /* The code for the boot sector goes here */ + *(.bootsect); + + /* The load_size symbol contains the size of the area (in + * sectors, aka 512 Bytes) that the boot sector should copy from + * the disk. The bss section is not included since it uses 0 + * bytes on disk */ + load_size = .; + LONG((__e_load - __b_load + 511) >> 9); + /* ---> This is equivalent to ceil( (__e_load - __b_load) / 512 ) */ + + /* At offsets 511 and 512, we set the boot sector signature (AA55h) */ + . = 0x1fe; + SHORT(0xAA55); + } +} + + +/* This is to avoid a cut/paste here. Please notice that a multiboot + * section WILL be inserted, which is NOT mandatory (we could have + * removed it without getting into trouble). Please note however that + * the *.bin files will NOT be multiboot compatible (they are not in ELF + * format): they are expected to be directly booted by the BIOS (or + * by the "chainloader" command of Grub). */ +INCLUDE ../support/sos.lds + +/* We overload the entry set in sos.lds, just to avoid an ld warning */ +ENTRY(sos_main); diff --git a/sos-code-article6.5/hwcore/cpu_context.c b/sos-code-article6.5/hwcore/cpu_context.c new file mode 100644 index 0000000..770452d --- /dev/null +++ b/sos-code-article6.5/hwcore/cpu_context.c @@ -0,0 +1,407 @@ +/* Copyright (C) 2005 David Decotigny + Copyright (C) 2000-2004, The KOS team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + + +#include <sos/assert.h> +#include <sos/klibc.h> +#include <drivers/bochs.h> +#include <drivers/x86_videomem.h> +#include <hwcore/segment.h> + +#include "cpu_context.h" + + +/** + * Here is the definition of a CPU context for IA32 processors. This + * is a SOS convention, not a specification given by the IA32 + * spec. However there is a strong constraint related to the x86 + * interrupt handling specification: the top of the stack MUST be + * compatible with the 'iret' instruction, ie there must be the + * err_code (might be 0), eip, cs and eflags of the destination + * context in that order (see Intel x86 specs vol 3, figure 5-4). + * + * @note IMPORTANT: This definition MUST be consistent with the way + * the registers are stored on the stack in + * irq_wrappers.S/exception_wrappers.S !!! Hence the constraint above. + */ +struct sos_cpu_state { + /* (Lower addresses) */ + + /* These are SOS convention */ + sos_ui16_t gs; + sos_ui16_t fs; + sos_ui16_t es; + sos_ui16_t ds; + sos_ui16_t cpl0_ss; /* This is ALWAYS the Stack Segment of the + Kernel context (CPL0) of the interrupted + thread, even for a user thread */ + sos_ui16_t alignment_padding; /* unused */ + sos_ui32_t eax; + sos_ui32_t ebx; + sos_ui32_t ecx; + sos_ui32_t edx; + sos_ui32_t esi; + sos_ui32_t edi; + sos_ui32_t ebp; + + /* MUST NEVER CHANGE (dependent on the IA32 iret instruction) */ + sos_ui32_t error_code; + sos_vaddr_t eip; + sos_ui32_t cs; /* 32bits according to the specs ! However, the CS + register is really 16bits long */ + sos_ui32_t eflags; + + /* (Higher addresses) */ +} __attribute__((packed)); + + +/** + * The CS value pushed on the stack by the CPU upon interrupt, and + * needed by the iret instruction, is 32bits long while the real CPU + * CS register is 16bits only: this macro simply retrieves the CPU + * "CS" register value from the CS value pushed on the stack by the + * CPU upon interrupt. + * + * The remaining 16bits pushed by the CPU should be considered + * "reserved" and architecture dependent. IMHO, the specs don't say + * anything about them. Considering that some architectures generate + * non-zero values for these 16bits (at least Cyrix), we'd better + * ignore them. + */ +#define GET_CPU_CS_REGISTER_VALUE(pushed_ui32_cs_value) \ + ( (pushed_ui32_cs_value) & 0xffff ) + + +/** + * Structure of an interrupted Kernel thread's context + */ +struct sos_cpu_kstate +{ + struct sos_cpu_state regs; +} __attribute__((packed)); + + +/** + * THE main operation of a kernel thread. This routine calls the + * kernel thread function start_func and calls exit_func when + * start_func returns. + */ +static void core_routine (sos_cpu_kstate_function_arg1_t *start_func, + sos_ui32_t start_arg, + sos_cpu_kstate_function_arg1_t *exit_func, + sos_ui32_t exit_arg) + __attribute__((noreturn)); + +static void core_routine (sos_cpu_kstate_function_arg1_t *start_func, + sos_ui32_t start_arg, + sos_cpu_kstate_function_arg1_t *exit_func, + sos_ui32_t exit_arg) +{ + start_func(start_arg); + exit_func(exit_arg); + + SOS_ASSERT_FATAL(! "The exit function of the thread should NOT return !"); + for(;;); +} + + +sos_ret_t sos_cpu_kstate_init(struct sos_cpu_state **ctxt, + sos_cpu_kstate_function_arg1_t *start_func, + sos_ui32_t start_arg, + sos_vaddr_t stack_bottom, + sos_size_t stack_size, + sos_cpu_kstate_function_arg1_t *exit_func, + sos_ui32_t exit_arg) +{ + /* We are initializing a Kernel thread's context */ + struct sos_cpu_kstate *kctxt; + + /* This is a critical internal function, so that it is assumed that + the caller knows what he does: we legitimally assume that values + for ctxt, start_func, stack_* and exit_func are allways VALID ! */ + + /* Setup the stack. + * + * On x86, the stack goes downward. Each frame is configured this + * way (higher addresses first): + * + * - (optional unused space. As of gcc 3.3, this space is 24 bytes) + * - arg n + * - arg n-1 + * - ... + * - arg 1 + * - return instruction address: The address the function returns to + * once finished + * - local variables + * + * The remaining of the code should be read from the end upward to + * understand how the processor will handle it. + */ + + sos_vaddr_t tmp_vaddr = stack_bottom + stack_size; + sos_ui32_t *stack = (sos_ui32_t*)tmp_vaddr; + + /* If needed, poison the stack */ +#ifdef SOS_CPU_STATE_DETECT_UNINIT_KERNEL_VARS + memset((void*)stack_bottom, SOS_CPU_STATE_STACK_POISON, stack_size); +#elif defined(SOS_CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW) + sos_cpu_state_prepare_detect_kernel_stack_overflow(stack_bottom, stack_size); +#endif + + /* Simulate a call to the core_routine() function: prepare its + arguments */ + *(--stack) = exit_arg; + *(--stack) = (sos_ui32_t)exit_func; + *(--stack) = start_arg; + *(--stack) = (sos_ui32_t)start_func; + *(--stack) = 0; /* Return address of core_routine => force page fault */ + + /* + * Setup the initial context structure, so that the CPU will execute + * the function core_routine() once this new context has been + * restored on CPU + */ + + /* Compute the base address of the structure, which must be located + below the previous elements */ + tmp_vaddr = ((sos_vaddr_t)stack) - sizeof(struct sos_cpu_kstate); + kctxt = (struct sos_cpu_kstate*)tmp_vaddr; + + /* Initialize the CPU context structure */ + memset(kctxt, 0x0, sizeof(struct sos_cpu_kstate)); + + /* Tell the CPU context structure that the first instruction to + execute will be that of the core_routine() function */ + kctxt->regs.eip = (sos_ui32_t)core_routine; + + /* Setup the segment registers */ + kctxt->regs.cs + = SOS_BUILD_SEGMENT_REG_VALUE(0, FALSE, SOS_SEG_KCODE); /* Code */ + kctxt->regs.ds + = SOS_BUILD_SEGMENT_REG_VALUE(0, FALSE, SOS_SEG_KDATA); /* Data */ + kctxt->regs.es + = SOS_BUILD_SEGMENT_REG_VALUE(0, FALSE, SOS_SEG_KDATA); /* Data */ + kctxt->regs.cpl0_ss + = SOS_BUILD_SEGMENT_REG_VALUE(0, FALSE, SOS_SEG_KDATA); /* Stack */ + /* fs and gs unused for the moment. */ + + /* The newly created context is initially interruptible */ + kctxt->regs.eflags = (1 << 9); /* set IF bit */ + + /* Finally, update the generic kernel/user thread context */ + *ctxt = (struct sos_cpu_state*) kctxt; + + return SOS_OK; +} + + +#if defined(SOS_CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW) +void +sos_cpu_state_prepare_detect_kernel_stack_overflow(const struct sos_cpu_state *ctxt, + sos_vaddr_t stack_bottom, + sos_size_t stack_size) +{ + sos_size_t poison_size = SOS_CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW; + if (poison_size > stack_size) + poison_size = stack_size; + + memset((void*)stack_bottom, SOS_CPU_STATE_STACK_POISON, poison_size); +} + + +void +sos_cpu_state_detect_kernel_stack_overflow(const struct sos_cpu_state *ctxt, + sos_vaddr_t stack_bottom, + sos_size_t stack_size) +{ + unsigned char *c; + int i; + + /* On SOS, "ctxt" corresponds to the address of the esp register of + the saved context in Kernel mode (always, even for the interrupted + context of a user thread). Here we make sure that this stack + pointer is within the allowed stack area */ + SOS_ASSERT_FATAL(((sos_vaddr_t)ctxt) >= stack_bottom); + SOS_ASSERT_FATAL(((sos_vaddr_t)ctxt) + sizeof(struct sos_cpu_kstate) + <= stack_bottom + stack_size); + + /* Check that the bottom of the stack has not been altered */ + for (c = (unsigned char*) stack_bottom, i = 0 ; + (i < SOS_CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW) && (i < stack_size) ; + c++, i++) + { + SOS_ASSERT_FATAL(SOS_CPU_STATE_STACK_POISON == *c); + } +} +#endif + + +/* ======================================================================= + * Public Accessor functions + */ + + +sos_vaddr_t sos_cpu_context_get_PC(const struct sos_cpu_state *ctxt) +{ + SOS_ASSERT_FATAL(NULL != ctxt); + + /* This is the PC of the interrupted context (ie kernel or user + context). */ + return ctxt->eip; +} + + +sos_vaddr_t sos_cpu_context_get_SP(const struct sos_cpu_state *ctxt) +{ + SOS_ASSERT_FATAL(NULL != ctxt); + + /* On SOS, "ctxt" corresponds to the address of the esp register of + the saved context in Kernel mode (always, even for the interrupted + context of a user thread). */ + return (sos_vaddr_t)ctxt; +} + + +void sos_cpu_context_dump(const struct sos_cpu_state *ctxt) +{ + char buf[128]; + snprintf(buf, sizeof(buf), + "CPU: eip=%x esp=%x eflags=%x cs=%x ds=%x ss=%x err=%x", + (unsigned)ctxt->eip, (unsigned)ctxt, (unsigned)ctxt->eflags, + (unsigned)GET_CPU_CS_REGISTER_VALUE(ctxt->cs), (unsigned)ctxt->ds, + (unsigned)ctxt->cpl0_ss, + (unsigned)ctxt->error_code); + sos_bochs_putstring(buf); sos_bochs_putstring("\n"); + sos_x86_videomem_putstring(23, 0, + SOS_X86_VIDEO_FG_BLACK | SOS_X86_VIDEO_BG_LTGRAY, + buf); +} + + +/* ======================================================================= + * Public Accessor functions TO BE USED ONLY BY Exception handlers + */ + + +sos_ui32_t sos_cpu_context_get_EX_info(const struct sos_cpu_state *ctxt) +{ + SOS_ASSERT_FATAL(NULL != ctxt); + return ctxt->error_code; +} + + +sos_vaddr_t +sos_cpu_context_get_EX_faulting_vaddr(const struct sos_cpu_state *ctxt) +{ + sos_ui32_t cr2; + + /* + * See Intel Vol 3 (section 5.14): the address of the faulting + * virtual address of a page fault is stored in the cr2 + * register. + * + * Actually, we do not store the cr2 register in a saved + * kernel thread's context. So we retrieve the cr2's value directly + * from the processor. The value we retrieve in an exception handler + * is actually the correct one because an exception is synchronous + * with the code causing the fault, and cannot be interrupted since + * the IDT entries in SOS are "interrupt gates" (ie IRQ are + * disabled). + */ + asm volatile ("movl %%cr2, %0" + :"=r"(cr2) + : ); + + return cr2; +} + + +/* ======================================================================= + * Backtrace facility. To be used for DEBUGging purpose ONLY. + */ + + +sos_ui32_t sos_backtrace(const struct sos_cpu_state *cpu_state, + sos_ui32_t max_depth, + sos_vaddr_t stack_bottom, + sos_size_t stack_size, + sos_backtrace_callback_t * backtracer, + void *custom_arg) +{ + int depth; + sos_vaddr_t callee_PC, caller_frame; + + /* + * Layout of a frame on the x86 (compiler=gcc): + * + * funcA calls funcB calls funcC + * + * .... + * funcB Argument 2 + * funcB Argument 1 + * funcA Return eip + * frameB: funcA ebp (ie previous stack frame) + * .... + * (funcB local variables) + * .... + * funcC Argument 2 + * funcC Argument 1 + * funcB Return eip + * frameC: funcB ebp (ie previous stack frame == A0) <---- a frame address + * .... + * (funcC local variables) + * .... + * + * The presence of "ebp" on the stack depends on 2 things: + * + the compiler is gcc + * + the source is compiled WITHOUT the -fomit-frame-pointer option + * In the absence of "ebp", chances are high that the value pushed + * at that address is outside the stack boundaries, meaning that the + * function will return -SOS_ENOSUP. + */ + + if (cpu_state) + { + callee_PC = cpu_state->eip; + caller_frame = cpu_state->ebp; + } + else + { + /* Skip the sos_backtrace() frame */ + callee_PC = (sos_vaddr_t)__builtin_return_address(0); + caller_frame = (sos_vaddr_t)__builtin_frame_address(1); + } + + for(depth=0 ; depth < max_depth ; depth ++) + { + /* Call the callback */ + backtracer(callee_PC, caller_frame + 8, depth, custom_arg); + + /* If the frame address is funky, don't go further */ + if ( (caller_frame < stack_bottom) + || (caller_frame + 4 >= stack_bottom + stack_size) ) + return depth; + + /* Go to caller frame */ + callee_PC = *((sos_vaddr_t*) (caller_frame + 4)); + caller_frame = *((sos_vaddr_t*) caller_frame); + } + + return depth; +} diff --git a/sos-code-article6.5/hwcore/cpu_context.h b/sos-code-article6.5/hwcore/cpu_context.h new file mode 100644 index 0000000..43b3718 --- /dev/null +++ b/sos-code-article6.5/hwcore/cpu_context.h @@ -0,0 +1,270 @@ +/* Copyright (C) 2005 David Decotigny + Copyright (C) 2000-2004, The KOS team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_CPUCTXT_H_ +#define _SOS_CPUCTXT_H_ + + +/** + * @file cpu_context.h + * + * Low level API to manage kernel and user thread CPU contexts. Should + * be some kind of architecture-independent. + */ + +#include <sos/types.h> +#include <sos/errno.h> + + +/** + * Opaque structure storing the CPU context of an inactive kernel or + * user thread, as saved by the low level primitives below or by the + * interrupt/exception handlers. + * + * @note This is an (architecture-independent) forward declaration: + * see cpu_context.c and the *.S files for its + * (architecture-dependent) definition. + */ +struct sos_cpu_state; + + +/** + * The type of the functions passed as arguments to the Kernel thread + * related functions. + */ +typedef void (sos_cpu_kstate_function_arg1_t(sos_ui32_t arg1)); + + +/** + * Function to create an initial context for a kernel thread starting + * its execution at function start_func with the argument initial_arg, + * and having the stack defined by stack_bottom/stack_size. When the + * start_func function returns, the function exit_func is called with + * argument exit_arg. + * + * @param kctxt The kernel thread CPU context to initialize. The + * address of the newly-initialized struct sos_cpu_state will be + * stored in this variable. The contents of this struct sos_cpu_state + * are actually located /inside/ the stack. + * + * @param start_func The address of the first instruction that will be + * executed when this context will be first transferred on + * CPU. Practically speaking, this is the address of a function that + * is assumed to take 1 argument. + * + * @param start_arg The value that will be passed as the argument to + * start_func when the thread starts. The stack will be setup + * accordingly to simulate a real call to the function and really + * passing this arguement. + * + * @param stack_bottom The lowest address of the stack. + * + * @param stack_size The size of the stack. + * + * @param exit_func The address of the instruction executed after the + * function start_func has returned. This function takes 1 parameter + * as argument: exit_arg. + * + * @param exit_arg The argument passed to the function exit_func. + * + * @note the newly created context is INTERRUPTIBLE by default ! + */ +sos_ret_t sos_cpu_kstate_init(struct sos_cpu_state **kctxt, + sos_cpu_kstate_function_arg1_t *start_func, + sos_ui32_t start_arg, + sos_vaddr_t stack_bottom, + sos_size_t stack_size, + sos_cpu_kstate_function_arg1_t *exit_func, + sos_ui32_t exit_arg); + + +/** + * Function that performs an immediate context-switch from one + * kernel/user thread to another one. It stores the current executing + * context in from_ctxt, and restores to_context on CPU. + * + * @param from_ctxt The address of the struct sos_cpu_state will be + * stored in this variable. Must NOT be NULL. + * + * @param to_ctxt The CPU will resume its execution with the struct + * sos_cpu_state located at this address. Must NOT be NULL. + */ +void sos_cpu_context_switch(struct sos_cpu_state **from_ctxt, + struct sos_cpu_state *to_ctxt); + + +/* + * Switch to the new given context (of a kernel/user thread) without + * saving the old context (of another kernel/user thread), and call + * the function reclaiming_func passing it the recalining_arg + * argument. The reclaining function is called from within the stack + * of the new context, so that it can (among other things) safely + * destroy the stack of the former context. + * + * @param switch_to_ctxt The context that will be restored on the CPU + * + * @param reclaiming_func The address of the function that will be + * called after having changed the stack, but before restoring the CPU + * context to switch_to_ctxt. + */ +void +sos_cpu_context_exit_to(struct sos_cpu_state *switch_to_ctxt, + sos_cpu_kstate_function_arg1_t *reclaiming_func, + sos_ui32_t reclaiming_arg) __attribute__((noreturn)); + +/* ======================================================================= + * Public Accessor functions + */ + + +/** + * Return Program Counter stored in the saved kernel/user context + */ +sos_vaddr_t sos_cpu_context_get_PC(const struct sos_cpu_state *ctxt); + + +/** + * Return Stack Pointer stored in the saved kernel/user context + */ +sos_vaddr_t sos_cpu_context_get_SP(const struct sos_cpu_state *ctxt); + + +/** + * Dump the contents of the CPU context (bochs + x86_videomem) + */ +void sos_cpu_context_dump(const struct sos_cpu_state *ctxt); + + +/* ======================================================================= + * Public Accessor functions TO BE USED ONLY BY Exception handlers + */ + + +/** + * Return the argument passed by the CPU upon exception, as stored in the + * saved context + */ +sos_ui32_t sos_cpu_context_get_EX_info(const struct sos_cpu_state *ctxt); + + +/** + * Return the faulting address of the exception + */ +sos_vaddr_t +sos_cpu_context_get_EX_faulting_vaddr(const struct sos_cpu_state *ctxt); + + +/* ======================================================================= + * Macros controlling stack poisoning. + * Stack poisoning can be used to detect: + * - unitialized local variables + * - when the thread might have gone too deep in the stack + */ +/** The signature of the poison */ +#define SOS_CPU_STATE_STACK_POISON 0xa5 + +/** + * When set, mean that the whole stack is poisoned to detect use of + * unititialized variables + */ +#define SOS_CPU_STATE_DETECT_UNINIT_KERNEL_VARS +/* #undef SOS_CPU_STATE_DETECT_UNINIT_KERNEL_VARS */ + +/** + * When set, mean that the bottom of the stack is poisoned to detect + * probable stack overflow. Its value indicates the number of bytes + * used for this detection. + */ +#define SOS_CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW 64 +/* #undef SOS_CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW */ + +#if defined(SOS_CPU_STATE_DETECT_KERNEL_STACK_OVERFLOW) +void +sos_cpu_state_prepare_detect_kernel_stack_overflow(const struct sos_cpu_state *ctxt, + sos_vaddr_t kernel_stack_bottom, + sos_size_t kernel_stack_size); +void sos_cpu_state_detect_kernel_stack_overflow(const struct sos_cpu_state *ctxt, + sos_vaddr_t kernel_stack_bottom, + sos_size_t kernel_stack_size); +#else +# define sos_cpu_state_prepare_detect_kernel_stack_overflow(ctxt,stkbottom,stksize) \ + ({ /* nop */ }) +# define sos_cpu_state_detect_kernel_stack_overflow(ctxt,stkbottom,stksize) \ + ({ /* nop */ }) +#endif + + +/* ======================================================================= + * Backtrace facility. To be used for DEBUGging purpose ONLY. + */ + + +/** + * The function called at each step of the backtrace iterations + * + * @param PC The address of the next instruction of the function that + * will be executed + * + * @param params The address of the array of the parameteres that have + * been passed to the function considered + * + * @param depth The index of the iteration (ie the depth of the + * current frame into the stack) + * + * @param custom_arg Whatever you want: this is the argument passed as + * custom_arg to sos_backtrace() + */ +typedef void (sos_backtrace_callback_t)(sos_vaddr_t PC, + sos_vaddr_t params, + sos_ui32_t depth, + void *custom_arg); + + +/** + * Call the backtracer callback on each frame stored in the cpu_state + * + * @param cpu_state The CPU context we want to explore. MUST be the + * context of a thread in Kernel mode, or NULL. When NULL: backtrace + * the current CPU context. + * + * @param max_depth The maximum number of frames to explore + * + * @param stack_bottom The lower boundary of the stack. This is used + * to make sure that the frame addresses fit inside the stack + * boudaries (ie are potentially correct). + * + * @param stack_size The size of the stack. Same comment. + * + * @param backtracer The function to call to handle the frame for each + * iteration + * + * @param custom_arg The arg passed as custom_arg to the backtracer + * + * @return The number of frames explored. + * + * @note Might be inaccurate when gcc's -fomit-frame-pointer has been + * used. + */ +sos_ui32_t sos_backtrace(const struct sos_cpu_state *cpu_state, + sos_ui32_t max_depth, + sos_vaddr_t stack_bottom, + sos_size_t stack_size, + sos_backtrace_callback_t * backtracer, + void *custom_arg); + +#endif /* _SOS_CPUCTXT_H_ */ diff --git a/sos-code-article6.5/hwcore/cpu_context_switch.S b/sos-code-article6.5/hwcore/cpu_context_switch.S new file mode 100644 index 0000000..4499c91 --- /dev/null +++ b/sos-code-article6.5/hwcore/cpu_context_switch.S @@ -0,0 +1,128 @@ +/* Copyright (C) 2005 David Decotigny + Copyright (C) 2000-2004, The KOS team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#define ASM_SOURCE 1 + + +.file "cpu_context_switch.S" + +.text + + +.globl sos_cpu_context_switch +.type sos_cpu_context_switch, @function +sos_cpu_context_switch: + // arg2= to_context -- esp+64 + // arg1= from_context -- esp+60 + // caller ip -- esp+56 + pushf // (eflags) esp+52 + pushl %cs // (cs) esp+48 + pushl $resume_pc // (ip) esp+44 + pushl $0 // (error code) esp+40 + pushl %ebp // esp+36 + pushl %edi // esp+32 + pushl %esi // esp+28 + pushl %edx // esp+24 + pushl %ecx // esp+20 + pushl %ebx // esp+16 + pushl %eax // esp+12 + subl $2, %esp // (alignment) esp+10 + pushw %ss // esp+8 + pushw %ds // esp+6 + pushw %es // esp+4 + pushw %fs // esp+2 + pushw %gs // esp + + /* + * Now that the original eax/ebx are stored, we can use them safely + */ + + /* Store the address of the saved context */ + movl 60(%esp), %ebx + movl %esp, (%ebx) + + /* This is the proper context switch ! We change the stack here */ + movl 64(%esp), %esp + + /* Restore the CPU context */ + popw %gs + popw %fs + popw %es + popw %ds + popw %ss + addl $2,%esp + popl %eax + popl %ebx + popl %ecx + popl %edx + popl %esi + popl %edi + popl %ebp + addl $4, %esp /* Ignore "error code" */ + + /* This restores the eflags, the cs and the eip registers */ + iret /* equivalent to: popfl ; ret */ + +resume_pc: + // Same context as that when sos_cpu_context_switch got called + // arg2= to_context -- esp+8 + // arg1= from_context -- esp+4 + // caller ip -- esp + ret + + + +/* ------------------------- */ +.globl sos_cpu_context_exit_to +.type sos_cpu_context_exit_to, @function +sos_cpu_context_exit_to: + // arg3= reclaiming_arg -- esp+12 + // arg2= reclaiming_func -- esp+8 + // arg1= to_context -- esp+4 + // caller ip -- esp + + /* Store the current SP in a temporary register */ + movl %esp, %eax + + /* This is the proper context switch ! We change the stack here */ + movl 4(%eax), %esp + + /* Call the reclaiming function (remember: the old frame address + is stored in eax) */ + pushl 12(%eax) + call *8(%eax) + addl $4, %esp + + /* Restore the CPU context */ + popw %gs + popw %fs + popw %es + popw %ds + popw %ss + addl $2,%esp + popl %eax + popl %ebx + popl %ecx + popl %edx + popl %esi + popl %edi + popl %ebp + addl $4, %esp /* Ignore "error code" */ + + /* This restores the eflags, the cs and the eip registers */ + iret /* equivalent to: popfl ; ret */ diff --git a/sos-code-article6.5/hwcore/exception.c b/sos-code-article6.5/hwcore/exception.c new file mode 100644 index 0000000..ffad632 --- /dev/null +++ b/sos-code-article6.5/hwcore/exception.c @@ -0,0 +1,167 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include "idt.h" +#include "irq.h" + +#include <sos/assert.h> +#include "exception.h" + +/* array of exception wrappers, defined in exception_wrappers.S */ +extern sos_vaddr_t sos_exception_wrapper_array[SOS_EXCEPT_NUM]; + +/* arrays of exception handlers, shared with exception_wrappers.S */ +sos_exception_handler_t sos_exception_handler_array[SOS_EXCEPT_NUM] = + { NULL, }; + +/* List of exception names for the x86 architecture */ +static const char * sos_x86_exnames[] = { + [SOS_EXCEPT_DIVIDE_ERROR] = "Division by zero", + [SOS_EXCEPT_DEBUG] = "Debug", + [SOS_EXCEPT_NMI_INTERRUPT] = "Non Maskable Interrupt", + [SOS_EXCEPT_BREAKPOINT] = "Breakpoint", + [SOS_EXCEPT_OVERFLOW] = "Overflow", + [SOS_EXCEPT_BOUND_RANGE_EXCEDEED] = "Bound Range Exceeded", + [SOS_EXCEPT_INVALID_OPCODE] = "Invalid Opcode", + [SOS_EXCEPT_DEVICE_NOT_AVAILABLE] = "Device Unavailable", + [SOS_EXCEPT_DOUBLE_FAULT] = "Double Fault", + [SOS_EXCEPT_COPROCESSOR_SEGMENT_OVERRUN] = "Coprocessor Segment Overrun", + [SOS_EXCEPT_INVALID_TSS] = "Invalid TSS", + [SOS_EXCEPT_SEGMENT_NOT_PRESENT] = "Segment Not Present", + [SOS_EXCEPT_STACK_SEGMENT_FAULT] = "Stack Segfault", + [SOS_EXCEPT_GENERAL_PROTECTION] = "General Protection", + [SOS_EXCEPT_PAGE_FAULT] = "Page Fault", + [SOS_EXCEPT_INTEL_RESERVED_1] = "INTEL1", + [SOS_EXCEPT_FLOATING_POINT_ERROR] = "FP Error", + [SOS_EXCEPT_ALIGNEMENT_CHECK] = "Alignment Check", + [SOS_EXCEPT_MACHINE_CHECK] = "Machine Check", + [SOS_EXCEPT_INTEL_RESERVED_2] = "INTEL2", + [SOS_EXCEPT_INTEL_RESERVED_3] = "INTEL3", + [SOS_EXCEPT_INTEL_RESERVED_4] = "INTEL4", + [SOS_EXCEPT_INTEL_RESERVED_5] = "INTEL5", + [SOS_EXCEPT_INTEL_RESERVED_6] = "INTEL6", + [SOS_EXCEPT_INTEL_RESERVED_7] = "INTEL7", + [SOS_EXCEPT_INTEL_RESERVED_8] = "INTEL8", + [SOS_EXCEPT_INTEL_RESERVED_9] = "INTEL9", + [SOS_EXCEPT_INTEL_RESERVED_10] = "INTEL10", + [SOS_EXCEPT_INTEL_RESERVED_11] = "INTEL11", + [SOS_EXCEPT_INTEL_RESERVED_12] = "INTEL12", + [SOS_EXCEPT_INTEL_RESERVED_13] = "INTEL13", + [SOS_EXCEPT_INTEL_RESERVED_14] = "INTEL14" +}; + + +/* Catch-all exception handler */ +static void sos_generic_ex(int exid, const struct sos_cpu_state *ctxt) +{ + const char *exname = sos_exception_get_name(exid); + + sos_display_fatal_error("Exception %s in Kernel at instruction 0x%x (info=%x)!\n", + exname, + sos_cpu_context_get_PC(ctxt), + (unsigned)sos_cpu_context_get_EX_info(ctxt)); +} + + +sos_ret_t sos_exception_subsystem_setup(void) +{ + sos_ret_t retval; + int exid; + + /* Setup the generic exception handler by default for everybody + except for the double fault exception */ + for (exid = 0 ; exid < SOS_EXCEPT_NUM ; exid ++) + { + /* Skip double fault (see below) */ + if (exid == SOS_EXCEPT_DOUBLE_FAULT) + continue; + + retval = sos_exception_set_routine(exid, sos_generic_ex); + if (SOS_OK != retval) + return retval; + } + + + /* We inidicate that the double fault exception handler is defined, + and give its address. this handler is a do-nothing handler (see + exception_wrappers.S), and it can NOT be overriden by the + functions below */ + return sos_idt_set_handler(SOS_EXCEPT_BASE + SOS_EXCEPT_DOUBLE_FAULT, + (sos_vaddr_t) sos_exception_wrapper_array[SOS_EXCEPT_DOUBLE_FAULT], + 0 /* CPL0 routine */); +} + + +sos_ret_t sos_exception_set_routine(int exception_number, + sos_exception_handler_t routine) +{ + sos_ret_t retval; + sos_ui32_t flags; + + if ((exception_number < 0) || (exception_number >= SOS_EXCEPT_NUM)) + return -SOS_EINVAL; + + /* Double fault not supported */ + if (exception_number == SOS_EXCEPT_DOUBLE_FAULT) + return -SOS_ENOSUP; + + sos_disable_IRQs(flags); + + retval = SOS_OK; + + /* Set the exception routine to be called by the exception wrapper */ + sos_exception_handler_array[exception_number] = routine; + + /* If the exception is to be enabled, update the IDT with the exception + wrapper */ + if (routine != NULL) + retval + = sos_idt_set_handler(SOS_EXCEPT_BASE + exception_number, + (sos_vaddr_t) sos_exception_wrapper_array[exception_number], + 0 /* CPL0 routine */); + else /* Disable the IDT entry */ + retval + = sos_idt_set_handler(SOS_EXCEPT_BASE + exception_number, + (sos_vaddr_t)NULL /* No routine => disable IDTE */, + 0 /* don't care */); + + sos_restore_IRQs(flags); + return retval; +} + + +sos_exception_handler_t sos_exception_get_routine(int exception_number) +{ + if ((exception_number < 0) || (exception_number >= SOS_EXCEPT_NUM)) + return NULL; + + /* Double fault not supported */ + if (exception_number == SOS_EXCEPT_DOUBLE_FAULT) + return NULL; + + /* Expected to be atomic */ + return sos_exception_handler_array[exception_number]; +} + + +const char * sos_exception_get_name(int exception_number) +{ + if ((exception_number < 0) || (exception_number >= SOS_EXCEPT_NUM)) + return NULL; + + return sos_x86_exnames[exception_number]; +} diff --git a/sos-code-article6.5/hwcore/exception.h b/sos-code-article6.5/hwcore/exception.h new file mode 100644 index 0000000..c58bbb2 --- /dev/null +++ b/sos-code-article6.5/hwcore/exception.h @@ -0,0 +1,83 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_HWEXCEPT_H_ +#define _SOS_HWEXCEPT_H_ + +/** + * @file exception.c + * + * Hardware exception routines management. + */ + +#ifndef ASM_SOURCE +# include <sos/errno.h> +# include "cpu_context.h" +#endif + +/** + * Standard Intel x86 exceptions. + * + * @see Intel x86 doc vol 3, section 5.12. + */ +#define SOS_EXCEPT_DIVIDE_ERROR 0 // No error code +#define SOS_EXCEPT_DEBUG 1 // No error code +#define SOS_EXCEPT_NMI_INTERRUPT 2 // No error code +#define SOS_EXCEPT_BREAKPOINT 3 // No error code +#define SOS_EXCEPT_OVERFLOW 4 // No error code +#define SOS_EXCEPT_BOUND_RANGE_EXCEDEED 5 // No error code +#define SOS_EXCEPT_INVALID_OPCODE 6 // No error code +#define SOS_EXCEPT_DEVICE_NOT_AVAILABLE 7 // No error code +#define SOS_EXCEPT_DOUBLE_FAULT 8 // Yes (Zero) +#define SOS_EXCEPT_COPROCESSOR_SEGMENT_OVERRUN 9 // No error code +#define SOS_EXCEPT_INVALID_TSS 10 // Yes +#define SOS_EXCEPT_SEGMENT_NOT_PRESENT 11 // Yes +#define SOS_EXCEPT_STACK_SEGMENT_FAULT 12 // Yes +#define SOS_EXCEPT_GENERAL_PROTECTION 13 // Yes +#define SOS_EXCEPT_PAGE_FAULT 14 // Yes +#define SOS_EXCEPT_INTEL_RESERVED_1 15 // No +#define SOS_EXCEPT_FLOATING_POINT_ERROR 16 // No +#define SOS_EXCEPT_ALIGNEMENT_CHECK 17 // Yes (Zero) +#define SOS_EXCEPT_MACHINE_CHECK 18 // No +#define SOS_EXCEPT_INTEL_RESERVED_2 19 // No +#define SOS_EXCEPT_INTEL_RESERVED_3 20 // No +#define SOS_EXCEPT_INTEL_RESERVED_4 21 // No +#define SOS_EXCEPT_INTEL_RESERVED_5 22 // No +#define SOS_EXCEPT_INTEL_RESERVED_6 23 // No +#define SOS_EXCEPT_INTEL_RESERVED_7 24 // No +#define SOS_EXCEPT_INTEL_RESERVED_8 25 // No +#define SOS_EXCEPT_INTEL_RESERVED_9 26 // No +#define SOS_EXCEPT_INTEL_RESERVED_10 27 // No +#define SOS_EXCEPT_INTEL_RESERVED_11 28 // No +#define SOS_EXCEPT_INTEL_RESERVED_12 29 // No +#define SOS_EXCEPT_INTEL_RESERVED_13 30 // No +#define SOS_EXCEPT_INTEL_RESERVED_14 31 // No + +#ifndef ASM_SOURCE + +typedef void (*sos_exception_handler_t)(int exception_number, + const struct sos_cpu_state *cpu_kstate); + +sos_ret_t sos_exception_subsystem_setup(void); +sos_ret_t sos_exception_set_routine(int exception_number, + sos_exception_handler_t routine); +sos_exception_handler_t sos_exception_get_routine(int exception_number); + +const char * sos_exception_get_name(int exception_number); +#endif /* ! ASM_SOURCE */ + +#endif /* _SOS_HWEXCEPT_H_ */ diff --git a/sos-code-article6.5/hwcore/exception_wrappers.S b/sos-code-article6.5/hwcore/exception_wrappers.S new file mode 100644 index 0000000..369614a --- /dev/null +++ b/sos-code-article6.5/hwcore/exception_wrappers.S @@ -0,0 +1,221 @@ +/* Copyright (C) 2004 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include "exception.h" + +.file "exception_wrappers.S" + +.text + +/* The address of the table of handlers (defined in exception.c) */ +.extern sos_exception_handler_array + +/* The address of the table of wrappers (defined below, and shared + with exception.c */ +.globl sos_exception_wrapper_array + +/** + * For exceptions with/without error code, refer to Intel x86 doc vol 3, + * section 5.12 + */ + +/* These wrappers are for exceptions without error code */ +.irp id, \ + SOS_EXCEPT_DIVIDE_ERROR, \ + SOS_EXCEPT_DEBUG, \ + SOS_EXCEPT_NMI_INTERRUPT, \ + SOS_EXCEPT_BREAKPOINT, \ + SOS_EXCEPT_OVERFLOW, \ + SOS_EXCEPT_BOUND_RANGE_EXCEDEED, \ + SOS_EXCEPT_INVALID_OPCODE, \ + SOS_EXCEPT_DEVICE_NOT_AVAILABLE, \ + SOS_EXCEPT_COPROCESSOR_SEGMENT_OVERRUN, \ + SOS_EXCEPT_INTEL_RESERVED_1, \ + SOS_EXCEPT_FLOATING_POINT_ERROR, \ + SOS_EXCEPT_MACHINE_CHECK, \ + SOS_EXCEPT_INTEL_RESERVED_2, \ + SOS_EXCEPT_INTEL_RESERVED_3, \ + SOS_EXCEPT_INTEL_RESERVED_4, \ + SOS_EXCEPT_INTEL_RESERVED_5, \ + SOS_EXCEPT_INTEL_RESERVED_6, \ + SOS_EXCEPT_INTEL_RESERVED_7, \ + SOS_EXCEPT_INTEL_RESERVED_8, \ + SOS_EXCEPT_INTEL_RESERVED_9, \ + SOS_EXCEPT_INTEL_RESERVED_10, \ + SOS_EXCEPT_INTEL_RESERVED_11, \ + SOS_EXCEPT_INTEL_RESERVED_12, \ + SOS_EXCEPT_INTEL_RESERVED_13, \ + SOS_EXCEPT_INTEL_RESERVED_14 + + .p2align 2, 0x90 + sos_exception_wrapper_\id: + .type sos_exception_wrapper_\id,@function + + /* Fake error code */ + pushl $0 + /* Backup the context */ + pushl %ebp + movl %esp, %ebp + + pushl %edi + pushl %esi + pushl %edx + pushl %ecx + pushl %ebx + pushl %eax + subl $2,%esp + pushw %ss + pushw %ds + pushw %es + pushw %fs + pushw %gs + + /* + * Call the handler with the exception number and the + * address of the stored CPU context as arguments + */ + pushl %esp + pushl $\id + leal sos_exception_handler_array,%edi + call *\id*4(%edi) + /* Unallocate the arguments passed to the handler */ + addl $8, %esp + + /* Restore the context */ + popw %gs + popw %fs + popw %es + popw %ds + popw %ss + addl $2,%esp + popl %eax + popl %ebx + popl %ecx + popl %edx + popl %esi + popl %edi + + popl %ebp + /* Remove fake error code */ + addl $4, %esp + iret +.endr + + /* These wrappers are for exceptions with error code */ +.irp id, \ + SOS_EXCEPT_INVALID_TSS, \ + SOS_EXCEPT_SEGMENT_NOT_PRESENT, \ + SOS_EXCEPT_STACK_SEGMENT_FAULT, \ + SOS_EXCEPT_GENERAL_PROTECTION, \ + SOS_EXCEPT_PAGE_FAULT, \ + SOS_EXCEPT_ALIGNEMENT_CHECK + + .p2align 2, 0x90 + sos_exception_wrapper_\id: + .type sos_exception_wrapper_\id,@function + + /* ret eflags */ + /* ret cs */ + /* ret eip */ + /* Error code */ + + /* Backup the context */ + pushl %ebp + movl %esp, %ebp + + pushl %edi + pushl %esi + pushl %edx + pushl %ecx + pushl %ebx + pushl %eax + subl $2,%esp + pushw %ss + pushw %ds + pushw %es + pushw %fs + pushw %gs + + /* + * Call the handler with the exception number and the + * address of the stored CPU context as arguments + */ + pushl %esp + pushl $\id + leal sos_exception_handler_array,%edi + call *\id*4(%edi) + /* Unallocate the arguments passed to the handler */ + addl $8, %esp + + /* Restore the context */ + popw %gs + popw %fs + popw %es + popw %ds + popw %ss + addl $2,%esp + popl %eax + popl %ebx + popl %ecx + popl %edx + popl %esi + popl %edi + popl %ebp + + /* Error code isn't compatible with iretd */ + addl $4, %esp + + iret +.endr + + +/* Double fault handler not supported. We must define it since we + define an entry for it in the sos_exception_wrapper_array. It + simply uses an alternate stack to display a message and stop the + system. qemu won't handle it correctly (see comment in qemu's + sources). */ +#define ALTERNATE_DOUBLE_FAULT_STACK_SIZE 512 +.irp id, SOS_EXCEPT_DOUBLE_FAULT +.p2align 2, 0x90 +sos_exception_wrapper_\id: +.type sos_exception_wrapper_\id,@function +1: cli /* Not necessary */ + movl $double_fault_alternate_stack, %eax + addl $ALTERNATE_DOUBLE_FAULT_STACK_SIZE, %eax + movl %eax, %esp + pushl $msg_double_fault_not_supported + call sos_display_fatal_error ; jmp 1b /* Not necessary */ +.endr + +.section ".rodata" +msg_double_fault_not_supported: + .string "exception_wrappers.S: Double fault detected ! NOT SUPPORTED yet. System Halted." + +/* Build the sos_irq_wrapper_array, shared with interrupt.c */ +.p2align 5, 0x0 +sos_exception_wrapper_array: + .irp id, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, \ + 16,17,18,19,20,21,22,23,24,25,26,27,29,30,31 + .long (sos_exception_wrapper_\id) + .endr + +/* Alternate stack for double fault handler */ +.bss +.p2align 2, 0x0 +.size double_fault_alternate_stack, ALTERNATE_DOUBLE_FAULT_STACK_SIZE +double_fault_alternate_stack: + .fill ALTERNATE_DOUBLE_FAULT_STACK_SIZE, 1, 0x0 diff --git a/sos-code-article6.5/hwcore/gdt.c b/sos-code-article6.5/hwcore/gdt.c new file mode 100644 index 0000000..781c9ca --- /dev/null +++ b/sos-code-article6.5/hwcore/gdt.c @@ -0,0 +1,152 @@ +/* Copyright (C) 2004 David Decotigny + Copyright (C) 2003 Thomas Petazzoni + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include "segment.h" + +#include "gdt.h" + + +/** + * The sructure of a segment descriptor. + * + * @see Intel x86 doc, Vol 3, section 3.4.3, figure 3-8. For segment + * types, see section 3.5 + */ +struct x86_segment_descriptor +{ + /* Lowest dword */ + sos_ui16_t limit_15_0; /* Segment limit, bits 15..0 */ + sos_ui16_t base_paged_addr_15_0; /* Base address, bits 15..0 */ + + /* Highest dword */ + sos_ui8_t base_paged_addr_23_16; /* Base address bits 23..16 */ + sos_ui8_t segment_type:4; /* Section 3.4.3.1 (code/data) + and 3.5 (system) of Intel x86 vol 3 */ + sos_ui8_t descriptor_type:1; /* 0=system, 1=Code/Data */ + sos_ui8_t dpl:2; /* Descriptor privilege level */ + sos_ui8_t present:1; + + sos_ui8_t limit_19_16:4; /* Segment limit, bits 19..16 */ + sos_ui8_t custom:1; + sos_ui8_t zero:1; + sos_ui8_t op_size:1; /* 0=16bits instructions, 1=32bits */ + sos_ui8_t granularity:1; /* 0=limit in bytes, 1=limit in pages */ + + sos_ui8_t base_paged_addr_31_24; /* Base address bits 31..24 */ +} __attribute__ ((packed, aligned (8))); + + +/** + * The GDT register, which stores the address and size of the + * GDT. + * + * @see Intel x86 doc vol 3, section 2.4, figure 2-4; and section + * 3.5.1 + */ +struct x86_gdt_register { + /* The maximum GDT offset allowed to access an entry in the GDT */ + sos_ui16_t limit; + + /* This is not exactly a "virtual" address, ie an adddress such as + those of instructions and data; this is a "linear" address, ie an + address in the paged memory. However, in SOS we configure the + segmented memory as a "flat" space: the 0-4GB segment-based (ie + "virtual") addresses directly map to the 0-4GB paged memory (ie + "linear"), so that the "linear" addresses are numerically equal + to the "virtual" addresses: this base_addr will thus be the same + as the address of the gdt array */ + sos_ui32_t base_addr; +} __attribute__((packed, aligned(8))); + + +/** + * Helper macro that builds a Segment descriptor for the virtual + * 0..4GB addresses to be mapped to the linear 0..4GB linear + * addresses. + */ +#define BUILD_GDTE(descr_privilege_level,is_code) \ + ((struct x86_segment_descriptor) { \ + .limit_15_0= 0xffff, \ + .base_paged_addr_15_0= 0, \ + .base_paged_addr_23_16= 0, \ + .segment_type= ((is_code)?0xb:0x3), \ + /* With descriptor_type (below) = 1 (code/data), \ + * see Figure 3-1 of section 3.4.3.1 in Intel \ + * x86 vol 3: \ + * - Code (bit 3 = 1): \ + * bit 0: 1=Accessed \ + * bit 1: 1=Readable \ + * bit 2: 0=Non-Conforming \ + * - Data (bit 3 = 0): \ + * bit 0: 1=Accessed \ + * bit 1: 1=Writable \ + * bit 2: 0=Expand up (stack-related) \ + * For Conforming/non conforming segments, see \ + * Intel x86 Vol 3 section 4.8.1.1 \ + */ \ + .descriptor_type= 1, /* 1=Code/Data */ \ + .dpl= ((descr_privilege_level) & 0x3), \ + .present= 1, \ + .limit_19_16= 0xf, \ + .custom= 0, \ + .op_size= 1, /* 32 bits instr/data */ \ + .granularity= 1 /* limit is in 4kB Pages */ \ + }) + + +/** The actual GDT */ +static struct x86_segment_descriptor gdt[] = { + [SOS_SEG_NULL] = (struct x86_segment_descriptor){ 0, }, + [SOS_SEG_KCODE] = BUILD_GDTE(0, 1), + [SOS_SEG_KDATA] = BUILD_GDTE(0, 0), +}; + +sos_ret_t sos_gdt_subsystem_setup(void) +{ + struct x86_gdt_register gdtr; + + /* Address of the GDT */ + gdtr.base_addr = (sos_ui32_t) gdt; + + /* The limit is the maximum offset in bytes from the base address of + the GDT */ + gdtr.limit = sizeof(gdt) - 1; + + /* Commit the GDT into the CPU, and update the segment + registers. The CS register may only be updated with a long jump + to an absolute address in the given segment (see Intel x86 doc + vol 3, section 4.8.1). */ + asm volatile ("lgdt %0 \n\ + ljmp %1,$1f \n\ + 1: \n\ + movw %2, %%ax \n\ + movw %%ax, %%ss \n\ + movw %%ax, %%ds \n\ + movw %%ax, %%es \n\ + movw %%ax, %%fs \n\ + movw %%ax, %%gs" + : + :"m"(gdtr), + "i"(SOS_BUILD_SEGMENT_REG_VALUE(0, FALSE, SOS_SEG_KCODE)), + "i"(SOS_BUILD_SEGMENT_REG_VALUE(0, FALSE, SOS_SEG_KDATA)) + :"memory","eax"); + + return SOS_OK; +} + + diff --git a/sos-code-article6.5/hwcore/gdt.h b/sos-code-article6.5/hwcore/gdt.h new file mode 100644 index 0000000..323b784 --- /dev/null +++ b/sos-code-article6.5/hwcore/gdt.h @@ -0,0 +1,42 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_GDT_H_ +#define _SOS_GDT_H_ + +/** + * @file gdt.h + * + * The routines that manage the GDT, the table that maps the virtual + * addresses (data/instructions, segment-relative), to "linear" + * addresses (ie paged-memory). In SOS/x86, we use a "flat" virtual + * space, ie the virtual and linear spaces are equivalent. + * + * @see Intel x86 doc vol 3, chapter 3 + */ + +#include <sos/types.h> +#include <sos/errno.h> + +/** + * Configure the virtual space as a direct mapping to the linear + * address space (ie "flat" virtual space). + */ +sos_ret_t sos_gdt_subsystem_setup(void); + + +#endif /* _SOS_GDT_H_ */ diff --git a/sos-code-article6.5/hwcore/i8254.c b/sos-code-article6.5/hwcore/i8254.c new file mode 100644 index 0000000..5198129 --- /dev/null +++ b/sos-code-article6.5/hwcore/i8254.c @@ -0,0 +1,79 @@ +/* Copyright (C) 2004 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include <hwcore/ioports.h> + +#include "i8254.h" + +/** 82c54 clock frequency */ +#define I8254_MAX_FREQ 1193180 + +/* Ports to communicate with the 82c54 */ +#define I8254_TIMER0 0x40 +#define I8254_TIMER1 0x41 +#define I8254_TIMER2 0x42 +#define I8254_CONTROL 0x43 + +/** + * Configure the first timer of the 82c54 chip as a rate generator, + * which will raise an IRQ0 on a regular periodic basis, as given by + * the freq parameter. Second (RAM refresh) and third (speaker) timers + * are left unchanged. Maximum frequency is that of the 8254 clock, ie + * 1193180 Hz. + * + * Ahhh PC systems are nice toys: this maximum "strange" frequency + * equals that of the NTSC clock (14.31818 MHz) divided by 12. In + * turn, the famous 4.77 MHz cpu clock frequency of the first IBM PC + * is this same NTSC frequency divided by 3. Why the NTSC frequency as + * a base "standard" ? Because the 14.31818 MHz quartz were cheap at + * that time, and because it allows to simply drive altogether the + * cpu, the "time of day" timer, and the video signal generators. + */ +sos_ret_t sos_i8254_set_frequency(unsigned int freq) +{ + unsigned int nb_tick; + + if (freq <= 0) + return -SOS_EINVAL; + + /* Compute counter value */ + nb_tick = I8254_MAX_FREQ / freq; + + /* Counter must be between 1 and 65536 */ + if (nb_tick > 65536) + return -SOS_EINVAL; + if (nb_tick <= 0) + return -SOS_EINVAL; + + /* The i8254 interprets 0 to mean counter == 65536, because 65536 + cannot be coded on 16bits */ + if (nb_tick == 65536) + nb_tick = 0; + + /* We want to configure timer0, we want to send both LSB+MSB to set + timer0 freq (-> 0x30), and we configure timer0 in mode 2, ie as a + rate generator (-> 0x4) ==> 0x34 */ + outb(0x34, I8254_CONTROL); + + /* Send LSB of counter first */ + outb((nb_tick & 0xFF), I8254_TIMER0); + + /* Send MSB of counter */ + outb((nb_tick >> 8) & 0xFF, I8254_TIMER0); + + return SOS_OK; +} diff --git a/sos-code-article6.5/hwcore/i8254.h b/sos-code-article6.5/hwcore/i8254.h new file mode 100644 index 0000000..406b713 --- /dev/null +++ b/sos-code-article6.5/hwcore/i8254.h @@ -0,0 +1,35 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_i8259_H_ +#define _SOS_i8259_H_ + +#include <sos/errno.h> + +/** + * @file i8254.h PC programmable timer + * + * Programmable timer routines. See the Intel 82C54 datasheet (on kos + * website). + * + * @see i82C54 datasheet on Kos website. + */ + +/** Change timer interrupt (IRQ 0) frequency */ +sos_ret_t sos_i8254_set_frequency(unsigned int freq); + +#endif /* _SOS_i8259_H_ */ diff --git a/sos-code-article6.5/hwcore/i8259.c b/sos-code-article6.5/hwcore/i8259.c new file mode 100644 index 0000000..d6c6c60 --- /dev/null +++ b/sos-code-article6.5/hwcore/i8259.c @@ -0,0 +1,79 @@ +/* Copyright (C) 2004 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include "ioports.h" + +#include "i8259.h" + +#define PIC_MASTER 0x20 +#define PIC_SLAVE 0xa0 + +/** Setup the 8259 PIC */ +sos_ret_t sos_i8259_subsystem_setup(void) +{ + /* Send ICW1: 8086 mode + NOT Single ctrl + call address + interval=8 */ + outb(0x11, PIC_MASTER); + outb(0x11, PIC_SLAVE); + + /* Send ICW2: ctrl base address */ + outb(0x20, PIC_MASTER+1); + outb(0x28, PIC_SLAVE+1); + + /* Send ICW3 master: mask where slaves are connected */ + outb(0x4, PIC_MASTER+1); + /* Send ICW3 slave: index where the slave is connected on master */ + outb(0x2, PIC_SLAVE+1); + + /* Send ICW4: 8086 mode, fully nested, not buffered, no implicit EOI */ + outb(0x1, PIC_MASTER+1); + outb(0x1, PIC_SLAVE+1); + + /* Send OCW1: + * Closing all IRQs : waiting for a correct handler The only IRQ + * enabled is the cascade (that's why we use 0xFB for the master) */ + outb(0xFB, PIC_MASTER+1); + outb(0xFF, PIC_SLAVE+1); + + return SOS_OK; +} + + +sos_ret_t sos_i8259_enable_irq_line(int numirq) +{ + if(numirq < 8) + /* irq on master PIC */ + outb((inb(PIC_MASTER+1) & ~(1 << numirq)), PIC_MASTER+1); + else + /* irq on slave PIC */ + outb((inb(PIC_SLAVE+1) & ~(1 << (numirq-8))), PIC_SLAVE+1); + + return SOS_OK; +} + + +sos_ret_t sos_i8259_disable_irq_line(int numirq) +{ + if(numirq < 8) + /* irq on master PIC */ + outb((inb(PIC_MASTER+1) | (1 << numirq)), PIC_MASTER+1); + else + /* irq on slave PIC */ + outb((inb(PIC_SLAVE+1) | (1 << (numirq-8))), PIC_SLAVE+1); + + return SOS_OK; +} diff --git a/sos-code-article6.5/hwcore/i8259.h b/sos-code-article6.5/hwcore/i8259.h new file mode 100644 index 0000000..0820524 --- /dev/null +++ b/sos-code-article6.5/hwcore/i8259.h @@ -0,0 +1,40 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_i8259_H_ +#define _SOS_i8259_H_ + +#include <sos/errno.h> + +/** + * @file i8259.h PIC + * + * PIC Management routines. See the Intel 8259A datasheet (on kos + * website), page 9+. Should be not be used directly: only interrupt.c + * should use this. + * + * @see i8259A datasheet on Kos website. + */ + +/** Setup PIC and Disable all IRQ lines */ +sos_ret_t sos_i8259_subsystem_setup(void); + +sos_ret_t sos_i8259_enable_irq_line(int numirq); + +sos_ret_t sos_i8259_disable_irq_line(int numirq); + +#endif /* _SOS_i8259_H_ */ diff --git a/sos-code-article6.5/hwcore/idt.c b/sos-code-article6.5/hwcore/idt.c new file mode 100644 index 0000000..07db4ab --- /dev/null +++ b/sos-code-article6.5/hwcore/idt.c @@ -0,0 +1,159 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include "segment.h" + +#include "idt.h" + +/** + * An entry in the IDT, or "IDTE" in the following, ie a reference to + * a interrupt/trap routine or a task gate to handle the sw/hw + * interrupts and exceptions. + * + * @see figure 5-2, intel x86 doc, vol 3 + */ +struct x86_idt_entry +{ + /* Low dword */ + sos_ui16_t offset_low; /* 15..0, offset of the routine in the segment */ + sos_ui16_t seg_sel; /* 31..16, the ID of the segment */ + + /* High dword */ + sos_ui8_t reserved:5; /* 4..0 */ + sos_ui8_t flags:3; /* 7..5 */ + sos_ui8_t type:3; /* 10..8 (interrupt gate, trap gate...) */ + sos_ui8_t op_size:1; /* 11 (0=16bits instructions, 1=32bits instr.) */ + sos_ui8_t zero:1; /* 12 */ + sos_ui8_t dpl:2; /* 14..13 */ + sos_ui8_t present:1; /* 15 */ + sos_ui16_t offset_high; /* 31..16 */ +} __attribute__((packed)); + + +/** + * The IDT register, which stores the address and size of the + * IDT. + * + * @see Intel x86 doc vol 3, section 2.4, figure 2-4 + */ +struct x86_idt_register +{ + /* The maximum GDT offset allowed to access an entry in the GDT */ + sos_ui16_t limit; + + /* This is not exactly a "virtual" address, ie an adddress such as + those of instructions and data; this is a "linear" address, ie an + address in the paged memory. However, in SOS we configure the + segmented memory as a "flat" space: the 0-4GB segment-based (ie + "virtual") addresses directly map to the 0-4GB paged memory (ie + "linear"), so that the "linear" addresses are numerically equal + to the "virtual" addresses: this base_addr will thus be the same + as the address of the gdt array */ + sos_ui32_t base_addr; +} __attribute__((packed, aligned (8))); + + +static struct x86_idt_entry idt[SOS_IDTE_NUM]; + +sos_ret_t sos_idt_subsystem_setup() +{ + struct x86_idt_register idtr; + int i; + + for (i = 0 ; + i < SOS_IDTE_NUM ; + i++) + { + struct x86_idt_entry *idte = idt + i; + + /* Setup an empty IDTE interrupt gate, see figure 5-2 in Intel + x86 doc, vol 3 */ + idte->seg_sel = SOS_BUILD_SEGMENT_REG_VALUE(0, FALSE, SOS_SEG_KCODE); + idte->reserved = 0; + idte->flags = 0; + idte->type = 0x6; /* Interrupt gate (110b) */ + idte->op_size = 1; /* 32bits instructions */ + idte->zero = 0; + + /* Disable this IDT entry for the moment */ + sos_idt_set_handler(i, (sos_vaddr_t)NULL, 0/* Don't care */); + } + + /* + * Setup the IDT register, see Intel x86 doc vol 3, section 5.8. + */ + + /* Address of the IDT */ + idtr.base_addr = (sos_ui32_t) idt; + + /* The limit is the maximum offset in bytes from the base address of + the IDT */ + idtr.limit = sizeof(idt) - 1; + + /* Commit the IDT into the CPU */ + asm volatile ("lidt %0\n"::"m"(idtr):"memory"); + + return SOS_OK; +} + + +sos_ret_t sos_idt_set_handler(int index, + sos_vaddr_t handler_address, + int lowest_priviledge /* 0..3 */) +{ + struct x86_idt_entry *idte; + + if ((index < 0) || (index >= SOS_IDTE_NUM)) + return -SOS_EINVAL; + if ((lowest_priviledge < 0) || (lowest_priviledge > 3)) + return -SOS_EINVAL; + + idte = idt + index; + if (handler_address != (sos_vaddr_t)NULL) + { + idte->offset_low = handler_address & 0xffff; + idte->offset_high = (handler_address >> 16) & 0xffff; + idte->dpl = lowest_priviledge; + idte->present = 1; /* Yes, there is a handler */ + } + else /* Disable this IDT entry */ + { + idte->offset_low = 0; + idte->offset_high = 0; + idte->dpl = 0; + idte->present = 0; /* No, there is no handler */ + } + + return SOS_OK; +} + + +sos_ret_t sos_idt_get_handler(int index, + sos_vaddr_t *handler_address, + int *lowest_priviledge) +{ + if ((index < 0) || (index >= SOS_IDTE_NUM)) + return -SOS_EINVAL; + + if (handler_address != NULL) + *handler_address = idt[index].offset_low + | (idt[index].offset_high << 16); + if (lowest_priviledge != NULL) + *lowest_priviledge = idt[index].dpl; + + return SOS_OK; +} diff --git a/sos-code-article6.5/hwcore/idt.h b/sos-code-article6.5/hwcore/idt.h new file mode 100644 index 0000000..2d165b0 --- /dev/null +++ b/sos-code-article6.5/hwcore/idt.h @@ -0,0 +1,84 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_IDT_H_ +#define _SOS_IDT_H_ + +/** + * @file idt.h + * + * Manage the x86 Interrupt Descriptor Table, the table which maps the + * hardware interrupt lines, hardware exceptions, and software + * interrupts, to software routines. We only define "interrupt gate" + * IDT entries. Don't use it directly; refer instead to interrupt.c, + * exceptions.c and syscall.c. + * + * @see Intel x86 doc, Vol 3, chapter 5 + */ + +#include <sos/errno.h> +#include <sos/types.h> + +/* Mapping of the CPU exceptions in the IDT (imposed by Intel + standards) */ +#define SOS_EXCEPT_BASE 0 +#define SOS_EXCEPT_NUM 32 +#define SOS_EXCEPT_MAX (SOS_HWEXCEPT_BASE + SOS_HWEXCEPT_NUM - 1) + +/* Mapping of the IRQ lines in the IDT */ +#define SOS_IRQ_BASE 32 +#define SOS_IRQ_NUM 16 +#define SOS_IRQ_MAX (SOS_IRQ_BASE + SOS_IRQ_NUM - 1) + +/** + * Number of IDT entries. + * + * @note Must be large enough to map the hw interrupts, the exceptions + * (=> total is 48 entries), and the syscall(s). Since our syscall + * will be 0x42, it must be >= 0x43. Intel doc limits this to 256 + * entries, we use this limit. + */ +#define SOS_IDTE_NUM 256 /* 0x100 */ + +/** Initialization routine: all the IDT entries (or "IDTE") are marked + "not present". */ +sos_ret_t sos_idt_subsystem_setup(void); + +/** + * Enable the IDT entry if handler_address != NULL, with the given + * lowest_priviledge.\ Disable the IDT entry when handler_address == + * NULL (the lowest_priviledge parameter is then ignored). Intel doc + * says that there must not be more than 256 entries. + * + * @note IRQ Unsafe + */ +sos_ret_t sos_idt_set_handler(int index, + sos_vaddr_t handler_address, + int lowest_priviledge /* 0..3 */); + + +/** + * @note IRQ Unsafe + * + * @return the handler address and DPL in the 2nd and 3rd + * parameters + */ +sos_ret_t sos_idt_get_handler(int index, + sos_vaddr_t *handler_address, + int *lowest_priviledge); + +#endif /* _SOS_IDT_H_ */ diff --git a/sos-code-article6.5/hwcore/ioports.h b/sos-code-article6.5/hwcore/ioports.h new file mode 100644 index 0000000..69c6d15 --- /dev/null +++ b/sos-code-article6.5/hwcore/ioports.h @@ -0,0 +1,46 @@ +/* Copyright (C) 2004 All GPL'ed OS + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_IOPORTS_H_ +#define _SOS_IOPORTS_H_ + +/** + * @ioports.h + * + * Intel-specific I/O space access routines. + */ + +/* This macro allows to write to an I/O port */ +#define outb(value, port) \ + __asm__ volatile ( \ + "outb %b0,%w1" \ + ::"a" (value),"Nd" (port) \ + ) \ + +// read one byte from port +#define inb(port) \ +({ \ + unsigned char _v; \ + __asm__ volatile ( \ + "inb %w1,%0" \ + :"=a" (_v) \ + :"Nd" (port) \ + ); \ + _v; \ +}) + +#endif /* _SOS_IOPORTS_H_ */ diff --git a/sos-code-article6.5/hwcore/irq.c b/sos-code-article6.5/hwcore/irq.c new file mode 100644 index 0000000..6722027 --- /dev/null +++ b/sos-code-article6.5/hwcore/irq.c @@ -0,0 +1,100 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include "idt.h" +#include "i8259.h" + +#include "irq.h" + +/** array of IRQ wrappers, defined in irq_wrappers.S */ +extern sos_vaddr_t sos_irq_wrapper_array[SOS_IRQ_NUM]; + +/** arrays of IRQ handlers, shared with irq_wrappers.S */ +sos_irq_handler_t sos_irq_handler_array[SOS_IRQ_NUM] = { NULL, }; + +/** Number of interrupt handlers that are currently executing */ +sos_ui32_t sos_irq_nested_level_counter; + +sos_ret_t sos_irq_subsystem_setup(void) +{ + sos_irq_nested_level_counter = 0; + return sos_i8259_subsystem_setup(); +} + + +sos_ret_t sos_irq_set_routine(int irq_level, + sos_irq_handler_t routine) +{ + sos_ret_t retval; + sos_ui32_t flags; + + if ((irq_level < 0) || (irq_level >= SOS_IRQ_NUM)) + return -SOS_EINVAL; + + sos_disable_IRQs(flags); + + retval = SOS_OK; + + /* Set the irq routine to be called by the IRQ wrapper */ + sos_irq_handler_array[irq_level] = routine; + + /* If the irq is to be enabled, update the IDT with the IRQ + wrapper */ + if (routine != NULL) + { + retval + = sos_idt_set_handler(SOS_IRQ_BASE + irq_level, + (sos_vaddr_t) sos_irq_wrapper_array[irq_level], + 0 /* CPL0 routine */); + /* A problem occured */ + if (retval != SOS_OK) + sos_irq_handler_array[irq_level] = NULL; + } + else /* Disable this idt entry */ + { + retval + = sos_idt_set_handler(SOS_IRQ_BASE + irq_level, + (sos_vaddr_t)NULL /* Disable IDTE */, + 0 /* Don't care */); + } + + /* Update the PIC only if an IRQ handler has been set */ + if (sos_irq_handler_array[irq_level] != NULL) + sos_i8259_enable_irq_line(irq_level); + else + sos_i8259_disable_irq_line(irq_level); + + sos_restore_IRQs(flags); + return retval; +} + + +sos_irq_handler_t sos_irq_get_routine(int irq_level) +{ + if ((irq_level < 0) || (irq_level >= SOS_IRQ_NUM)) + return NULL; + + /* Expected to be atomic */ + return sos_irq_handler_array[irq_level]; +} + + +sos_ui32_t sos_irq_get_nested_level() +{ + /* No need to disable interrupts here */ + return sos_irq_nested_level_counter; +} diff --git a/sos-code-article6.5/hwcore/irq.h b/sos-code-article6.5/hwcore/irq.h new file mode 100644 index 0000000..c067cc4 --- /dev/null +++ b/sos-code-article6.5/hwcore/irq.h @@ -0,0 +1,97 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_HWINTR_H_ +#define _SOS_HWINTR_H_ + + +/** + * @file irq.c + * + * Hardware interrupts routines management. + */ + + +#include <sos/errno.h> +#include "cpu_context.h" + + +#define sos_save_flags(flags) \ + asm volatile("pushfl ; popl %0":"=g"(flags)::"memory") +#define sos_restore_flags(flags) \ + asm volatile("push %0; popfl"::"g"(flags):"memory") + + +#define sos_disable_IRQs(flags) \ + ({ sos_save_flags(flags); asm("cli\n"); }) +#define sos_restore_IRQs(flags) \ + sos_restore_flags(flags) + + +/* Usual IRQ levels */ +#define SOS_IRQ_TIMER 0 +#define SOS_IRQ_KEYBOARD 1 +#define SOS_IRQ_SLAVE_PIC 2 +#define SOS_IRQ_COM2 3 +#define SOS_IRQ_COM1 4 +#define SOS_IRQ_LPT2 5 +#define SOS_IRQ_FLOPPY 6 +#define SOS_IRQ_LPT1 7 +#define SOS_IRQ_8_NOT_DEFINED 8 +#define SOS_IRQ_RESERVED_1 9 +#define SOS_IRQ_RESERVED_2 10 +#define SOS_IRQ_RESERVED_3 11 +#define SOS_IRQ_RESERVED_4 12 +#define SOS_IRQ_COPROCESSOR 13 +#define SOS_IRQ_HARDDISK 14 +#define SOS_IRQ_RESERVED_5 15 + + +/** Definition of an hardware IRQ handler */ +typedef void (*sos_irq_handler_t)(int irq_level); + + +/** Setup the PIC */ +sos_ret_t sos_irq_subsystem_setup(void); + + +/** + * If the routine is not NULL, the IDT is setup to call an IRQ + * wrapper upon interrupt, which in turn will call the routine, and + * the PIC is programmed to raise an irq.\ If the routine is + * NULL, we disable the irq line. + */ +sos_ret_t sos_irq_set_routine(int irq_level, + sos_irq_handler_t routine); + +sos_irq_handler_t sos_irq_get_routine(int irq_level); + + +/** + * Tell how many nested IRQ handler have been fired + */ +sos_ui32_t sos_irq_get_nested_level(); + + +/** + * Return TRUE when we are currently executing in interrupt context + */ +#define sos_servicing_irq() \ + (sos_irq_get_nested_level() > 0) + + +#endif /* _SOS_HWINTR_H_ */ diff --git a/sos-code-article6.5/hwcore/irq_wrappers.S b/sos-code-article6.5/hwcore/irq_wrappers.S new file mode 100644 index 0000000..8945123 --- /dev/null +++ b/sos-code-article6.5/hwcore/irq_wrappers.S @@ -0,0 +1,220 @@ +/* Copyright (C) 2004 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +.file "irq_wrappers.S" + +.text + +/** The address of the table of handlers (defined in irq.c) */ +.extern sos_irq_handler_array + +/** The address of the table of wrappers (defined below, and shared + with irq.c */ +.globl sos_irq_wrapper_array + +/** The variable holding the nested level of the IRQ handlers */ +.extern sos_irq_nested_level_counter + +/* These pre-handlers are for IRQ (Master PIC) */ +.irp id, 0,1,2,3,4,5,6,7 + + .p2align 2, 0x90 + + sos_irq_wrapper_\id: + .type sos_irq_wrapper_\id,@function + + /* + * Backup the CPU context + */ + + /* Fake error code */ + pushl $0 + + /* Backup the actual context */ + pushl %ebp + movl %esp, %ebp + + pushl %edi + pushl %esi + pushl %edx + pushl %ecx + pushl %ebx + pushl %eax + subl $2,%esp + pushw %ss + pushw %ds + pushw %es + pushw %fs + pushw %gs + + /* + * Increment IRQ nested level + */ + incl sos_irq_nested_level_counter + + /* Send EOI to PIC. See Intel 8259 datasheet + available on Kos website */ + movb $0x20, %al + outb %al, $0x20 + + /* + * Call the handler with IRQ number as argument + */ + pushl $\id + leal sos_irq_handler_array,%edi + call *\id*4(%edi) + addl $4, %esp + + /* + * Decrement IRQ nested level + */ + cli /* Just in case we messed up everything in the handler */ + subl $1, sos_irq_nested_level_counter + + /* sos_irq_nested_level_counter went below 0 ?! */ + jnc 2f + + 1: /* Yes: Print fatal error message */ + pushl $msg_nested_level_overflow + call sos_display_fatal_error + addl $4, %esp ; jmp 1b + /* Never returns */ + + 2: /* No: all right ! */ + + /* Restore the context */ + popw %gs + popw %fs + popw %es + popw %ds + popw %ss + addl $2,%esp + popl %eax + popl %ebx + popl %ecx + popl %edx + popl %esi + popl %edi + popl %ebp + + /* Remove fake error code */ + addl $4, %esp + + iret + .endr + + +/* These pre-handlers are for IRQ (Slave PIC) */ +.irp id, 8,9,10,11,12,13,14,15 + + .p2align 2, 0x90 + + sos_irq_wrapper_\id: + .type sos_irq_wrapper_\id,@function + + /* + * Backup the CPU context + */ + + /* Fake error code */ + pushl $0 + + /* Backup the actual context */ + pushl %ebp + movl %esp, %ebp + + pushl %edi + pushl %esi + pushl %edx + pushl %ecx + pushl %ebx + pushl %eax + subl $2,%esp + pushw %ss + pushw %ds + pushw %es + pushw %fs + pushw %gs + + /* + * Increment IRQ nested level + */ + incl sos_irq_nested_level_counter + + /* Send EOI to PIC. See Intel 8259 datasheet + available on Kos website */ + movb $0x20, %al + outb %al, $0xa0 + outb %al, $0x20 + + /* + * Call the handler with IRQ number as argument + */ + pushl $\id + leal sos_irq_handler_array,%edi + call *\id*4(%edi) + addl $4, %esp + + /* + * Decrement IRQ nested level + */ + cli /* Just in case we messed up everything in the handler */ + subl $1, sos_irq_nested_level_counter + + /* sos_irq_nested_level_counter went below 0 ?! */ + jnc 2f + + 1: /* Yes: Print fatal error message */ + pushl $msg_nested_level_overflow + call sos_display_fatal_error + addl $4, %esp ; jmp 1b + /* Never returns */ + + 2: /* No: all right ! */ + + /* Restore the context */ + popw %gs + popw %fs + popw %es + popw %ds + popw %ss + addl $2,%esp + popl %eax + popl %ebx + popl %ecx + popl %edx + popl %esi + popl %edi + popl %ebp + + /* Remove fake error code */ + addl $4, %esp + + iret + .endr + +.section ".rodata" +msg_nested_level_overflow: + .string "irq_wrappers.S: IRQ Nested level overflow ! System halted." + +/* Build the sos_irq_wrapper_array, shared with irq.c */ +.p2align 5, 0x0 +sos_irq_wrapper_array: + .irp id, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + .long (sos_irq_wrapper_\id) + .endr diff --git a/sos-code-article6.5/hwcore/paging.c b/sos-code-article6.5/hwcore/paging.c new file mode 100644 index 0000000..11f3da6 --- /dev/null +++ b/sos-code-article6.5/hwcore/paging.c @@ -0,0 +1,465 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include <sos/physmem.h> +#include <sos/klibc.h> +#include <sos/assert.h> + +#include "paging.h" + + +/** The structure of a page directory entry. See Intel vol 3 section + 3.6.4 */ +struct x86_pde +{ + sos_ui32_t present :1; /* 1=PT mapped */ + sos_ui32_t write :1; /* 0=read-only, 1=read/write */ + sos_ui32_t user :1; /* 0=supervisor, 1=user */ + sos_ui32_t write_through :1; /* 0=write-back, 1=write-through */ + sos_ui32_t cache_disabled :1; /* 1=cache disabled */ + sos_ui32_t accessed :1; /* 1=read/write access since last clear */ + sos_ui32_t zero :1; /* Intel reserved */ + sos_ui32_t page_size :1; /* 0=4kB, 1=4MB or 2MB (depending on PAE) */ + sos_ui32_t global_page :1; /* Ignored (Intel reserved) */ + sos_ui32_t custom :3; /* Do what you want with them */ + sos_ui32_t pt_paddr :20; +} __attribute__ ((packed)); + + +/** The structure of a page table entry. See Intel vol 3 section + 3.6.4 */ +struct x86_pte +{ + sos_ui32_t present :1; /* 1=PT mapped */ + sos_ui32_t write :1; /* 0=read-only, 1=read/write */ + sos_ui32_t user :1; /* 0=supervisor, 1=user */ + sos_ui32_t write_through :1; /* 0=write-back, 1=write-through */ + sos_ui32_t cache_disabled :1; /* 1=cache disabled */ + sos_ui32_t accessed :1; /* 1=read/write access since last clear */ + sos_ui32_t dirty :1; /* 1=write access since last clear */ + sos_ui32_t zero :1; /* Intel reserved */ + sos_ui32_t global_page :1; /* 1=No TLB invalidation upon cr3 switch + (when PG set in cr4) */ + sos_ui32_t custom :3; /* Do what you want with them */ + sos_ui32_t paddr :20; +} __attribute__ ((packed)); + + +/** Structure of the x86 CR3 register: the Page Directory Base + Register. See Intel x86 doc Vol 3 section 2.5 */ +struct x86_pdbr +{ + sos_ui32_t zero1 :3; /* Intel reserved */ + sos_ui32_t write_through :1; /* 0=write-back, 1=write-through */ + sos_ui32_t cache_disabled :1; /* 1=cache disabled */ + sos_ui32_t zero2 :7; /* Intel reserved */ + sos_ui32_t pd_paddr :20; +} __attribute__ ((packed)); + + +/** + * Helper macro to control the MMU: invalidate the TLB entry for the + * page located at the given virtual address. See Intel x86 vol 3 + * section 3.7. + */ +#define invlpg(vaddr) \ + do { \ + __asm__ __volatile__("invlpg %0"::"m"(*((unsigned *)(vaddr)))); \ + } while(0) + + +/** + * Helper macro to control the MMU: invalidate the whole TLB. See + * Intel x86 vol 3 section 3.7. + */ +#define flush_tlb() \ + do { \ + unsigned long tmpreg; \ + asm volatile("movl %%cr3,%0\n\tmovl %0,%%cr3" :"=r" \ + (tmpreg) : :"memory"); \ + } while (0) + + +/** + * Helper macro to compute the index in the PD for the given virtual + * address + */ +#define virt_to_pd_index(vaddr) \ + (((unsigned)(vaddr)) >> 22) + + +/** + * Helper macro to compute the index in the PT for the given virtual + * address + */ +#define virt_to_pt_index(vaddr) \ + ( (((unsigned)(vaddr)) >> 12) & 0x3ff ) + + +/** + * Helper macro to compute the offset in the page for the given virtual + * address + */ +#define virt_to_page_offset(vaddr) \ + (((unsigned)(vaddr)) & SOS_PAGE_MASK) + + +/** + * Helper function to map a page in the pd.\ Suppose that the RAM + * is identity mapped to resolve PT actual (CPU) address from the PD + * entry + */ +static sos_ret_t paging_setup_map_helper(struct x86_pde * pd, + sos_paddr_t ppage, + sos_vaddr_t vaddr) +{ + /* Get the page directory entry and table entry index for this + address */ + unsigned index_in_pd = virt_to_pd_index(vaddr); + unsigned index_in_pt = virt_to_pt_index(vaddr); + + /* Make sure the page table was mapped */ + struct x86_pte * pt; + if (pd[index_in_pd].present) + { + pt = (struct x86_pte*) (pd[index_in_pd].pt_paddr << 12); + + /* If we allocate a new entry in the PT, increase its reference + count. This test will always be TRUE here, since the setup + routine scans the kernel pages in a strictly increasing + order: at each step, the map will result in the allocation of + a new PT entry. For the sake of clarity, we keep the test + here. */ + if (! pt[index_in_pt].present) + sos_physmem_ref_physpage_at((sos_paddr_t)pt); + + /* The previous test should always be TRUE */ + else + SOS_ASSERT_FATAL(FALSE); /* indicate a fatal error */ + } + else + { + /* No : allocate a new one */ + pt = (struct x86_pte*) sos_physmem_ref_physpage_new(FALSE); + if (! pt) + return -SOS_ENOMEM; + + memset((void*)pt, 0x0, SOS_PAGE_SIZE); + + pd[index_in_pd].present = TRUE; + pd[index_in_pd].write = 1; /* It would be too complicated to + determine whether it + corresponds to a real R/W area + of the kernel code/data or + read-only */ + pd[index_in_pd].pt_paddr = ((sos_paddr_t)pt) >> 12; + } + + + /* Map the page in the page table */ + pt[index_in_pt].present = 1; + pt[index_in_pt].write = 1; /* It would be too complicated to + determine whether it corresponds to + a real R/W area of the kernel + code/data or R/O only */ + pt[index_in_pt].user = 0; + pt[index_in_pt].paddr = ppage >> 12; + + return SOS_OK; +} + + +sos_ret_t sos_paging_subsystem_setup(sos_paddr_t identity_mapping_base, + sos_paddr_t identity_mapping_top) +{ + /* The PDBR we will setup below */ + struct x86_pdbr cr3; + + /* Get the PD for the kernel */ + struct x86_pde * pd + = (struct x86_pde*) sos_physmem_ref_physpage_new(FALSE); + + /* The iterator for scanning the kernel area */ + sos_paddr_t paddr; + + /* Reset the PD. For the moment, there is still an IM for the whole + RAM, so that the paddr are also vaddr */ + memset((void*)pd, + 0x0, + SOS_PAGE_SIZE); + + /* Identity-map the identity_mapping_* area */ + for (paddr = identity_mapping_base ; + paddr < identity_mapping_top ; + paddr += SOS_PAGE_SIZE) + { + if (paging_setup_map_helper(pd, paddr, paddr)) + return -SOS_ENOMEM; + } + + /* Identity-map the PC-specific BIOS/Video area */ + for (paddr = BIOS_N_VIDEO_START ; + paddr < BIOS_N_VIDEO_END ; + paddr += SOS_PAGE_SIZE) + { + if (paging_setup_map_helper(pd, paddr, paddr)) + return -SOS_ENOMEM; + } + + /* Ok, kernel is now identity mapped in the PD. We still have to set + up the mirroring */ + pd[virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)].present = TRUE; + pd[virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)].write = 1; + pd[virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)].user = 0; + pd[virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)].pt_paddr + = ((sos_paddr_t)pd)>>12; + + /* We now just have to configure the MMU to use our PD. See Intel + x86 doc vol 3, section 3.6.3 */ + memset(& cr3, 0x0, sizeof(struct x86_pdbr)); /* Reset the PDBR */ + cr3.pd_paddr = ((sos_paddr_t)pd) >> 12; + + /* Actual loading of the PDBR in the MMU: setup cr3 + bits 31[Paging + Enabled] and 16[Write Protect] of cr0, see Intel x86 doc vol 3, + sections 2.5, 3.6.1 and 4.11.3 + note table 4-2 */ + asm volatile ("movl %0,%%cr3\n\t" + "movl %%cr0,%%eax\n\t" + "orl $0x80010000, %%eax\n\t" /* bit 31 | bit 16 */ + "movl %%eax,%%cr0\n\t" + "jmp 1f\n\t" + "1:\n\t" + "movl $2f, %%eax\n\t" + "jmp *%%eax\n\t" + "2:\n\t" ::"r"(cr3):"memory","eax"); + + /* + * Here, the only memory available is: + * - The BIOS+video area + * - the identity_mapping_base .. identity_mapping_top area + * - the PD mirroring area (4M) + * All accesses to other virtual addresses will generate a #PF + */ + + return SOS_OK; +} + + +/* Suppose that the current address is configured with the mirroring + * enabled to access the PD and PT. */ +sos_ret_t sos_paging_map(sos_paddr_t ppage_paddr, + sos_vaddr_t vpage_vaddr, + sos_bool_t is_user_page, + sos_ui32_t flags) +{ + /* Get the page directory entry and table entry index for this + address */ + unsigned index_in_pd = virt_to_pd_index(vpage_vaddr); + unsigned index_in_pt = virt_to_pt_index(vpage_vaddr); + + /* Get the PD of the current context */ + struct x86_pde *pd = (struct x86_pde*) + (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)); + + /* Address of the PT in the mirroring */ + struct x86_pte * pt = (struct x86_pte*) (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*index_in_pd); + + /* The mapping of anywhere in the PD mirroring is FORBIDDEN ;) */ + if ((vpage_vaddr >= SOS_PAGING_MIRROR_VADDR) + && (vpage_vaddr < SOS_PAGING_MIRROR_VADDR + SOS_PAGING_MIRROR_SIZE)) + return -SOS_EINVAL; + + /* Map a page for the PT if necessary */ + if (! pd[index_in_pd].present) + { + + /* No : allocate a new one */ + sos_paddr_t pt_ppage + = sos_physmem_ref_physpage_new(! (flags & SOS_VM_MAP_ATOMIC)); + if (! pt_ppage) + { + return -SOS_ENOMEM; + } + + pd[index_in_pd].present = TRUE; + pd[index_in_pd].write = 1; /* Ignored in supervisor mode, see + Intel vol 3 section 4.12 */ + pd[index_in_pd].user = (is_user_page)?1:0; + pd[index_in_pd].pt_paddr = ((sos_paddr_t)pt_ppage) >> 12; + + /* + * The PT is now mapped in the PD mirroring + */ + + /* Invalidate TLB for the page we just added */ + invlpg(pt); + + /* Reset this new PT */ + memset((void*)pt, 0x0, SOS_PAGE_SIZE); + } + + /* If we allocate a new entry in the PT, increase its reference + count. */ + else if (! pt[index_in_pt].present) + sos_physmem_ref_physpage_at(pd[index_in_pd].pt_paddr << 12); + + /* Otherwise, that means that a physical page is implicitely + unmapped */ + else + sos_physmem_unref_physpage(pt[index_in_pt].paddr << 12); + + /* Map the page in the page table */ + pt[index_in_pt].present = TRUE; + pt[index_in_pt].write = (flags & SOS_VM_MAP_PROT_WRITE)?1:0; + pt[index_in_pt].user = (is_user_page)?1:0; + pt[index_in_pt].paddr = ppage_paddr >> 12; + sos_physmem_ref_physpage_at(ppage_paddr); + + /* + * The page is now mapped in the current address space + */ + + /* Invalidate TLB for the page we just added */ + invlpg(vpage_vaddr); + + return SOS_OK; +} + + +sos_ret_t sos_paging_unmap(sos_vaddr_t vpage_vaddr) +{ + sos_ret_t pt_unref_retval; + + /* Get the page directory entry and table entry index for this + address */ + unsigned index_in_pd = virt_to_pd_index(vpage_vaddr); + unsigned index_in_pt = virt_to_pt_index(vpage_vaddr); + + /* Get the PD of the current context */ + struct x86_pde *pd = (struct x86_pde*) + (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)); + + /* Address of the PT in the mirroring */ + struct x86_pte * pt = (struct x86_pte*) (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*index_in_pd); + + /* No page mapped at this address ? */ + if (! pd[index_in_pd].present) + return -SOS_EINVAL; + if (! pt[index_in_pt].present) + return -SOS_EINVAL; + + /* The unmapping of anywhere in the PD mirroring is FORBIDDEN ;) */ + if ((vpage_vaddr >= SOS_PAGING_MIRROR_VADDR) + && (vpage_vaddr < SOS_PAGING_MIRROR_VADDR + SOS_PAGING_MIRROR_SIZE)) + return -SOS_EINVAL; + + /* Reclaim the physical page */ + sos_physmem_unref_physpage(pt[index_in_pt].paddr << 12); + + /* Unmap the page in the page table */ + memset(pt + index_in_pt, 0x0, sizeof(struct x86_pte)); + + /* Invalidate TLB for the page we just unmapped */ + invlpg(vpage_vaddr); + + /* Reclaim this entry in the PT, which may free the PT */ + pt_unref_retval = sos_physmem_unref_physpage(pd[index_in_pd].pt_paddr << 12); + SOS_ASSERT_FATAL(pt_unref_retval >= 0); + if (pt_unref_retval == TRUE) + /* If the PT is now completely unused... */ + { + union { struct x86_pde pde; sos_ui32_t ui32; } u; + + /* + * Reset the PDE + */ + + /* Mark the PDE as unavailable */ + u.ui32 = 0; + + /* Update the PD */ + pd[index_in_pd] = u.pde; + + /* Update the TLB */ + invlpg(pt); + } + + return SOS_OK; +} + + +int sos_paging_get_prot(sos_vaddr_t vaddr) +{ + int retval; + + /* Get the page directory entry and table entry index for this + address */ + unsigned index_in_pd = virt_to_pd_index(vaddr); + unsigned index_in_pt = virt_to_pt_index(vaddr); + + /* Get the PD of the current context */ + struct x86_pde *pd = (struct x86_pde*) + (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)); + + /* Address of the PT in the mirroring */ + struct x86_pte * pt = (struct x86_pte*) (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*index_in_pd); + + /* No page mapped at this address ? */ + if (! pd[index_in_pd].present) + return SOS_VM_MAP_PROT_NONE; + if (! pt[index_in_pt].present) + return SOS_VM_MAP_PROT_NONE; + + /* Default access right of an available page is "read" on x86 */ + retval = SOS_VM_MAP_PROT_READ; + if (pd[index_in_pd].write && pt[index_in_pt].write) + retval |= SOS_VM_MAP_PROT_WRITE; + + return retval; +} + + +sos_paddr_t sos_paging_get_paddr(sos_vaddr_t vaddr) +{ + /* Get the page directory entry and table entry index for this + address */ + unsigned index_in_pd = virt_to_pd_index(vaddr); + unsigned index_in_pt = virt_to_pt_index(vaddr); + unsigned offset_in_page = virt_to_page_offset(vaddr); + + /* Get the PD of the current context */ + struct x86_pde *pd = (struct x86_pde*) + (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*virt_to_pd_index(SOS_PAGING_MIRROR_VADDR)); + + /* Address of the PT in the mirroring */ + struct x86_pte * pt = (struct x86_pte*) (SOS_PAGING_MIRROR_VADDR + + SOS_PAGE_SIZE*index_in_pd); + + /* No page mapped at this address ? */ + if (! pd[index_in_pd].present) + return (sos_paddr_t)NULL; + if (! pt[index_in_pt].present) + return (sos_paddr_t)NULL; + + return (pt[index_in_pt].paddr << 12) + offset_in_page; +} diff --git a/sos-code-article6.5/hwcore/paging.h b/sos-code-article6.5/hwcore/paging.h new file mode 100644 index 0000000..24b9fc9 --- /dev/null +++ b/sos-code-article6.5/hwcore/paging.h @@ -0,0 +1,132 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_PAGING_H_ +#define _SOS_PAGING_H_ + +/** + * @file paging.h + * + * MMU management routines (arch-dependent). Setup the MMU without + * identity-mapping physical<->virtual addresses over the whole + * physical address space: a single, restricted and known, area is + * identity-mapped, the remaining kernel/user space is not. To access + * and manage the MMU translation tables (PD/PT on x86), we rely on a + * particular configuration, called "mirroring", where the top-level + * translation table (PD on x86) maps itself at a known and fixed (virtual) + * address. The only assumption for this to be possible is that the + * structure of the translation table entries are compatible at the + * different levels of vadddr->paddr translation process (PDE and PTE + * on x86 are Ok). Credits go to Christophe Avoinne for that. + */ + +#include <sos/types.h> +#include <sos/errno.h> + + +/** + * Basic SOS virtual memory organization + */ +/** Frontier between kernel and user space virtual addresses */ +#define SOS_PAGING_BASE_USER_ADDRESS (0x40000000) /* 1GB */ +#define SOS_PAGING_TOP_USER_ADDRESS (0xFFFFFFFF) /* 4GB */ + +/** Length of the space reserved for the mirroring in the kernel + virtual space */ +#define SOS_PAGING_MIRROR_SIZE (1 << 22) /* 1 PD = 1024 Page Tables = 4MB */ + +/** Virtual address where the mirroring takes place */ +#define SOS_PAGING_MIRROR_VADDR \ + (SOS_PAGING_BASE_USER_ADDRESS - SOS_PAGING_MIRROR_SIZE) + + +/** + * sos_paging_map flags + */ +/** Usual virtual memory access rights */ +#define SOS_VM_MAP_PROT_NONE 0 +#define SOS_VM_MAP_PROT_READ (1<<0) +#define SOS_VM_MAP_PROT_WRITE (1<<1) +/* EXEC not supported */ + +/** Mapping a page may involve an physical page allocation (for a new + PT), hence may potentially block */ +#define SOS_VM_MAP_ATOMIC (1<<31) + + +/** + * Setup initial page directory structure where the kernel is + * identically-mapped, and the mirroring. This routine also + * identity-maps the BIOS and video areas, to allow some debugging + * text to be printed to the console. Finally, this routine installs + * the whole configuration into the MMU. + */ +sos_ret_t sos_paging_subsystem_setup(sos_paddr_t identity_mapping_base, + sos_paddr_t identity_mapping_top); + +/** + * Map the given physical page at the given virtual address in the + * current address space. + * + * @note *IMPORTANT*: The physical page ppage_paddr *MUST* have been + * referenced by the caller through either a call to + * sos_physmem_ref_physpage_new() or sos_physmem_ref_physpage_at(). It + * would work if this were untrue, but this would be INCORRECT (it is + * expected that one is owning the page before mapping it, or + * otherwise the page could have been stolen by an interrupt or + * another thread). + * + * @param ppage_paddr The address of a physical page (page-aligned) + * @param vpage_vaddr The address of the virtual page (page-aligned) + * @param is_user_page TRUE when the page is available from user space + * @param flags A mask made of SOS_VM_* bits + * + * @note Unless the SOS_VM_MAP_ATOMIC bit is set in the flags, the + * function may potentially block, because a physical page may be + * allocated for a new PT. + */ +sos_ret_t sos_paging_map(sos_paddr_t ppage_paddr, + sos_vaddr_t vpage_vaddr, + sos_bool_t is_user_page, + sos_ui32_t flags); + +/** + * Undo the mapping from vaddr to the underlying physical page (if any) + * @param vpage_vaddr The address of the virtual page (page-aligned) + */ +sos_ret_t sos_paging_unmap(sos_vaddr_t vpage_vaddr); + +/** + * Return the page protection flags (SOS_VM_MAP_PROT_*) associated + * with the address, or SOS_VM_MAP_PROT_NONE when page is not mapped + */ +int sos_paging_get_prot(sos_vaddr_t vaddr); + +/** + * Return the physical address of the given virtual address. Since page + * at physical addr 0 is not mapped, the NULL result means "page not + * mapped". + */ +sos_paddr_t sos_paging_get_paddr(sos_vaddr_t vaddr); + +/** + * Tell whether the address is physically mapped + */ +#define sos_paging_check_present(vaddr) \ + (sos_paging_get_paddr(vaddr) != NULL) + +#endif /* _SOS_PAGING_H_ */ diff --git a/sos-code-article6.5/hwcore/segment.h b/sos-code-article6.5/hwcore/segment.h new file mode 100644 index 0000000..4487216 --- /dev/null +++ b/sos-code-article6.5/hwcore/segment.h @@ -0,0 +1,70 @@ +/* Copyright (C) 2004 The SOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_HWSEGS_H_ +#define _SOS_HWSEGS_H_ + +/** + * @file segments.h + * + * Global and local (GDT/LDT) segment descriptor definition and + * structure. These segments map virtual addresses (ie + * data/instruction addresses, relative to these segment descriptors) + * to linear addresses (ie addresses in the paged-memory space). + * + * @see Intel x86 doc, vol 3 chapter 3. + */ + + +/* + * Global segment selectors (GDT) for SOS/x86. + * + * @see gdt.h + */ +#define SOS_SEG_NULL 0 /* NULL segment, unused by the procesor */ +#define SOS_SEG_KCODE 1 /* Kernel code segment */ +#define SOS_SEG_KDATA 2 /* Kernel data segment */ + + +#ifndef ASM_SOURCE +/** + * Helper macro that builds a segment register's value + */ +#define SOS_BUILD_SEGMENT_REG_VALUE(desc_privilege,in_ldt,seg_index) \ + ( (((desc_privilege) & 0x3) << 0) \ + | (((in_ldt)?1:0) << 2) \ + | ((seg_index) << 3) ) +#else +/* + * Assembler-compliant version. + * + * Caution: In assembler code, "in_ldt" MUST be either 1 or 0, nothing + * else. + */ +#define SOS_BUILD_SEGMENT_REG_VALUE(desc_privilege,in_ldt,seg_index) \ + ( (((desc_privilege) & 0x3) << 0) \ + | ((in_ldt & 1) << 2) \ + | ((seg_index) << 3) ) +#endif + + +/* + * Local segment selectors (LDT) for SOS/x86 + */ +/* None */ + +#endif /* _SOS_HWSEGS_H_ */ diff --git a/sos-code-article6.5/sos.map b/sos-code-article6.5/sos.map new file mode 100644 index 0000000..3d26a75 --- /dev/null +++ b/sos-code-article6.5/sos.map @@ -0,0 +1,320 @@ +0020a708 a.1066 +00207dc7 _add_action +002081e1 add_in_ready_queue +00209fd7 AffectMovement +0020ce64 arg_b.1440 +0020ce74 arg_c.1441 +0020ce84 arg_d.1442 +0020ce94 arg_e.1443 +0020cea4 arg_R.1444 +0020ceb4 arg_S.1445 +00208824 backtracer.1399 +00201000 __b_kernel +00200000 __b_load +0020a568 bootstrap_stack_bottom +0020a56c bootstrap_stack_size +00204ab7 cache_add_slab +00204c08 cache_grow +00204978 cache_initialize +0020ce10 cache_of_struct_kslab +0020ce0c cache_of_struct_kslab_cache +00204cfb cache_release_slab +0020ce3c cache_thread +0020cedc CheeseCount +00209ce7 ChoosePosition +002087e7 clk_it +0020ce5c clock_count.1387 +002033e8 core_routine +00204eaf create_cache_of_caches +00204fd9 create_cache_of_ranges +002094d1 CreateMap +0020a31b CreateMouse +00203b02 create_range +0020ce34 current_thread +00206fc2 delete_thread +0020ce60 demand_paging_count.1410 +00208b67 demo_thread +0020875c display_bits +0020cbc0 double_fault_alternate_stack +0020959c DrawMap +00209358 DrawPixel +00208988 dump_backtrace +00211000 __e_kernel +002097f9 ElementInit +0020c327 __e_load +00209ed8 EvaluatePositions +0020394e find_suitable_free_range +002058fa free_object +0020ce1c free_ppage +0020a570 gdt +002038f8 get_closest_preceding_kmem_range +002062be get_page_descr_at_paddr +00209094 idle_thread +0020c340 idt +002096c3 InitMapInput +0020975e InitMapOutput +0020399e insert_range +0020a171 IsCollision +0020a640 kmalloc_cache +0020ce00 kmem_free_range_list +0020ce08 kmem_range_cache +0020ce04 kmem_used_range_list +0020ce14 kslab_cache_list +00207640 _kwaitq_add_entry +002077d3 _kwaitq_remove_entry +0020ce4c last_tick_time +00203a4b lookup_range +00201011 loop +00206697 memcmp +00206640 memcpy +00206671 memset +002099be Mouse +0020ced8 MouseCount +0020a2da MouseCreator +00209b0c MouseMove +00209394 MouseSim +0020a710 MouseSpeed +0020a8e0 msg_double_fault_not_supported +0020a9c0 msg_nested_level_overflow +00201000 multiboot_entry +00200000 multiboot_header +0020cee0 ObstacleCount +0020248c paging_setup_map_helper +002089fd pgflt_ex +0020ce18 physical_page_descr_array +0020ce24 physmem_base +0020ce28 physmem_top +0020ce2c physmem_total_pages +0020ce30 physmem_used_pages +0020cec4 pMap +0020b548 __PRETTY_FUNCTION__.1040 +0020afe6 __PRETTY_FUNCTION__.1064 +0020aff3 __PRETTY_FUNCTION__.1073 +0020ac94 __PRETTY_FUNCTION__.1093 +0020ba8b __PRETTY_FUNCTION__.1096 +0020b000 __PRETTY_FUNCTION__.1101 +0020aa9f __PRETTY_FUNCTION__.1111 +0020acc0 __PRETTY_FUNCTION__.1121 +0020b5a8 __PRETTY_FUNCTION__.1124 +0020aceb __PRETTY_FUNCTION__.1131 +0020b01d __PRETTY_FUNCTION__.1132 +0020ad02 __PRETTY_FUNCTION__.1136 +0020b382 __PRETTY_FUNCTION__.1137 +0020aab7 __PRETTY_FUNCTION__.1145 +0020ad19 __PRETTY_FUNCTION__.1145 +0020bb4e __PRETTY_FUNCTION__.1162 +0020b3a0 __PRETTY_FUNCTION__.1177 +0020bb61 __PRETTY_FUNCTION__.1179 +0020b9e7 __PRETTY_FUNCTION__.1183 +0020b9f9 __PRETTY_FUNCTION__.1200 +0020b3c7 __PRETTY_FUNCTION__.1227 +0020b3dc __PRETTY_FUNCTION__.1242 +0020b400 __PRETTY_FUNCTION__.1262 +0020b8d1 __PRETTY_FUNCTION__.1262 +0020b8e8 __PRETTY_FUNCTION__.1267 +0020c318 __PRETTY_FUNCTION__.1273 +0020b8f5 __PRETTY_FUNCTION__.1294 +0020b905 __PRETTY_FUNCTION__.1309 +0020c321 __PRETTY_FUNCTION__.1312 +0020b91c __PRETTY_FUNCTION__.1325 +0020b92a __PRETTY_FUNCTION__.1335 +0020c061 __PRETTY_FUNCTION__.1414 +0020c06a __PRETTY_FUNCTION__.1428 +0020c076 __PRETTY_FUNCTION__.1470 +0020a704 q.1067 +0020a70c r.1068 +00206888 random +0020a700 _random_seed +0020ce54 ready_queue +00208021 _remove_action +002038cf resume_pc +0020cec8 SemMap +0020ced0 SemMouse +00206ccd _set_current +0020ad38 sizeof_struct_sos_kmem_range +00207359 sleep_timeout +00206c4a snprintf +002037f0 sos_backtrace +002032dd sos_bochs_hexdump +002033a2 sos_bochs_printf +002030c2 sos_bochs_putchar +002030ff sos_bochs_puthex +002030da sos_bochs_putstring +002030b8 sos_bochs_setup +002036c1 sos_cpu_context_dump +002038d0 sos_cpu_context_exit_to +002037df sos_cpu_context_get_EX_faulting_vaddr +00203798 sos_cpu_context_get_EX_info +00203636 sos_cpu_context_get_PC +0020367d sos_cpu_context_get_SP +0020388c sos_cpu_context_switch +00203437 sos_cpu_kstate_init +0020354f sos_cpu_state_detect_kernel_stack_overflow +00203518 sos_cpu_state_prepare_detect_kernel_stack_overflow +00206e30 sos_create_kernel_thread +002086e4 sos_display_fatal_error +00201382 sos_exception_get_name +00201353 sos_exception_get_routine +0020cb40 sos_exception_handler_array +002012c0 sos_exception_set_routine +00201260 sos_exception_subsystem_setup +002013a4 sos_exception_wrapper_0 +002013e8 sos_exception_wrapper_1 +00201a48 sos_exception_wrapper_10 +00201a8c sos_exception_wrapper_11 +00201ad0 sos_exception_wrapper_12 +00201b14 sos_exception_wrapper_13 +00201b58 sos_exception_wrapper_14 +00201608 sos_exception_wrapper_15 +0020164c sos_exception_wrapper_16 +00201b9c sos_exception_wrapper_17 +00201690 sos_exception_wrapper_18 +002016d4 sos_exception_wrapper_19 +0020142c sos_exception_wrapper_2 +00201718 sos_exception_wrapper_20 +0020175c sos_exception_wrapper_21 +002017a0 sos_exception_wrapper_22 +002017e4 sos_exception_wrapper_23 +00201828 sos_exception_wrapper_24 +0020186c sos_exception_wrapper_25 +002018b0 sos_exception_wrapper_26 +002018f4 sos_exception_wrapper_27 +00201938 sos_exception_wrapper_28 +0020197c sos_exception_wrapper_29 +00201470 sos_exception_wrapper_3 +002019c0 sos_exception_wrapper_30 +00201a04 sos_exception_wrapper_31 +002014b4 sos_exception_wrapper_4 +002014f8 sos_exception_wrapper_5 +0020153c sos_exception_wrapper_6 +00201580 sos_exception_wrapper_7 +00201be0 sos_exception_wrapper_8 +002015c4 sos_exception_wrapper_9 +0020a940 sos_exception_wrapper_array +002011e4 sos_gdt_subsystem_setup +00201218 sos_generic_ex +00202e44 sos_i8254_set_frequency +00202432 sos_i8259_disable_irq_line +002023d4 sos_i8259_enable_irq_line +00202384 sos_i8259_subsystem_setup +00201174 sos_idt_get_handler +002010b9 sos_idt_set_handler +00201014 sos_idt_subsystem_setup +00201cfe sos_irq_get_nested_level +00201cdc sos_irq_get_routine +0020cdc0 sos_irq_handler_array +0020cee4 sos_irq_nested_level_counter +00201c13 sos_irq_set_routine +00201bfc sos_irq_subsystem_setup +00201d08 sos_irq_wrapper_0 +00201d6c sos_irq_wrapper_1 +00202114 sos_irq_wrapper_10 +0020217c sos_irq_wrapper_11 +002021e4 sos_irq_wrapper_12 +0020224c sos_irq_wrapper_13 +002022b4 sos_irq_wrapper_14 +0020231c sos_irq_wrapper_15 +00201dd4 sos_irq_wrapper_2 +00201e3c sos_irq_wrapper_3 +00201ea4 sos_irq_wrapper_4 +00201f0c sos_irq_wrapper_5 +00201f74 sos_irq_wrapper_6 +00201fdc sos_irq_wrapper_7 +00202044 sos_irq_wrapper_8 +002020ac sos_irq_wrapper_9 +0020aa00 sos_irq_wrapper_array +00205e4b sos_kfree +00205da9 sos_kmalloc +00205ce0 sos_kmalloc_subsystem_setup +0020565e sos_kmem_cache_alloc +002054cb sos_kmem_cache_create +002055cf sos_kmem_cache_destroy +00205bc8 sos_kmem_cache_free +00205c11 sos_kmem_cache_release_struct_range +002054af sos_kmem_cache_subsystem_setup_commit +002050f1 sos_kmem_cache_subsystem_setup_prepare +0020488b sos_kmem_vmm_alloc +0020432f sos_kmem_vmm_del_range +002048c2 sos_kmem_vmm_free +00204954 sos_kmem_vmm_is_valid_vaddr +002040cf sos_kmem_vmm_new_range +0020492b sos_kmem_vmm_resolve_slab +0020490b sos_kmem_vmm_set_slab +00203d52 sos_kmem_vmm_subsystem_setup +002085bc sos_kmutex_dispose +00208596 sos_kmutex_init +002085d2 sos_kmutex_lock +00208638 sos_kmutex_trylock +00208670 sos_kmutex_unlock +002084a5 sos_ksema_dispose +002084bb sos_ksema_down +00208480 sos_ksema_init +00208518 sos_ksema_trydown +00208553 sos_ksema_up +002077a9 sos_kwaitq_add_entry +002075b9 sos_kwaitq_dispose +00207588 sos_kwaitq_init +0020760e sos_kwaitq_init_entry +002075e7 sos_kwaitq_is_empty +00207931 sos_kwaitq_remove_entry +0020795b sos_kwaitq_wait +002079c3 sos_kwaitq_wakeup +002090cf sos_main +00202d87 sos_paging_get_paddr +00202cd7 sos_paging_get_prot +00202851 sos_paging_map +002026c5 sos_paging_subsystem_setup +00202b2f sos_paging_unmap +002065b9 sos_physmem_get_kmem_range +00206613 sos_physmem_get_state +0020630b sos_physmem_ref_physpage_at +00206151 sos_physmem_ref_physpage_new +002065e2 sos_physmem_set_kmem_range +00205e74 sos_physmem_subsystem_setup +0020644e sos_physmem_unref_physpage +00208360 sos_reschedule +0020832d sos_sched_set_ready +002081b8 sos_sched_subsystem_setup +0020708b sos_thread_exit +00207528 sos_thread_force_unblock +00206c80 sos_thread_get_current +00207182 sos_thread_get_state +002073b8 sos_thread_sleep +00206d2c sos_thread_subsystem_setup +00207335 sos_thread_yield +00207c65 sos_time_cmp +00207ae6 sos_time_dec +00208146 sos_time_do_tick +00207d95 sos_time_get_now +00207d31 sos_time_get_tick_resolution +00207a40 sos_time_inc +00207cc7 sos_time_is_zero +00207fe1 sos_time_register_action_absolute +00207fa1 sos_time_register_action_relative +00207d63 sos_time_set_tick_resolution +00207ceb sos_time_subsysem_setup +00208121 sos_time_unregister_action +0020a5a0 sos_x86_exnames +00202edc sos_x86_videomem_cls +00203026 sos_x86_videomem_printf +00202faf sos_x86_videomem_putchar +00202f1f sos_x86_videomem_putstring +00202ebc sos_x86_videomem_setup +002068d5 srandom +0020d000 stack +00201000 start +00201000 _start +002067e5 strcmp +002066f1 strlen +00206827 strncmp +00206716 strnlen +0020679a strzcat +00206749 strzcpy +0020719b _switch_to_next_thread +00208efc test_thread +0020ce38 thread_list +0020ce44 tick_resolution +0020ce40 timeout_action_list +0020ce20 used_ppage +0020a620 video +002068e2 vsnprintf diff --git a/sos-code-article6.5/sos/assert.c b/sos-code-article6.5/sos/assert.c new file mode 100644 index 0000000..2bc0d41 --- /dev/null +++ b/sos-code-article6.5/sos/assert.c @@ -0,0 +1,44 @@ +/* Copyright (C) 2004 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +#include <sos/klibc.h> +#include <drivers/bochs.h> +#include <drivers/x86_videomem.h> + +#include "assert.h" + +void sos_display_fatal_error(const char *format, /* args */...) +{ + char buff[256]; + va_list ap; + + asm("cli\n"); /* disable interrupts -- x86 only */ \ + + va_start(ap, format); + vsnprintf(buff, sizeof(buff), format, ap); + va_end(ap); + + sos_bochs_putstring(buff); sos_bochs_putstring("\n"); + sos_x86_videomem_putstring(23, 0, + SOS_X86_VIDEO_BG_BLACK + | SOS_X86_VIDEO_FG_LTRED , buff); + + /* Infinite loop: processor halted */ + for ( ; ; ) + asm("hlt\n"); +} diff --git a/sos-code-article6.5/sos/assert.h b/sos-code-article6.5/sos/assert.h new file mode 100644 index 0000000..9fcfec0 --- /dev/null +++ b/sos-code-article6.5/sos/assert.h @@ -0,0 +1,45 @@ +/* Copyright (C) 2004 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_ASSERT_H_ +#define _SOS_ASSERT_H_ + + +void sos_display_fatal_error(const char *format, /* args */...) + __attribute__ ((format (printf, 1, 2), noreturn)); + + +/** + * If the expr is FALSE, print a message and halt the machine + */ +#define SOS_ASSERT_FATAL(expr) \ + ({ \ + int __res=(int)(expr); \ + if (! __res) \ + sos_display_fatal_error("%s@%s:%d Assertion " # expr " failed", \ + __PRETTY_FUNCTION__, __FILE__, __LINE__); \ + }) + + +#define SOS_FATAL_ERROR(fmt,args...) \ + ({ \ + sos_display_fatal_error("%s@%s:%d FATAL: " fmt, \ + __PRETTY_FUNCTION__, __FILE__, __LINE__, \ + ##args); \ + }) + +#endif /* _SOS_ASSERT_H_ */ diff --git a/sos-code-article6.5/sos/errno.h b/sos-code-article6.5/sos/errno.h new file mode 100644 index 0000000..fda5ed8 --- /dev/null +++ b/sos-code-article6.5/sos/errno.h @@ -0,0 +1,42 @@ +/* Copyright (C) 2004 The SOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_ERRNO_H_ +#define _SOS_ERRNO_H_ + +/** + * @file errno.h + * + * SOS return value codes and errors. + */ + +/* Positive values of the error codes */ +#define SOS_OK 0 /* No error */ +#define SOS_EINVAL 1 /* Invalid argument */ +#define SOS_ENOSUP 2 /* Operation not supported */ +#define SOS_ENOMEM 3 /* No available memory */ +#define SOS_EBUSY 4 /* Object or device still in use */ +#define SOS_EINTR 5 /* Wait/Sleep has been interrupted */ +#define SOS_EPERM 6 /* Mutex/files ownership error */ +#define SOS_EFATAL 255 /* Internal fatal error */ + +/* A negative value means that an error occured. For + * example -SOS_EINVAL means that the error was "invalid + * argument" */ +typedef int sos_ret_t; + +#endif /* _SOS_ERRNO_H_ */ diff --git a/sos-code-article6.5/sos/klibc.c b/sos-code-article6.5/sos/klibc.c new file mode 100644 index 0000000..4442842 --- /dev/null +++ b/sos-code-article6.5/sos/klibc.c @@ -0,0 +1,308 @@ +/* Copyright (C) 2004 David Decotigny (with INSA Rennes for vsnprintf) + Copyright (C) 2003 The KOS Team + Copyright (C) 1999 Free Software Foundation + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include "klibc.h" + +/* For an optimized version, see BSD sources ;) */ +void *memcpy(void *dst0, const void *src0, register unsigned int size) +{ + char *dst; + const char *src; + for (dst = (char*)dst0, src = (const char*)src0 ; + size > 0 ; + dst++, src++, size--) + *dst = *src; + return dst0; +} + +/* ditto */ +void *memset(void *dst0, register int c, register unsigned int length) +{ + char *dst; + for (dst = (char*) dst0 ; + length > 0 ; + dst++, length --) + *dst = (char)c; + return dst0; +} + +int memcmp(const void *s1, const void *s2, sos_size_t len) +{ + const unsigned char *c1, *c2; + unsigned int i; + + for (i = 0, c1 = s1, c2 = s2; i < len; i++, c1++, c2++) + { + if(*c1 != *c2) + return *c1 - *c2; + } + + return 0; +} + + +unsigned int strlen(register const char *str) +{ + unsigned int retval = 0; + + while (*str++) + retval++; + + return retval; +} + + +unsigned int strnlen(const char * s, sos_size_t count) +{ + const char *sc; + + for (sc = s; count-- && *sc != '\0'; ++sc) + /* nothing */continue; + + return sc - s; +} + + +char *strzcpy(register char *dst, register const char *src, register int len) +{ + int i; + + if (len <= 0) + return dst; + + for (i = 0; i < len; i++) + { + dst[i] = src[i]; + if(src[i] == '\0') + return dst; + } + + dst[len-1] = '\0'; + return dst; +} + + +char *strzcat (char *dest, const char *src, sos_size_t n) +{ + char *res = dest; + + for ( ; *dest ; dest++); + + for ( ; *src ; src++, dest++) { + *dest = *src; + n--; + if (n <= 0) + break; + } + + *dest = '\0'; + return res; +} + +int strcmp(register const char *s1, register const char *s2) +{ + while (*s1 == *s2++) + if (*s1++ == 0) + return (0); + + return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1)); +} + + +int strncmp(register const char *s1, register const char *s2, register int len) +{ + char c1 = '\0', c2 = '\0'; + + while (len > 0) + { + c1 = (unsigned char) *s1++; + c2 = (unsigned char) *s2++; + if (c1 == '\0' || c1 != c2) + return c1 - c2; + len--; + } + + return c1 - c2; +} + + +static unsigned long int _random_seed = 93186752; + +/** + * The following code is borrowed from Glenn Rhoads. + * http://remus.rutgers.edu/~rhoads/Code/code.html + * License to be defined... + */ +unsigned long int random (void) +{ +/* The following parameters are recommended settings based on research + uncomment the one you want. */ + +/* For RAND_MAX == 4294967291 */ + static unsigned int a = 1588635695, q = 2, r = 1117695901; +/* static unsigned int a = 1223106847, m = 4294967291U, q = 3, r = 625646750;*/ +/* static unsigned int a = 279470273, m = 4294967291U, q = 15, r = 102913196;*/ + +/* For RAND_MAX == 2147483647 */ +/* static unsigned int a = 1583458089, m = 2147483647, q = 1, r = 564025558; */ +/* static unsigned int a = 784588716, m = 2147483647, q = 2, r = 578306215; */ +/* static unsigned int a = 16807, m = 2147483647, q = 127773, r = 2836; */ +/* static unsigned int a = 950706376, m = 2147483647, q = 2, r = 246070895; */ + + _random_seed = a*(_random_seed % q) - r*(_random_seed / q); + return _random_seed; +} + + +void srandom (unsigned long int seed) +{ + _random_seed = seed; +} + + +/* I (d2) borrowed and rewrote this for Nachos/INSA Rennes. Thanks to + them for having kindly allowed me to do so. */ +int vsnprintf(char *buff, sos_size_t len, const char * format, va_list ap) +{ + sos_size_t i, result; + + if (!buff || !format || (len < 0)) + return -1; + +#define PUTCHAR(thechar) \ + do { \ + if (result < len-1) \ + *buff++ = (thechar); \ + result++; \ + } while (0) + + result = 0; + for(i=0 ; format[i] != '\0' ; i++){ + switch (format[i]) + { + case '%': + i++; + switch(format[i]) + { + case '%': + { + PUTCHAR('%'); + break; + } + case 'i':; + case 'd': + { + int integer = va_arg(ap,int); + int cpt2 = 0; + char buff_int[16]; + + if (integer<0) + PUTCHAR('-'); + /* Ne fait pas integer = -integer ici parce que INT_MIN + n'a pas d'equivalent positif (int = [-2^31, 2^31-1]) */ + + do { + int m10 = integer%10; + m10 = (m10 < 0)? -m10:m10; + buff_int[cpt2++]=(char)('0'+ m10); + integer=integer/10; + } while(integer!=0); + + for(cpt2 = cpt2 - 1 ; cpt2 >= 0 ; cpt2--) + PUTCHAR(buff_int[cpt2]); + + break; + } + + case 'c': + { + int value = va_arg(ap,int); + PUTCHAR((char)value); + break; + } + + case 's': + { + char *string = va_arg(ap,char *); + if (! string) + string = "(null)"; + for( ; *string != '\0' ; string++) + PUTCHAR(*string); + break; + } + + case 'p': + PUTCHAR('0'); + PUTCHAR('x'); + case 'x': + { + unsigned int hexa = va_arg(ap,int); + unsigned int nb; + int i, had_nonzero = 0; + for(i=0 ; i < 8 ; i++) + { + nb = (unsigned int)(hexa << (i*4)); + nb = (nb >> 28) & 0xf; + // Skip the leading zeros + if (nb == 0) + { + if (had_nonzero) + PUTCHAR('0'); + } + else + { + had_nonzero = 1; + if (nb < 10) + PUTCHAR('0'+nb); + else + PUTCHAR('a'+(nb-10)); + } + } + if (! had_nonzero) + PUTCHAR('0'); + break; + } + break; + + default: + PUTCHAR('%'); + PUTCHAR(format[i]); + } + break; + + default: + PUTCHAR(format[i]); + } + } + + *buff = '\0'; + return result; +} + + +int snprintf(char * buff, sos_size_t len, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + len = vsnprintf(buff, len, format, ap); + va_end(ap); + + return len; +} diff --git a/sos-code-article6.5/sos/klibc.h b/sos-code-article6.5/sos/klibc.h new file mode 100644 index 0000000..7002778 --- /dev/null +++ b/sos-code-article6.5/sos/klibc.h @@ -0,0 +1,103 @@ +/* Copyright (C) 2003 The KOS Team + Copyright (C) 1999 Free Software Foundation + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_KLIBC_H_ +#define _SOS_KLIBC_H_ + +/** + * @file klibc.h + * + * Basic libc-style support for common useful functions (string.h, + * stdarg.h), some with slight non-standard behavior (see comments). + * + * Most of the prototypes of these functions are borrowed from + * FreeBSD, but their implementation (in klibc.c) come either from Kos + * (GPL v2) or from David Decotigny (SOS). + */ + +#include <sos/types.h> + +/* string.h functions */ + +void *memcpy(void *dst, const void *src, register unsigned int size ) ; +void *memset(void *dst, register int c, register unsigned int length ) ; +int memcmp(const void *s1, const void *s2, sos_size_t n); + +unsigned int strlen( register const char *str) ; +unsigned int strnlen(const char * s, sos_size_t maxlen); + +/** + * @note Same as strncpy(), with a slightly different semantic. + * Actually, strncpy(3C) says " The result will not be null-terminated + * if the length of 'from' is n or more.". Here, 'dst' is ALWAYS + * null-terminated. And its total len will ALWAYS be <= len, with + * null-terminating-char included. + */ +char *strzcpy( register char *dst, register const char *src, + register int len ) ; + +/** + * @note Same as strncat(), with the same semantic : 'dst' is ALWAYS + * null-terminated. And its total len will ALWAYS be <= len, with + * null-terminating-char included. + */ +char *strzcat (char *dest, const char *src, + const sos_size_t len); + +int strcmp(register const char *s1, register const char *s2 ); +int strncmp(register const char *s1, register const char *s2, + register int len ); + +/* Basic stdarg.h macros. Taken from gcc support files */ +#define __GNUC_VA_LIST +typedef void *__gnuc_va_list; +typedef __gnuc_va_list va_list; +#define __va_rounded_size(TYPE) \ + (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int)) +#define va_start(AP, LASTARG) \ + (AP = ((__gnuc_va_list) __builtin_next_arg (LASTARG))) +#define va_end(AP) \ + ((void)0) +#define va_arg(AP, TYPE) \ + (AP = (__gnuc_va_list) ((char *) (AP) + __va_rounded_size (TYPE)), \ + *((TYPE *) (void *) ((char *) (AP) - __va_rounded_size (TYPE)))) +#define __va_copy(dest, src) \ + (dest) = (src) + +/* stdarg.h functions. There might be a non-standard behavior: there + will always be a trailing '\0' in the resulting string */ +int vsnprintf(char *, sos_size_t, const char *, va_list); +int snprintf(char *, sos_size_t, const char *, /*args*/ ...) + __attribute__ ((format (printf, 3, 4))); + + +/* + * Pseudo-random generation functions. Useful to do some coverage + * tests. + */ + +/* Amplitude of the random number generation */ +#define RAND_MAX 4294967291U + +/* Pseudo-random number generation (MT unsafe) */ +unsigned long int random (void); + +/* Set random seed (MT unsafe) */ +void srandom (unsigned long int seed); + +#endif /* _SOS_KLIBC_H_ */ diff --git a/sos-code-article6.5/sos/kmalloc.c b/sos-code-article6.5/sos/kmalloc.c new file mode 100644 index 0000000..62d948d --- /dev/null +++ b/sos-code-article6.5/sos/kmalloc.c @@ -0,0 +1,113 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +#include <sos/assert.h> +#include <sos/macros.h> + +#include "physmem.h" +#include "kmem_vmm.h" +#include "kmem_slab.h" + +#include "kmalloc.h" + +/* The cache structures for these caches, the object size, their + names, and some number of pages that contain them. They might not + necessarily be powers of 2s. */ +static struct { + const char *name; + sos_size_t object_size; + sos_count_t pages_per_slab; + struct sos_kslab_cache *cache; +} kmalloc_cache[] = + { + { "kmalloc 8B objects", 8, 1 }, + { "kmalloc 16B objects", 16, 1 }, + { "kmalloc 32B objects", 32, 1 }, + { "kmalloc 64B objects", 64, 1 }, + { "kmalloc 128B objects", 128, 1 }, + { "kmalloc 256B objects", 256, 2 }, + { "kmalloc 1024B objects", 1024, 2 }, + { "kmalloc 2048B objects", 2048, 3 }, + { "kmalloc 4096B objects", 4096, 4 }, + { "kmalloc 8192B objects", 8192, 8 }, + { "kmalloc 16384B objects", 16384, 12 }, + { NULL, 0, 0, NULL } + }; + + +sos_ret_t sos_kmalloc_subsystem_setup() +{ + int i; + for (i = 0 ; kmalloc_cache[i].object_size != 0 ; i ++) + { + struct sos_kslab_cache *new_cache; + new_cache = sos_kmem_cache_create(kmalloc_cache[i].name, + kmalloc_cache[i].object_size, + kmalloc_cache[i].pages_per_slab, + 0, + SOS_KSLAB_CREATE_MAP + ); + SOS_ASSERT_FATAL(new_cache != NULL); + kmalloc_cache[i].cache = new_cache; + } + return SOS_OK; +} + + +sos_vaddr_t sos_kmalloc(sos_size_t size, sos_ui32_t flags) +{ + /* Look for a suitable pre-allocated kmalloc cache */ + int i; + for (i = 0 ; kmalloc_cache[i].object_size != 0 ; i ++) + { + if (kmalloc_cache[i].object_size >= size) + return sos_kmem_cache_alloc(kmalloc_cache[i].cache, + (flags + & SOS_KMALLOC_ATOMIC)? + SOS_KSLAB_ALLOC_ATOMIC:0); + } + + /* none found yet => we directly use the kmem_vmm subsystem to + allocate whole pages */ + return sos_kmem_vmm_alloc(SOS_PAGE_ALIGN_SUP(size) / SOS_PAGE_SIZE, + ( (flags + & SOS_KMALLOC_ATOMIC)? + SOS_KMEM_VMM_ATOMIC:0) + | SOS_KMEM_VMM_MAP + ); +} + + +sos_ret_t sos_kfree(sos_vaddr_t vaddr) +{ + /* The trouble here is that we aren't sure whether this object is a + slab object in a pre-allocated kmalloc cache, or an object + directly allocated as a kmem_vmm region. */ + + /* We first pretend this object is allocated in a pre-allocated + kmalloc cache */ + if (! sos_kmem_cache_free(vaddr)) + return SOS_OK; /* Great ! We guessed right ! */ + + /* Here we're wrong: it appears not to be an object in a + pre-allocated kmalloc cache. So we try to pretend this is a + kmem_vmm area */ + return sos_kmem_vmm_free(vaddr); +} + + diff --git a/sos-code-article6.5/sos/kmalloc.h b/sos-code-article6.5/sos/kmalloc.h new file mode 100644 index 0000000..3f35b9d --- /dev/null +++ b/sos-code-article6.5/sos/kmalloc.h @@ -0,0 +1,63 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_KMALLOC_H_ +#define _SOS_KMALLOC_H_ + +/** + * @file kmalloc.h + * + * Simple malloc-style wrapper to kmem_vmm.h and kmem_slab.h for + * "anonymous" objects (ie not associated to any precise slab cache). + */ + +#include <sos/types.h> +#include <sos/errno.h> + + +/** + * Iniatilize the kmalloc subsystem, ie pre-allocate a series of caches. + */ +sos_ret_t sos_kmalloc_subsystem_setup(void); + +/* + * sos_kmalloc flags + */ +/** sos_kmalloc() should succeed without blocking, or return NULL */ +#define SOS_KMALLOC_ATOMIC 1 + +/** + * Allocate a kernel object of the given size in the most suited slab + * cache if size can be handled by one of the pre-allocated caches, or + * using directly the range allocator otherwise. The object will + * allways be mapped in physical memory (ie implies + * SOS_KSLAB_CREATE_MAP and SOS_KMEM_VMM_MAP). + * + * @param size The size of the object + * @param flags The allocation flags (SOS_KMALLOC_* flags) + */ +sos_vaddr_t sos_kmalloc(sos_size_t size, sos_ui32_t flags); + +/** + * @note you are perfectly allowed to give the address of the + * kernel image, or the address of the bios area here, it will work: + * the kernel/bios WILL be "deallocated". But if you really want to do + * this, well..., do expect some "surprises" ;) + */ +sos_ret_t sos_kfree(sos_vaddr_t vaddr); + +#endif /* _SOS_KMALLOC_H_ */ diff --git a/sos-code-article6.5/sos/kmem_slab.c b/sos-code-article6.5/sos/kmem_slab.c new file mode 100644 index 0000000..49a1527 --- /dev/null +++ b/sos-code-article6.5/sos/kmem_slab.c @@ -0,0 +1,812 @@ +/* Copyright (C) 2000 Thomas Petazzoni + Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include <sos/macros.h> +#include <sos/klibc.h> +#include <sos/list.h> +#include <sos/assert.h> +#include <hwcore/paging.h> +#include <sos/physmem.h> +#include <sos/kmem_vmm.h> + +#include "kmem_slab.h" + +/* Dimensioning constants */ +#define NB_PAGES_IN_SLAB_OF_CACHES 1 +#define NB_PAGES_IN_SLAB_OF_RANGES 1 + +/** The structure of a slab cache */ +struct sos_kslab_cache +{ + char *name; + + /* non mutable characteristics of this slab */ + sos_size_t original_obj_size; /* asked object size */ + sos_size_t alloc_obj_size; /* actual object size, taking the + alignment constraints into account */ + sos_count_t nb_objects_per_slab; + sos_count_t nb_pages_per_slab; + sos_count_t min_free_objects; + +/* slab cache flags */ +// #define SOS_KSLAB_CREATE_MAP (1<<0) /* See kmem_slab.h */ +// #define SOS_KSLAB_CREATE_ZERO (1<<1) /* " " " " " " " " */ +#define ON_SLAB (1<<31) /* struct sos_kslab is included inside the slab */ + sos_ui32_t flags; + + /* Supervision data (updated at run-time) */ + sos_count_t nb_free_objects; + + /* The lists of slabs owned by this cache */ + struct sos_kslab *slab_list; /* head = non full, tail = full */ + + /* The caches are linked together on the kslab_cache_list */ + struct sos_kslab_cache *prev, *next; +}; + + +/** The structure of a slab */ +struct sos_kslab +{ + /** Number of free objects on this slab */ + sos_count_t nb_free; + + /** The list of these free objects */ + struct sos_kslab_free_object *free; + + /** The address of the associated range structure */ + struct sos_kmem_range *range; + + /** Virtual start address of this range */ + sos_vaddr_t first_object; + + /** Slab cache owning this slab */ + struct sos_kslab_cache *cache; + + /** Links to the other slabs managed by the same cache */ + struct sos_kslab *prev, *next; +}; + + +/** The structure of the free objects in the slab */ +struct sos_kslab_free_object +{ + struct sos_kslab_free_object *prev, *next; +}; + +/** The cache of slab caches */ +static struct sos_kslab_cache *cache_of_struct_kslab_cache; + +/** The cache of slab structures for non-ON_SLAB caches */ +static struct sos_kslab_cache *cache_of_struct_kslab; + +/** The list of slab caches */ +static struct sos_kslab_cache *kslab_cache_list; + +/* Helper function to initialize a cache structure */ +static sos_ret_t +cache_initialize(/*out*/struct sos_kslab_cache *the_cache, + const char* name, + sos_size_t obj_size, + sos_count_t pages_per_slab, + sos_count_t min_free_objs, + sos_ui32_t cache_flags) +{ + unsigned int space_left; + sos_size_t alloc_obj_size; + + if (obj_size <= 0) + return -SOS_EINVAL; + + /* Default allocation size is the requested one */ + alloc_obj_size = obj_size; + + /* Make sure the requested size is large enough to store a + free_object structure */ + if (alloc_obj_size < sizeof(struct sos_kslab_free_object)) + alloc_obj_size = sizeof(struct sos_kslab_free_object); + + /* Align obj_size on 4 bytes */ + alloc_obj_size = SOS_ALIGN_SUP(alloc_obj_size, sizeof(int)); + + /* Make sure supplied number of pages per slab is consistent with + actual allocated object size */ + if (alloc_obj_size > pages_per_slab*SOS_PAGE_SIZE) + return -SOS_EINVAL; + + /* Refuse too large slabs */ + if (pages_per_slab > MAX_PAGES_PER_SLAB) + return -SOS_ENOMEM; + + /* Fills in the cache structure */ + memset(the_cache, 0x0, sizeof(struct sos_kslab_cache)); + the_cache->name = (char*)name; + the_cache->flags = cache_flags; + the_cache->original_obj_size = obj_size; + the_cache->alloc_obj_size = alloc_obj_size; + the_cache->min_free_objects = min_free_objs; + the_cache->nb_pages_per_slab = pages_per_slab; + + /* Small size objets => the slab structure is allocated directly in + the slab */ + if(alloc_obj_size <= sizeof(struct sos_kslab)) + the_cache->flags |= ON_SLAB; + + /* + * Compute the space left once the maximum number of objects + * have been allocated in the slab + */ + space_left = the_cache->nb_pages_per_slab*SOS_PAGE_SIZE; + if(the_cache->flags & ON_SLAB) + space_left -= sizeof(struct sos_kslab); + the_cache->nb_objects_per_slab = space_left / alloc_obj_size; + space_left -= the_cache->nb_objects_per_slab*alloc_obj_size; + + /* Make sure a single slab is large enough to contain the minimum + number of objects requested */ + if (the_cache->nb_objects_per_slab < min_free_objs) + return -SOS_EINVAL; + + /* If there is now enough place for both the objects and the slab + structure, then make the slab structure ON_SLAB */ + if (space_left >= sizeof(struct sos_kslab)) + the_cache->flags |= ON_SLAB; + + return SOS_OK; +} + + +/** Helper function to add a new slab for the given cache. */ +static sos_ret_t +cache_add_slab(struct sos_kslab_cache *kslab_cache, + sos_vaddr_t vaddr_slab, + struct sos_kslab *slab) +{ + int i; + + /* Setup the slab structure */ + memset(slab, 0x0, sizeof(struct sos_kslab)); + slab->cache = kslab_cache; + + /* Establish the address of the first free object */ + slab->first_object = vaddr_slab; + + /* Account for this new slab in the cache */ + slab->nb_free = kslab_cache->nb_objects_per_slab; + kslab_cache->nb_free_objects += slab->nb_free; + + /* Build the list of free objects */ + for (i = 0 ; i < kslab_cache->nb_objects_per_slab ; i++) + { + sos_vaddr_t obj_vaddr; + + /* Set object's address */ + obj_vaddr = slab->first_object + i*kslab_cache->alloc_obj_size; + + /* Add it to the list of free objects */ + list_add_tail(slab->free, + (struct sos_kslab_free_object *)obj_vaddr); + } + + /* Add the slab to the cache's slab list: add the head of the list + since this slab is non full */ + list_add_head(kslab_cache->slab_list, slab); + + return SOS_OK; +} + + +/** Helper function to allocate a new slab for the given kslab_cache */ +static sos_ret_t +cache_grow(struct sos_kslab_cache *kslab_cache, + sos_ui32_t alloc_flags) +{ + sos_ui32_t range_alloc_flags; + + struct sos_kmem_range *new_range; + sos_vaddr_t new_range_start; + + struct sos_kslab *new_slab; + + /* + * Setup the flags for the range allocation + */ + range_alloc_flags = 0; + + /* Atomic ? */ + if (alloc_flags & SOS_KSLAB_ALLOC_ATOMIC) + range_alloc_flags |= SOS_KMEM_VMM_ATOMIC; + + /* Need physical mapping NOW ? */ + if (kslab_cache->flags & (SOS_KSLAB_CREATE_MAP + | SOS_KSLAB_CREATE_ZERO)) + range_alloc_flags |= SOS_KMEM_VMM_MAP; + + /* Allocate the range */ + new_range = sos_kmem_vmm_new_range(kslab_cache->nb_pages_per_slab, + range_alloc_flags, + & new_range_start); + if (! new_range) + return -SOS_ENOMEM; + + /* Allocate the slab structure */ + if (kslab_cache->flags & ON_SLAB) + { + /* Slab structure is ON the slab: simply set its address to the + end of the range */ + sos_vaddr_t slab_vaddr + = new_range_start + kslab_cache->nb_pages_per_slab*SOS_PAGE_SIZE + - sizeof(struct sos_kslab); + new_slab = (struct sos_kslab*)slab_vaddr; + } + else + { + /* Slab structure is OFF the slab: allocate it from the cache of + slab structures */ + sos_vaddr_t slab_vaddr + = sos_kmem_cache_alloc(cache_of_struct_kslab, + alloc_flags); + if (! slab_vaddr) + { + sos_kmem_vmm_del_range(new_range); + return -SOS_ENOMEM; + } + new_slab = (struct sos_kslab*)slab_vaddr; + } + + cache_add_slab(kslab_cache, new_range_start, new_slab); + new_slab->range = new_range; + + /* Set the backlink from range to this slab */ + sos_kmem_vmm_set_slab(new_range, new_slab); + + return SOS_OK; +} + + +/** + * Helper function to release a slab + * + * The corresponding range is always deleted, except when the @param + * must_del_range_now is not set. This happens only when the function + * gets called from sos_kmem_cache_release_struct_range(), to avoid + * large recursions. + */ +static sos_ret_t +cache_release_slab(struct sos_kslab *slab, + sos_bool_t must_del_range_now) +{ + struct sos_kslab_cache *kslab_cache = slab->cache; + struct sos_kmem_range *range = slab->range; + + SOS_ASSERT_FATAL(kslab_cache != NULL); + SOS_ASSERT_FATAL(range != NULL); + SOS_ASSERT_FATAL(slab->nb_free == slab->cache->nb_objects_per_slab); + + /* First, remove the slab from the slabs' list of the cache */ + list_delete(kslab_cache->slab_list, slab); + slab->cache->nb_free_objects -= slab->nb_free; + + /* Release the slab structure if it is OFF slab */ + if (! (slab->cache->flags & ON_SLAB)) + sos_kmem_cache_free((sos_vaddr_t)slab); + + /* Ok, the range is not bound to any slab anymore */ + sos_kmem_vmm_set_slab(range, NULL); + + /* Always delete the range now, unless we are told not to do so (see + sos_kmem_cache_release_struct_range() below) */ + if (must_del_range_now) + return sos_kmem_vmm_del_range(range); + + return SOS_OK; +} + + +/** + * Helper function to create the initial cache of caches, with a very + * first slab in it, so that new cache structures can be simply allocated. + * @return the cache structure for the cache of caches + */ +static struct sos_kslab_cache * +create_cache_of_caches(sos_vaddr_t vaddr_first_slab_of_caches, + int nb_pages) +{ + /* The preliminary cache structure we need in order to allocate the + first slab in the cache of caches (allocated on the stack !) */ + struct sos_kslab_cache fake_cache_of_caches; + + /* The real cache structure for the cache of caches */ + struct sos_kslab_cache *real_cache_of_caches; + + /* The kslab structure for this very first slab */ + struct sos_kslab *slab_of_caches; + + /* Init the cache structure for the cache of caches */ + if (cache_initialize(& fake_cache_of_caches, + "Caches", sizeof(struct sos_kslab_cache), + nb_pages, 0, SOS_KSLAB_CREATE_MAP | ON_SLAB)) + /* Something wrong with the parameters */ + return NULL; + + memset((void*)vaddr_first_slab_of_caches, 0x0, nb_pages*SOS_PAGE_SIZE); + + /* Add the pages for the 1st slab of caches */ + slab_of_caches = (struct sos_kslab*)(vaddr_first_slab_of_caches + + nb_pages*SOS_PAGE_SIZE + - sizeof(struct sos_kslab)); + + /* Add the abovementioned 1st slab to the cache of caches */ + cache_add_slab(& fake_cache_of_caches, + vaddr_first_slab_of_caches, + slab_of_caches); + + /* Now we allocate a cache structure, which will be the real cache + of caches, ie a cache structure allocated INSIDE the cache of + caches, not inside the stack */ + real_cache_of_caches + = (struct sos_kslab_cache*) sos_kmem_cache_alloc(& fake_cache_of_caches, + 0); + /* We initialize it */ + memcpy(real_cache_of_caches, & fake_cache_of_caches, + sizeof(struct sos_kslab_cache)); + /* We need to update the slab's 'cache' field */ + slab_of_caches->cache = real_cache_of_caches; + + /* Add the cache to the list of slab caches */ + list_add_tail(kslab_cache_list, real_cache_of_caches); + + return real_cache_of_caches; +} + + +/** + * Helper function to create the initial cache of ranges, with a very + * first slab in it, so that new kmem_range structures can be simply + * allocated. + * @return the cache of kmem_range + */ +static struct sos_kslab_cache * +create_cache_of_ranges(sos_vaddr_t vaddr_first_slab_of_ranges, + sos_size_t sizeof_struct_range, + int nb_pages) +{ + /* The cache structure for the cache of kmem_range */ + struct sos_kslab_cache *cache_of_ranges; + + /* The kslab structure for the very first slab of ranges */ + struct sos_kslab *slab_of_ranges; + + cache_of_ranges = (struct sos_kslab_cache*) + sos_kmem_cache_alloc(cache_of_struct_kslab_cache, + 0); + if (! cache_of_ranges) + return NULL; + + /* Init the cache structure for the cache of ranges with min objects + per slab = 2 !!! */ + if (cache_initialize(cache_of_ranges, + "struct kmem_range", + sizeof_struct_range, + nb_pages, 2, SOS_KSLAB_CREATE_MAP | ON_SLAB)) + /* Something wrong with the parameters */ + return NULL; + + /* Add the cache to the list of slab caches */ + list_add_tail(kslab_cache_list, cache_of_ranges); + + /* + * Add the first slab for this cache + */ + memset((void*)vaddr_first_slab_of_ranges, 0x0, nb_pages*SOS_PAGE_SIZE); + + /* Add the pages for the 1st slab of ranges */ + slab_of_ranges = (struct sos_kslab*)(vaddr_first_slab_of_ranges + + nb_pages*SOS_PAGE_SIZE + - sizeof(struct sos_kslab)); + + cache_add_slab(cache_of_ranges, + vaddr_first_slab_of_ranges, + slab_of_ranges); + + return cache_of_ranges; +} + + +struct sos_kslab_cache * +sos_kmem_cache_subsystem_setup_prepare(sos_vaddr_t kernel_core_base, + sos_vaddr_t kernel_core_top, + sos_size_t sizeof_struct_range, + /* results */ + struct sos_kslab **first_struct_slab_of_caches, + sos_vaddr_t *first_slab_of_caches_base, + sos_count_t *first_slab_of_caches_nb_pages, + struct sos_kslab **first_struct_slab_of_ranges, + sos_vaddr_t *first_slab_of_ranges_base, + sos_count_t *first_slab_of_ranges_nb_pages) +{ + int i; + sos_ret_t retval; + sos_vaddr_t vaddr; + + /* The cache of ranges we are about to allocate */ + struct sos_kslab_cache *cache_of_ranges; + + /* In the begining, there isn't any cache */ + kslab_cache_list = NULL; + cache_of_struct_kslab = NULL; + cache_of_struct_kslab_cache = NULL; + + /* + * Create the cache of caches, initialised with 1 allocated slab + */ + + /* Allocate the pages needed for the 1st slab of caches, and map them + in kernel space, right after the kernel */ + *first_slab_of_caches_base = SOS_PAGE_ALIGN_SUP(kernel_core_top); + for (i = 0, vaddr = *first_slab_of_caches_base ; + i < NB_PAGES_IN_SLAB_OF_CACHES ; + i++, vaddr += SOS_PAGE_SIZE) + { + sos_paddr_t ppage_paddr; + + ppage_paddr + = sos_physmem_ref_physpage_new(FALSE); + SOS_ASSERT_FATAL(ppage_paddr != (sos_paddr_t)NULL); + + retval = sos_paging_map(ppage_paddr, vaddr, + FALSE, + SOS_VM_MAP_ATOMIC + | SOS_VM_MAP_PROT_READ + | SOS_VM_MAP_PROT_WRITE); + SOS_ASSERT_FATAL(retval == SOS_OK); + + retval = sos_physmem_unref_physpage(ppage_paddr); + SOS_ASSERT_FATAL(retval == FALSE); + } + + /* Create the cache of caches */ + *first_slab_of_caches_nb_pages = NB_PAGES_IN_SLAB_OF_CACHES; + cache_of_struct_kslab_cache + = create_cache_of_caches(*first_slab_of_caches_base, + NB_PAGES_IN_SLAB_OF_CACHES); + SOS_ASSERT_FATAL(cache_of_struct_kslab_cache != NULL); + + /* Retrieve the slab that should have been allocated */ + *first_struct_slab_of_caches + = list_get_head(cache_of_struct_kslab_cache->slab_list); + + + /* + * Create the cache of ranges, initialised with 1 allocated slab + */ + *first_slab_of_ranges_base = vaddr; + /* Allocate the 1st slab */ + for (i = 0, vaddr = *first_slab_of_ranges_base ; + i < NB_PAGES_IN_SLAB_OF_RANGES ; + i++, vaddr += SOS_PAGE_SIZE) + { + sos_paddr_t ppage_paddr; + + ppage_paddr + = sos_physmem_ref_physpage_new(FALSE); + SOS_ASSERT_FATAL(ppage_paddr != (sos_paddr_t)NULL); + + retval = sos_paging_map(ppage_paddr, vaddr, + FALSE, + SOS_VM_MAP_ATOMIC + | SOS_VM_MAP_PROT_READ + | SOS_VM_MAP_PROT_WRITE); + SOS_ASSERT_FATAL(retval == SOS_OK); + + retval = sos_physmem_unref_physpage(ppage_paddr); + SOS_ASSERT_FATAL(retval == FALSE); + } + + /* Create the cache of ranges */ + *first_slab_of_ranges_nb_pages = NB_PAGES_IN_SLAB_OF_RANGES; + cache_of_ranges = create_cache_of_ranges(*first_slab_of_ranges_base, + sizeof_struct_range, + NB_PAGES_IN_SLAB_OF_RANGES); + SOS_ASSERT_FATAL(cache_of_ranges != NULL); + + /* Retrieve the slab that should have been allocated */ + *first_struct_slab_of_ranges + = list_get_head(cache_of_ranges->slab_list); + + /* + * Create the cache of slabs, without any allocated slab yet + */ + cache_of_struct_kslab + = sos_kmem_cache_create("off-slab slab structures", + sizeof(struct sos_kslab), + 1, + 0, + SOS_KSLAB_CREATE_MAP); + SOS_ASSERT_FATAL(cache_of_struct_kslab != NULL); + + return cache_of_ranges; +} + + +sos_ret_t +sos_kmem_cache_subsystem_setup_commit(struct sos_kslab *first_struct_slab_of_caches, + struct sos_kmem_range *first_range_of_caches, + struct sos_kslab *first_struct_slab_of_ranges, + struct sos_kmem_range *first_range_of_ranges) +{ + first_struct_slab_of_caches->range = first_range_of_caches; + first_struct_slab_of_ranges->range = first_range_of_ranges; + return SOS_OK; +} + + +struct sos_kslab_cache * +sos_kmem_cache_create(const char* name, + sos_size_t obj_size, + sos_count_t pages_per_slab, + sos_count_t min_free_objs, + sos_ui32_t cache_flags) +{ + struct sos_kslab_cache *new_cache; + + /* Allocate the new cache */ + new_cache = (struct sos_kslab_cache*) + sos_kmem_cache_alloc(cache_of_struct_kslab_cache, + 0/* NOT ATOMIC */); + if (! new_cache) + return NULL; + + if (cache_initialize(new_cache, name, obj_size, + pages_per_slab, min_free_objs, + cache_flags)) + { + /* Something was wrong */ + sos_kmem_cache_free((sos_vaddr_t)new_cache); + return NULL; + } + + /* Add the cache to the list of slab caches */ + list_add_tail(kslab_cache_list, new_cache); + + /* if the min_free_objs is set, pre-allocate a slab */ + if (min_free_objs) + { + if (cache_grow(new_cache, 0 /* Not atomic */) != SOS_OK) + { + sos_kmem_cache_destroy(new_cache); + return NULL; /* Not enough memory */ + } + } + + return new_cache; +} + + +sos_ret_t sos_kmem_cache_destroy(struct sos_kslab_cache *kslab_cache) +{ + int nb_slabs; + struct sos_kslab *slab; + + if (! kslab_cache) + return -SOS_EINVAL; + + /* Refuse to destroy the cache if there are any objects still + allocated */ + list_foreach(kslab_cache->slab_list, slab, nb_slabs) + { + if (slab->nb_free != kslab_cache->nb_objects_per_slab) + return -SOS_EBUSY; + } + + /* Remove all the slabs */ + while ((slab = list_get_head(kslab_cache->slab_list)) != NULL) + { + cache_release_slab(slab, TRUE); + } + + /* Remove the cache */ + return sos_kmem_cache_free((sos_vaddr_t)kslab_cache); +} + + +sos_vaddr_t sos_kmem_cache_alloc(struct sos_kslab_cache *kslab_cache, + sos_ui32_t alloc_flags) +{ + sos_vaddr_t obj_vaddr; + struct sos_kslab * slab_head; +#define ALLOC_RET return + + /* If the slab at the head of the slabs' list has no free object, + then the other slabs don't either => need to allocate a new + slab */ + if ((! kslab_cache->slab_list) + || (! list_get_head(kslab_cache->slab_list)->free)) + { + if (cache_grow(kslab_cache, alloc_flags) != SOS_OK) + /* Not enough memory or blocking alloc */ + ALLOC_RET( (sos_vaddr_t)NULL); + } + + /* Here: we are sure that list_get_head(kslab_cache->slab_list) + exists *AND* that list_get_head(kslab_cache->slab_list)->free is + NOT NULL */ + slab_head = list_get_head(kslab_cache->slab_list); + SOS_ASSERT_FATAL(slab_head != NULL); + + /* Allocate the object at the head of the slab at the head of the + slabs' list */ + obj_vaddr = (sos_vaddr_t)list_pop_head(slab_head->free); + slab_head->nb_free --; + kslab_cache->nb_free_objects --; + + /* If needed, reset object's contents */ + if (kslab_cache->flags & SOS_KSLAB_CREATE_ZERO) + memset((void*)obj_vaddr, 0x0, kslab_cache->alloc_obj_size); + + /* Slab is now full ? */ + if (slab_head->free == NULL) + { + /* Transfer it at the tail of the slabs' list */ + struct sos_kslab *slab; + slab = list_pop_head(kslab_cache->slab_list); + list_add_tail(kslab_cache->slab_list, slab); + } + + /* + * For caches that require a minimum amount of free objects left, + * allocate a slab if needed. + * + * Notice the "== min_objects - 1": we did not write " < + * min_objects" because for the cache of kmem structure, this would + * lead to an chicken-and-egg problem, since cache_grow below would + * call cache_alloc again for the kmem_vmm cache, so we return here + * with the same cache. If the test were " < min_objects", then we + * would call cache_grow again for the kmem_vmm cache again and + * again... until we reach the bottom of our stack (infinite + * recursion). By telling precisely "==", then the cache_grow would + * only be called the first time. + */ + if ((kslab_cache->min_free_objects > 0) + && (kslab_cache->nb_free_objects == (kslab_cache->min_free_objects - 1))) + { + /* No: allocate a new slab now */ + if (cache_grow(kslab_cache, alloc_flags) != SOS_OK) + { + /* Not enough free memory or blocking alloc => undo the + allocation */ + sos_kmem_cache_free(obj_vaddr); + ALLOC_RET( (sos_vaddr_t)NULL); + } + } + + ALLOC_RET(obj_vaddr); +} + + +/** + * Helper function to free the object located at the given address. + * + * @param empty_slab is the address of the slab to release, if removing + * the object causes the slab to become empty. + */ +inline static +sos_ret_t +free_object(sos_vaddr_t vaddr, + struct sos_kslab ** empty_slab) +{ + struct sos_kslab_cache *kslab_cache; + + /* Lookup the slab containing the object in the slabs' list */ + struct sos_kslab *slab = sos_kmem_vmm_resolve_slab(vaddr); + + /* By default, consider that the slab will not become empty */ + *empty_slab = NULL; + + /* Did not find the slab */ + if (! slab) + return -SOS_EINVAL; + + SOS_ASSERT_FATAL(slab->cache); + kslab_cache = slab->cache; + + /* + * Check whether the address really could mark the start of an actual + * allocated object + */ + /* Address multiple of an object's size ? */ + if (( (vaddr - slab->first_object) + % kslab_cache->alloc_obj_size) != 0) + return -SOS_EINVAL; + /* Address not too large ? */ + if (( (vaddr - slab->first_object) + / kslab_cache->alloc_obj_size) >= kslab_cache->nb_objects_per_slab) + return -SOS_EINVAL; + + /* + * Ok: we now release the object + */ + + /* Did find a full slab => will not be full any more => move it + to the head of the slabs' list */ + if (! slab->free) + { + list_delete(kslab_cache->slab_list, slab); + list_add_head(kslab_cache->slab_list, slab); + } + + /* Release the object */ + list_add_head(slab->free, (struct sos_kslab_free_object*)vaddr); + slab->nb_free++; + kslab_cache->nb_free_objects++; + SOS_ASSERT_FATAL(slab->nb_free <= slab->cache->nb_objects_per_slab); + + /* Cause the slab to be released if it becomes empty, and if we are + allowed to do it */ + if ((slab->nb_free >= kslab_cache->nb_objects_per_slab) + && (kslab_cache->nb_free_objects - slab->nb_free + >= kslab_cache->min_free_objects)) + { + *empty_slab = slab; + } + + return SOS_OK; +} + + +sos_ret_t sos_kmem_cache_free(sos_vaddr_t vaddr) +{ + sos_ret_t retval; + struct sos_kslab *empty_slab; + + /* Remove the object from the slab */ + retval = free_object(vaddr, & empty_slab); + if (retval != SOS_OK) + return retval; + + /* Remove the slab and the underlying range if needed */ + if (empty_slab != NULL) + return cache_release_slab(empty_slab, TRUE); + + return SOS_OK; +} + + +struct sos_kmem_range * +sos_kmem_cache_release_struct_range(struct sos_kmem_range *the_range) +{ + sos_ret_t retval; + struct sos_kslab *empty_slab; + + /* Remove the object from the slab */ + retval = free_object((sos_vaddr_t)the_range, & empty_slab); + if (retval != SOS_OK) + return NULL; + + /* Remove the slab BUT NOT the underlying range if needed */ + if (empty_slab != NULL) + { + struct sos_kmem_range *empty_range = empty_slab->range; + SOS_ASSERT_FATAL(cache_release_slab(empty_slab, FALSE) == SOS_OK); + SOS_ASSERT_FATAL(empty_range != NULL); + return empty_range; + } + + return NULL; +} + diff --git a/sos-code-article6.5/sos/kmem_slab.h b/sos-code-article6.5/sos/kmem_slab.h new file mode 100644 index 0000000..1f28ff9 --- /dev/null +++ b/sos-code-article6.5/sos/kmem_slab.h @@ -0,0 +1,206 @@ +/* Copyright (C) 2000 Thomas Petazzoni + Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_KMEM_SLAB_H_ +#define _SOS_KMEM_SLAB_H_ + +/** + * @file kmem_slab.h + * + * Kernel Memory Allocator based on Bonwick's slab llocator (Solaris + * 2.4, Linux 2.4). This allocator achieves good memory utilization + * ratio (memory effectively used / memory requested) ie limited + * fragmentation, while elegantly handling cache-effect considerations + * (TLB locality through the notion of "cache" of slabs, and the + * dcache utilization through the notion of cache colouring to + * decrease the conflicts in the dcache for accesses to different data + * in the same cache). + * + * This allocator relies on the range allocator (kmem_vmm.h) to + * allocate the slabs, which itself relies on the slab allocator to + * allocate its "range" data structures, thus leading to a + * chicken-and-egg problem. We solve this problem by introducing the + * notion of "min_free_objs" for the slab caches, in order for the cache + * of ranges to always have enough ranges in reserve to complete the + * range allocation before being urged to allocate a new slab of + * ranges, which would require the allocation of a new range. + * + * Compared to Bonwick's recommendations, we don't handle ctor/dtor + * routines on the objects, so that we can alter the objects once they + * are set free. Thus, the list of free object is stored in the free + * objects themselves, not alongside the objects (this also implies that + * the SOS_KSLAB_CREATE_MAP flag below is meaningless). We also don't + * implement the cache colouring (trivial to add, but we omit it for + * readability reasons), and the only alignment constraint we respect + * is that allocated objects are aligned on a 4B boundary: for other + * alignment constraints, the user must integrate them in the + * "object_size" parameter to "sos_kmem_cache_create()". + * + * References : + * - J. Bonwick's paper, "The slab allocator: An object-caching kernel + * memory allocator", In USENIX Summer 1994 Technical Conference + * - The bible, aka "Unix internals : the new frontiers" (section + * 12.10), Uresh Vahalia, Prentice Hall 1996, ISBN 0131019082 + * - "The Linux slab allocator", B. Fitzgibbons, + * http://www.cc.gatech.edu/people/home/bradf/cs7001/proj2/ + * - The Kos, http://kos.enix.org/ + */ +#include <sos/types.h> +#include <sos/errno.h> + +/** Opaque data structure that defines a Cache of slabs */ +struct sos_kslab_cache; + +/** Opaque data structure that defines a slab. Exported only to + kmem_vmm.h */ +struct sos_kslab; + +#include "kmem_vmm.h" + + +/** The maximum allowed pages for each slab */ +#define MAX_PAGES_PER_SLAB 32 /* 128 kB */ + + +/** + * Initialize the slab cache of slab caches, and prepare the cache of + * kmem_range for kmem_vmm. + * + * @param kernel_core_base The virtual address of the first byte used + * by the kernel code/data + * + * @param kernel_core_top The virtual address of the first byte after + * the kernel code/data. + * + * @param sizeof_struct_range the size of the objects (aka "struct + * sos_kmem_vmm_ranges") to be allocated in the cache of ranges + * + * @param first_struct_slab_of_caches (output value) the virtual + * address of the first slab structure that gets allocated for the + * cache of caches. The function actually manually allocate the first + * slab of the cache of caches because of a chicken-and-egg thing. The + * address of the slab is used by the kmem_vmm_setup routine to + * finalize the allocation of the slab, in order for it to behave like + * a real slab afterwards. + * + * @param first_slab_of_caches_base (output value) the virtual address + * of the slab associated to the slab structure. + * + * @param first_slab_of_caches_nb_pages (output value) the number of + * (virtual) pages used by the first slab of the cache of caches. + * + * @param first_struct_slab_of_ranges (output value) the virtual address + * of the first slab that gets allocated for the cache of ranges. Same + * explanation as above. + * + * @param first_slab_of_ranges_base (output value) the virtual address + * of the slab associated to the slab structure. + * + * @param first_slab_of_ranges_nb_pages (output value) the number of + * (virtual) pages used by the first slab of the cache of ranges. + * + * @return the cache of kmem_range immediatly usable + */ +struct sos_kslab_cache * +sos_kmem_cache_subsystem_setup_prepare(sos_vaddr_t kernel_core_base, + sos_vaddr_t kernel_core_top, + sos_size_t sizeof_struct_range, + /* results */ + struct sos_kslab **first_struct_slab_of_caches, + sos_vaddr_t *first_slab_of_caches_base, + sos_count_t *first_slab_of_caches_nb_pages, + struct sos_kslab **first_struct_slab_of_ranges, + sos_vaddr_t *first_slab_of_ranges_base, + sos_count_t *first_slab_of_ranges_nb_pages); + +/** + * Update the configuration of the cache subsystem once the vmm + * subsystem has been fully initialized + */ +sos_ret_t +sos_kmem_cache_subsystem_setup_commit(struct sos_kslab *first_struct_slab_of_caches, + struct sos_kmem_range *first_range_of_caches, + struct sos_kslab *first_struct_slab_of_ranges, + struct sos_kmem_range *first_range_of_ranges); + + +/* + * Flags for sos_kmem_cache_create() + */ +/** The slabs should be initially mapped in physical memory */ +#define SOS_KSLAB_CREATE_MAP (1<<0) +/** The object should always be set to zero at allocation (implies + SOS_KSLAB_CREATE_MAP) */ +#define SOS_KSLAB_CREATE_ZERO (1<<1) + +/** + * @note this function MAY block (involved allocations are not atomic) + * @param name must remain valid during the whole cache's life + * (shallow copy) ! + * @param cache_flags An or-ed combination of the SOS_KSLAB_CREATE_* flags + */ +struct sos_kslab_cache * +sos_kmem_cache_create(const char* name, + sos_size_t object_size, + sos_count_t pages_per_slab, + sos_count_t min_free_objects, + sos_ui32_t cache_flags); + +sos_ret_t sos_kmem_cache_destroy(struct sos_kslab_cache *kslab_cache); + + +/* + * Flags for sos_kmem_cache_alloc() + */ +/** Allocation should either succeed or fail, without blocking */ +#define SOS_KSLAB_ALLOC_ATOMIC (1<<0) + +/** + * Allocate an object from the given cache. + * + * @param alloc_flags An or-ed combination of the SOS_KSLAB_ALLOC_* flags + */ +sos_vaddr_t sos_kmem_cache_alloc(struct sos_kslab_cache *kslab_cache, + sos_ui32_t alloc_flags); + + +/** + * Free an object (assumed to be already allocated and not already + * free) at the given virtual address. + */ +sos_ret_t sos_kmem_cache_free(sos_vaddr_t vaddr); + + +/* + * Function reserved to kmem_vmm.c. Does almost everything + * sos_kmem_cache_free() does, except it does not call + * sos_kmem_vmm_del_range() if it needs to. This is aimed at avoiding + * large recursion when a range is freed with + * sos_kmem_vmm_del_range(). + * + * @param the_range The range structure to free + * + * @return NULL when the range containing 'the_range' still contains + * other ranges, or the address of the range which owned 'the_range' + * if it becomes empty. + */ +struct sos_kmem_range * +sos_kmem_cache_release_struct_range(struct sos_kmem_range *the_range); + + +#endif /* _SOS_KMEM_SLAB_H_ */ diff --git a/sos-code-article6.5/sos/kmem_vmm.c b/sos-code-article6.5/sos/kmem_vmm.c new file mode 100644 index 0000000..ea2fdf1 --- /dev/null +++ b/sos-code-article6.5/sos/kmem_vmm.c @@ -0,0 +1,606 @@ +/* Copyright (C) 2000 Thomas Petazzoni + Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +#include <sos/list.h> +#include <sos/physmem.h> +#include <hwcore/paging.h> +#include <sos/assert.h> + +#include "kmem_vmm.h" + +/** The structure of a range of kernel-space virtual addresses */ +struct sos_kmem_range +{ + sos_vaddr_t base_vaddr; + sos_count_t nb_pages; + + /* The slab owning this range, or NULL */ + struct sos_kslab *slab; + + struct sos_kmem_range *prev, *next; +}; +const int sizeof_struct_sos_kmem_range = sizeof(struct sos_kmem_range); + +/** The ranges are SORTED in (strictly) ascending base addresses */ +static struct sos_kmem_range *kmem_free_range_list, *kmem_used_range_list; + +/** The slab cache for the kmem ranges */ +static struct sos_kslab_cache *kmem_range_cache; + + + +/** Helper function to get the closest preceding or containing + range for the given virtual address */ +static struct sos_kmem_range * +get_closest_preceding_kmem_range(struct sos_kmem_range *the_list, + sos_vaddr_t vaddr) +{ + int nb_elements; + struct sos_kmem_range *a_range, *ret_range; + + /* kmem_range list is kept SORTED, so we exit as soon as vaddr >= a + range base address */ + ret_range = NULL; + list_foreach(the_list, a_range, nb_elements) + { + if (vaddr < a_range->base_vaddr) + return ret_range; + ret_range = a_range; + } + + /* This will always be the LAST range in the kmem area */ + return ret_range; +} + + +/** + * Helper function to lookup a free range large enough to hold nb_pages + * pages (first fit) + */ +static struct sos_kmem_range *find_suitable_free_range(sos_count_t nb_pages) +{ + int nb_elements; + struct sos_kmem_range *r; + + list_foreach(kmem_free_range_list, r, nb_elements) + { + if (r->nb_pages >= nb_pages) + return r; + } + + return NULL; +} + + +/** + * Helper function to add a_range in the_list, in strictly ascending order. + * + * @return The (possibly) new head of the_list + */ +static struct sos_kmem_range *insert_range(struct sos_kmem_range *the_list, + struct sos_kmem_range *a_range) +{ + struct sos_kmem_range *prec_used; + + /** Look for any preceding range */ + prec_used = get_closest_preceding_kmem_range(the_list, + a_range->base_vaddr); + /** insert a_range /after/ this prec_used */ + if (prec_used != NULL) + list_insert_after(the_list, prec_used, a_range); + else /* Insert at the beginning of the list */ + list_add_head(the_list, a_range); + + return the_list; +} + + +/** + * Helper function to retrieve the range owning the given vaddr, by + * scanning the physical memory first if vaddr is mapped in RAM + */ +static struct sos_kmem_range *lookup_range(sos_vaddr_t vaddr) +{ + struct sos_kmem_range *range; + + /* First: try to retrieve the physical page mapped at this address */ + sos_paddr_t ppage_paddr = SOS_PAGE_ALIGN_INF(sos_paging_get_paddr(vaddr)); + + if (ppage_paddr) + { + range = sos_physmem_get_kmem_range(ppage_paddr); + + /* If a page is mapped at this address, it is EXPECTED that it + is really associated with a range */ + SOS_ASSERT_FATAL(range != NULL); + } + + /* Otherwise scan the list of used ranges, looking for the range + owning the address */ + else + { + range = get_closest_preceding_kmem_range(kmem_used_range_list, + vaddr); + /* Not found */ + if (! range) + return NULL; + + /* vaddr not covered by this range */ + if ( (vaddr < range->base_vaddr) + || (vaddr >= (range->base_vaddr + range->nb_pages*SOS_PAGE_SIZE)) ) + return NULL; + } + + return range; +} + + +/** + * Helper function for sos_kmem_vmm_setup() to initialize a new range + * that maps a given area as free or as already used. + * This function either succeeds or halts the whole system. + */ +static struct sos_kmem_range * +create_range(sos_bool_t is_free, + sos_vaddr_t base_vaddr, + sos_vaddr_t top_vaddr, + struct sos_kslab *associated_slab) +{ + struct sos_kmem_range *range; + + SOS_ASSERT_FATAL(SOS_IS_PAGE_ALIGNED(base_vaddr)); + SOS_ASSERT_FATAL(SOS_IS_PAGE_ALIGNED(top_vaddr)); + + if ((top_vaddr - base_vaddr) < SOS_PAGE_SIZE) + return NULL; + + range = (struct sos_kmem_range*)sos_kmem_cache_alloc(kmem_range_cache, + SOS_KSLAB_ALLOC_ATOMIC); + SOS_ASSERT_FATAL(range != NULL); + + range->base_vaddr = base_vaddr; + range->nb_pages = (top_vaddr - base_vaddr) / SOS_PAGE_SIZE; + + if (is_free) + { + list_add_tail(kmem_free_range_list, + range); + } + else + { + sos_vaddr_t vaddr; + range->slab = associated_slab; + list_add_tail(kmem_used_range_list, + range); + + /* Ok, set the range owner for the pages in this page */ + for (vaddr = base_vaddr ; + vaddr < top_vaddr ; + vaddr += SOS_PAGE_SIZE) + { + sos_paddr_t ppage_paddr = sos_paging_get_paddr(vaddr); + SOS_ASSERT_FATAL((void*)ppage_paddr != NULL); + sos_physmem_set_kmem_range(ppage_paddr, range); + } + } + + return range; +} + + +sos_ret_t +sos_kmem_vmm_subsystem_setup(sos_vaddr_t kernel_core_base, + sos_vaddr_t kernel_core_top, + sos_vaddr_t bootstrap_stack_bottom_vaddr, + sos_vaddr_t bootstrap_stack_top_vaddr) +{ + struct sos_kslab *first_struct_slab_of_caches, + *first_struct_slab_of_ranges; + sos_vaddr_t first_slab_of_caches_base, + first_slab_of_caches_nb_pages, + first_slab_of_ranges_base, + first_slab_of_ranges_nb_pages; + struct sos_kmem_range *first_range_of_caches, + *first_range_of_ranges; + + list_init(kmem_free_range_list); + list_init(kmem_used_range_list); + + kmem_range_cache + = sos_kmem_cache_subsystem_setup_prepare(kernel_core_base, + kernel_core_top, + sizeof(struct sos_kmem_range), + & first_struct_slab_of_caches, + & first_slab_of_caches_base, + & first_slab_of_caches_nb_pages, + & first_struct_slab_of_ranges, + & first_slab_of_ranges_base, + & first_slab_of_ranges_nb_pages); + SOS_ASSERT_FATAL(kmem_range_cache != NULL); + + /* Mark virtual addresses 16kB - Video as FREE */ + create_range(TRUE, + SOS_KMEM_VMM_BASE, + SOS_PAGE_ALIGN_INF(BIOS_N_VIDEO_START), + NULL); + + /* Mark virtual addresses in Video hardware mapping as NOT FREE */ + create_range(FALSE, + SOS_PAGE_ALIGN_INF(BIOS_N_VIDEO_START), + SOS_PAGE_ALIGN_SUP(BIOS_N_VIDEO_END), + NULL); + + /* Mark virtual addresses Video - Kernel as FREE */ + create_range(TRUE, + SOS_PAGE_ALIGN_SUP(BIOS_N_VIDEO_END), + SOS_PAGE_ALIGN_INF(kernel_core_base), + NULL); + + /* Mark virtual addresses in Kernel code/data up to the bootstrap stack + as NOT FREE */ + create_range(FALSE, + SOS_PAGE_ALIGN_INF(kernel_core_base), + bootstrap_stack_bottom_vaddr, + NULL); + + /* Mark virtual addresses in the bootstrap stack as NOT FREE too, + but in another vmm region in order to be un-allocated later */ + create_range(FALSE, + bootstrap_stack_bottom_vaddr, + bootstrap_stack_top_vaddr, + NULL); + + /* Mark the remaining virtual addresses in Kernel code/data after + the bootstrap stack as NOT FREE */ + create_range(FALSE, + bootstrap_stack_top_vaddr, + SOS_PAGE_ALIGN_SUP(kernel_core_top), + NULL); + + /* Mark virtual addresses in the first slab of the cache of caches + as NOT FREE */ + SOS_ASSERT_FATAL(SOS_PAGE_ALIGN_SUP(kernel_core_top) + == first_slab_of_caches_base); + SOS_ASSERT_FATAL(first_struct_slab_of_caches != NULL); + first_range_of_caches + = create_range(FALSE, + first_slab_of_caches_base, + first_slab_of_caches_base + + first_slab_of_caches_nb_pages*SOS_PAGE_SIZE, + first_struct_slab_of_caches); + + /* Mark virtual addresses in the first slab of the cache of ranges + as NOT FREE */ + SOS_ASSERT_FATAL((first_slab_of_caches_base + + first_slab_of_caches_nb_pages*SOS_PAGE_SIZE) + == first_slab_of_ranges_base); + SOS_ASSERT_FATAL(first_struct_slab_of_ranges != NULL); + first_range_of_ranges + = create_range(FALSE, + first_slab_of_ranges_base, + first_slab_of_ranges_base + + first_slab_of_ranges_nb_pages*SOS_PAGE_SIZE, + first_struct_slab_of_ranges); + + /* Mark virtual addresses after these slabs as FREE */ + create_range(TRUE, + first_slab_of_ranges_base + + first_slab_of_ranges_nb_pages*SOS_PAGE_SIZE, + SOS_KMEM_VMM_TOP, + NULL); + + /* Update the cache subsystem so that the artificially-created + caches of caches and ranges really behave like *normal* caches (ie + those allocated by the normal slab API) */ + sos_kmem_cache_subsystem_setup_commit(first_struct_slab_of_caches, + first_range_of_caches, + first_struct_slab_of_ranges, + first_range_of_ranges); + + return SOS_OK; +} + + +/** + * Allocate a new kernel area spanning one or multiple pages. + * + * @eturn a new range structure + */ +struct sos_kmem_range *sos_kmem_vmm_new_range(sos_count_t nb_pages, + sos_ui32_t flags, + sos_vaddr_t * range_start) +{ + struct sos_kmem_range *free_range, *new_range; + + if (nb_pages <= 0) + return NULL; + + /* Find a suitable free range to hold the size-sized object */ + free_range = find_suitable_free_range(nb_pages); + if (free_range == NULL) + return NULL; + + /* If range has exactly the requested size, just move it to the + "used" list */ + if(free_range->nb_pages == nb_pages) + { + list_delete(kmem_free_range_list, free_range); + kmem_used_range_list = insert_range(kmem_used_range_list, + free_range); + /* The new_range is exactly the free_range */ + new_range = free_range; + } + + /* Otherwise the range is bigger than the requested size, split it. + This involves reducing its size, and allocate a new range, which + is going to be added to the "used" list */ + else + { + /* free_range split in { new_range | free_range } */ + new_range = (struct sos_kmem_range*) + sos_kmem_cache_alloc(kmem_range_cache, + (flags & SOS_KMEM_VMM_ATOMIC)? + SOS_KSLAB_ALLOC_ATOMIC:0); + if (! new_range) + return NULL; + + new_range->base_vaddr = free_range->base_vaddr; + new_range->nb_pages = nb_pages; + free_range->base_vaddr += nb_pages*SOS_PAGE_SIZE; + free_range->nb_pages -= nb_pages; + + /* free_range is still at the same place in the list */ + /* insert new_range in the used list */ + kmem_used_range_list = insert_range(kmem_used_range_list, + new_range); + } + + /* By default, the range is not associated with any slab */ + new_range->slab = NULL; + + /* If mapping of physical pages is needed, map them now */ + if (flags & SOS_KMEM_VMM_MAP) + { + int i; + for (i = 0 ; i < nb_pages ; i ++) + { + /* Get a new physical page */ + sos_paddr_t ppage_paddr + = sos_physmem_ref_physpage_new(! (flags & SOS_KMEM_VMM_ATOMIC)); + + /* Map the page in kernel space */ + if (ppage_paddr) + { + if (sos_paging_map(ppage_paddr, + new_range->base_vaddr + + i * SOS_PAGE_SIZE, + FALSE /* Not a user page */, + ((flags & SOS_KMEM_VMM_ATOMIC)? + SOS_VM_MAP_ATOMIC:0) + | SOS_VM_MAP_PROT_READ + | SOS_VM_MAP_PROT_WRITE)) + { + /* Failed => force unallocation, see below */ + sos_physmem_unref_physpage(ppage_paddr); + ppage_paddr = (sos_paddr_t)NULL; + } + else + { + /* Success : page can be unreferenced since it is + now mapped */ + sos_physmem_unref_physpage(ppage_paddr); + } + } + + /* Undo the allocation if failed to allocate or map a new page */ + if (! ppage_paddr) + { + sos_kmem_vmm_del_range(new_range); + return NULL; + } + + /* Ok, set the range owner for this page */ + sos_physmem_set_kmem_range(ppage_paddr, new_range); + } + } + /* ... Otherwise: Demand Paging will do the job */ + + if (range_start) + *range_start = new_range->base_vaddr; + + return new_range; +} + + +sos_ret_t sos_kmem_vmm_del_range(struct sos_kmem_range *range) +{ + int i; + struct sos_kmem_range *ranges_to_free; + list_init(ranges_to_free); + + SOS_ASSERT_FATAL(range != NULL); + SOS_ASSERT_FATAL(range->slab == NULL); + + /* Remove the range from the 'USED' list now */ + list_delete(kmem_used_range_list, range); + + /* + * The following do..while() loop is here to avoid an indirect + * recursion: if we call directly kmem_cache_free() from inside the + * current function, we take the risk to re-enter the current function + * (sos_kmem_vmm_del_range()) again, which may cause problem if it + * in turn calls kmem_slab again and sos_kmem_vmm_del_range again, + * and again and again. This may happen while freeing ranges of + * struct sos_kslab... + * + * To avoid this,we choose to call a special function of kmem_slab + * doing almost the same as sos_kmem_cache_free(), but which does + * NOT call us (ie sos_kmem_vmm_del_range()): instead WE add the + * range that is to be freed to a list, and the do..while() loop is + * here to process this list ! The recursion is replaced by + * classical iterations. + */ + do + { + /* Ok, we got the range. Now, insert this range in the free list */ + kmem_free_range_list = insert_range(kmem_free_range_list, range); + + /* Unmap the physical pages */ + for (i = 0 ; i < range->nb_pages ; i ++) + { + /* This will work even if no page is mapped at this address */ + sos_paging_unmap(range->base_vaddr + i*SOS_PAGE_SIZE); + } + + /* Eventually coalesce it with prev/next free ranges (there is + always a valid prev/next link since the list is circular). Note: + the tests below will lead to correct behaviour even if the list + is limited to the 'range' singleton, at least as long as the + range is not zero-sized */ + /* Merge with preceding one ? */ + if (range->prev->base_vaddr + range->prev->nb_pages*SOS_PAGE_SIZE + == range->base_vaddr) + { + struct sos_kmem_range *empty_range_of_ranges = NULL; + struct sos_kmem_range *prec_free = range->prev; + + /* Merge them */ + prec_free->nb_pages += range->nb_pages; + list_delete(kmem_free_range_list, range); + + /* Mark the range as free. This may cause the slab owning + the range to become empty */ + empty_range_of_ranges = + sos_kmem_cache_release_struct_range(range); + + /* If this causes the slab owning the range to become empty, + add the range corresponding to the slab at the end of the + list of the ranges to be freed: it will be actually freed + in one of the next iterations of the do{} loop. */ + if (empty_range_of_ranges != NULL) + { + list_delete(kmem_used_range_list, empty_range_of_ranges); + list_add_tail(ranges_to_free, empty_range_of_ranges); + } + + /* Set range to the beginning of this coelescion */ + range = prec_free; + } + + /* Merge with next one ? [NO 'else' since range may be the result of + the merge above] */ + if (range->base_vaddr + range->nb_pages*SOS_PAGE_SIZE + == range->next->base_vaddr) + { + struct sos_kmem_range *empty_range_of_ranges = NULL; + struct sos_kmem_range *next_range = range->next; + + /* Merge them */ + range->nb_pages += next_range->nb_pages; + list_delete(kmem_free_range_list, next_range); + + /* Mark the next_range as free. This may cause the slab + owning the next_range to become empty */ + empty_range_of_ranges = + sos_kmem_cache_release_struct_range(next_range); + + /* If this causes the slab owning the next_range to become + empty, add the range corresponding to the slab at the end + of the list of the ranges to be freed: it will be + actually freed in one of the next iterations of the + do{} loop. */ + if (empty_range_of_ranges != NULL) + { + list_delete(kmem_used_range_list, empty_range_of_ranges); + list_add_tail(ranges_to_free, empty_range_of_ranges); + } + } + + + /* If deleting the range(s) caused one or more range(s) to be + freed, get the next one to free */ + if (list_is_empty(ranges_to_free)) + range = NULL; /* No range left to free */ + else + range = list_pop_head(ranges_to_free); + + } + /* Stop when there is no range left to be freed for now */ + while (range != NULL); + + return SOS_OK; +} + + +sos_vaddr_t sos_kmem_vmm_alloc(sos_count_t nb_pages, + sos_ui32_t flags) +{ + struct sos_kmem_range *range + = sos_kmem_vmm_new_range(nb_pages, + flags, + NULL); + if (! range) + return (sos_vaddr_t)NULL; + + return range->base_vaddr; +} + + +sos_ret_t sos_kmem_vmm_free(sos_vaddr_t vaddr) +{ + struct sos_kmem_range *range = lookup_range(vaddr); + + /* We expect that the given address is the base address of the + range */ + if (!range || (range->base_vaddr != vaddr)) + return -SOS_EINVAL; + + /* We expect that this range is not held by any cache */ + if (range->slab != NULL) + return -SOS_EBUSY; + + return sos_kmem_vmm_del_range(range); +} + + +sos_ret_t sos_kmem_vmm_set_slab(struct sos_kmem_range *range, + struct sos_kslab *slab) +{ + if (! range) + return -SOS_EINVAL; + + range->slab = slab; + return SOS_OK; +} + +struct sos_kslab * sos_kmem_vmm_resolve_slab(sos_vaddr_t vaddr) +{ + struct sos_kmem_range *range = lookup_range(vaddr); + if (! range) + return NULL; + + return range->slab; +} + + +sos_bool_t sos_kmem_vmm_is_valid_vaddr(sos_vaddr_t vaddr) +{ + struct sos_kmem_range *range = lookup_range(vaddr); + return (range != NULL); +} diff --git a/sos-code-article6.5/sos/kmem_vmm.h b/sos-code-article6.5/sos/kmem_vmm.h new file mode 100644 index 0000000..49b262d --- /dev/null +++ b/sos-code-article6.5/sos/kmem_vmm.h @@ -0,0 +1,113 @@ +/* Copyright (C) 2000 Thomas Petazzoni + Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_KMEM_VMM_H_ +#define _SOS_KMEM_VMM_H_ + +/** + * @file kmem_vmm.h + * + * Kernel Memory Allocator for multiple-page-sized objects residing in + * the kernel (virtual memory) space. Relies on the slab cache + * allocator to allocate its (internal) "range" data structure. + */ + +#include <hwcore/paging.h> + +/* The base and top virtual addresses covered by the kernel allocator */ +#define SOS_KMEM_VMM_BASE 0x4000 /* 16kB */ +#define SOS_KMEM_VMM_TOP SOS_PAGING_MIRROR_VADDR /* 1GB - 4MB */ + +/** Opaque structure used internally and declared here for physmem.h */ +struct sos_kmem_range; + +#include <sos/kmem_slab.h> + +/** + * Mark the areas belonging to SOS_KMEM_VMM_BASE and SOS_KMEM_VMM_TOP + * are either used or free. Those that are already mapped are marked + * as "used", and the 0..SOS_KMEM_VMM_BASE virtual addresses as marked + * as "used" too (to detect incorrect pointer dereferences). + */ +sos_ret_t +sos_kmem_vmm_subsystem_setup(sos_vaddr_t kernel_core_base_vaddr, + sos_vaddr_t kernel_core_top_vaddr, + sos_vaddr_t bootstrap_stack_bottom_vaddr, + sos_vaddr_t bootstrap_stack_top_vaddr); + + +/* + * Flags for kmem_vmm_new_range and kmem_vmm_alloc + */ +/** Physical pages should be immediately mapped */ +#define SOS_KMEM_VMM_MAP (1<<0) +/** Allocation should either success or fail, without blocking */ +#define SOS_KMEM_VMM_ATOMIC (1<<1) + +/** + * Allocate a new kernel area spanning one or multiple pages. + * + * @param range_base_vaddr If not NULL, the start address of the range + * is stored in this location + * @eturn a new range structure + */ +struct sos_kmem_range *sos_kmem_vmm_new_range(sos_size_t nb_pages, + sos_ui32_t flags, + sos_vaddr_t *range_base_vaddr); +sos_ret_t sos_kmem_vmm_del_range(struct sos_kmem_range *range); + + +/** + * Straighforward variant of sos_kmem_vmm_new_range() returning the + * range's start address instead of the range structure + */ +sos_vaddr_t sos_kmem_vmm_alloc(sos_size_t nb_pages, + sos_ui32_t flags); + +/** + * @note you are perfectly allowed to give the address of the + * kernel image, or the address of the bios area here, it will work: + * the kernel/bios WILL be "deallocated". But if you really want to do + * this, well..., do expect some "surprises" ;) + */ +sos_ret_t sos_kmem_vmm_free(sos_vaddr_t vaddr); + + +/** + * @return TRUE when vaddr is covered by any (used) kernel range + */ +sos_bool_t sos_kmem_vmm_is_valid_vaddr(sos_vaddr_t vaddr); + + +/* ***************************** + * Reserved to kmem_slab.c ONLY. + */ +/** + * Associate the range with the given slab. + */ +sos_ret_t sos_kmem_vmm_set_slab(struct sos_kmem_range *range, + struct sos_kslab *slab); + +/** + * Retrieve the (used) slab associated with the range covering vaddr. + * + * @return NULL if the range is not associated with a KMEM range + */ +struct sos_kslab *sos_kmem_vmm_resolve_slab(sos_vaddr_t vaddr); + +#endif /* _SOS_KMEM_VMM_H_ */ diff --git a/sos-code-article6.5/sos/ksynch.c b/sos-code-article6.5/sos/ksynch.c new file mode 100644 index 0000000..799ecc0 --- /dev/null +++ b/sos-code-article6.5/sos/ksynch.c @@ -0,0 +1,233 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + + +#include <hwcore/irq.h> + + +#include "ksynch.h" + + +sos_ret_t sos_ksema_init(struct sos_ksema *sema, const char *name, + int initial_value) +{ + sema->value = initial_value; + return sos_kwaitq_init(& sema->kwaitq, name); +} + + +sos_ret_t sos_ksema_dispose(struct sos_ksema *sema) +{ + return sos_kwaitq_dispose(& sema->kwaitq); +} + + +sos_ret_t sos_ksema_down(struct sos_ksema *sema, + struct sos_time *timeout) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + retval = SOS_OK; + + sema->value --; + if (sema->value < 0) + { + /* Wait for somebody to wake us */ + retval = sos_kwaitq_wait(& sema->kwaitq, timeout); + + /* Something wrong happened (timeout, external wakeup, ...) ? */ + if (SOS_OK != retval) + { + /* Yes: pretend we did not ask for the semaphore */ + sema->value ++; + } + } + + sos_restore_IRQs(flags); + return retval; +} + + +sos_ret_t sos_ksema_trydown(struct sos_ksema *sema) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + + /* Can we take the semaphore without blocking ? */ + if (sema->value >= 1) + { + /* Yes: we take it now */ + sema->value --; + retval = SOS_OK; + } + else + { + /* No: we signal it */ + retval = -SOS_EBUSY; + } + + sos_restore_IRQs(flags); + return retval; +} + + +sos_ret_t sos_ksema_up(struct sos_ksema *sema) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + + sema->value ++; + retval = sos_kwaitq_wakeup(& sema->kwaitq, 1, SOS_OK); + + sos_restore_IRQs(flags); + return retval; +} + + +sos_ret_t sos_kmutex_init(struct sos_kmutex *mutex, const char *name) +{ + mutex->owner = NULL; + return sos_kwaitq_init(& mutex->kwaitq, name); +} + + +sos_ret_t sos_kmutex_dispose(struct sos_kmutex *mutex) +{ + return sos_kwaitq_dispose(& mutex->kwaitq); +} + + +/* + * Implementation based on ownership transfer (ie no while() + * loop). The only assumption is that the thread awoken by + * kmutex_unlock is not suppressed before effectively waking up: in + * that case the mutex will be forever locked AND unlockable (by + * nobody other than the owner, but this is not natural since this + * owner already issued an unlock()...). The same problem happens with + * the semaphores, but in a less obvious manner. + */ +sos_ret_t sos_kmutex_lock(struct sos_kmutex *mutex, + struct sos_time *timeout) +{ + __label__ exit_kmutex_lock; + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + retval = SOS_OK; + + /* Mutex already owned ? */ + if (NULL != mutex->owner) + { + /* Owned by us or by someone else ? */ + if (sos_thread_get_current() == mutex->owner) + { + /* Owned by us: do nothing */ + retval = -SOS_EBUSY; + goto exit_kmutex_lock; + } + + /* Wait for somebody to wake us */ + retval = sos_kwaitq_wait(& mutex->kwaitq, timeout); + + /* Something wrong happened ? */ + if (SOS_OK != retval) + { + goto exit_kmutex_lock; + } + } + + /* Ok, the mutex is available to us: take it */ + mutex->owner = sos_thread_get_current(); + + exit_kmutex_lock: + sos_restore_IRQs(flags); + return retval; +} + + +sos_ret_t sos_kmutex_trylock(struct sos_kmutex *mutex) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + + /* Mutex available to us ? */ + if (NULL == mutex->owner) + { + /* Great ! Take it now */ + mutex->owner = sos_thread_get_current(); + + retval = SOS_OK; + } + else + { + /* No: signal it */ + retval = -SOS_EBUSY; + } + + sos_restore_IRQs(flags); + return retval; +} + + +sos_ret_t sos_kmutex_unlock(struct sos_kmutex *mutex) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + + if (sos_thread_get_current() != mutex->owner) + retval = -SOS_EPERM; + + else if (sos_kwaitq_is_empty(& mutex->kwaitq)) + { + /* + * There is NOT ANY thread waiting => we really mark the mutex + * as FREE + */ + mutex->owner = NULL; + retval = SOS_OK; + } + else + { + /* + * There is at least 1 thread waiting => we DO NOT mark the + * mutex as free ! + * Actually, we should have written: + * mutex->owner = thread_that_is_woken_up; + * But the real Id of the next thread owning the mutex is not + * that important. What is important here is that mutex->owner + * IS NOT NULL. Otherwise there will be a possibility for the + * thread woken up here to have the mutex stolen by a thread + * locking the mutex in the meantime. + */ + retval = sos_kwaitq_wakeup(& mutex->kwaitq, 1, SOS_OK); + } + + sos_restore_IRQs(flags); + return retval; +} diff --git a/sos-code-article6.5/sos/ksynch.h b/sos-code-article6.5/sos/ksynch.h new file mode 100644 index 0000000..597971d --- /dev/null +++ b/sos-code-article6.5/sos/ksynch.h @@ -0,0 +1,170 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_KSYNCH_H_ +#define _SOS_KSYNCH_H_ + + +/** + * @file synch.h + * + * Common kernel synchronisation primitives. + */ + + +#include <sos/errno.h> +#include <sos/kwaitq.h> + + +/* ==================================================================== + * Kernel semaphores, NON-recursive + */ + + +/** + * The structure of a (NON-RECURSIVE) kernel Semaphore + */ +struct sos_ksema +{ + int value; + struct sos_kwaitq kwaitq; +}; + + +/* + * Initialize a kernel semaphore structure with the given name + * + * @param name Name of the semaphore (for debugging purpose only; safe + * [deep copied]) + * + * @param initial_value The value of the semaphore before any up/down + */ +sos_ret_t sos_ksema_init(struct sos_ksema *sema, const char *name, + int initial_value); + + +/* + * De-initialize a kernel semaphore + * + * @return -SOS_EBUSY when semaphore could not be de-initialized + * because at least a thread is in the waitq. + */ +sos_ret_t sos_ksema_dispose(struct sos_ksema *sema); + + +/* + * Enters the semaphore + * + * @param timeout Maximum time to wait for the semaphore. Or NULL for + * "no limit". Updated on return to reflect the time remaining (0 when + * timeout has been triggered) + * + * @return -SOS_EINTR when timeout was triggered or when another waitq + * woke us up. + * + * @note This is a BLOCKING FUNCTION + */ +sos_ret_t sos_ksema_down(struct sos_ksema *sema, + struct sos_time *timeout); + + +/* + * Try to enter the semaphore without blocking. + * + * @return -SOS_EBUSY when locking the semaphore would block + */ +sos_ret_t sos_ksema_trydown(struct sos_ksema *sema); + + +/** + * Increments the semaphore's value, eventually waking up a thread + */ +sos_ret_t sos_ksema_up(struct sos_ksema *sema); + + + +/* ==================================================================== + * Kernel mutex (ie binary semaphore with strong ownership), + * NON-recursive ! + */ + + +/** + * The structure of a (NON-RECURSIVE) kernel Mutex + */ +struct sos_kmutex +{ + struct sos_thread *owner; + struct sos_kwaitq kwaitq; +}; + + +/* + * Initialize a kernel mutex structure with the given name + * + * @param name Name of the mutex (for debugging purpose only; safe + * [deep copied]) + * + * @param initial_value The value of the mutex before any up/down + */ +sos_ret_t sos_kmutex_init(struct sos_kmutex *mutex, const char *name); + + +/* + * De-initialize a kernel mutex + * + * @return -SOS_EBUSY when mutex could not be de-initialized + * because at least a thread is in the waitq. + */ +sos_ret_t sos_kmutex_dispose(struct sos_kmutex *mutex); + + +/* + * Lock the mutex. If the same thread multiply locks the same mutex, + * it won't hurt (no deadlock, return value = -SOS_EBUSY). + * + * @param timeout Maximum time to wait for the mutex. Or NULL for "no + * limit". Updated on return to reflect the time remaining (0 when + * timeout has been triggered) + * + * @return -SOS_EINTR when timeout was triggered or when another waitq + * woke us up, -SOS_EBUSY when the thread already owns the mutex. + * + * @note This is a BLOCKING FUNCTION + */ +sos_ret_t sos_kmutex_lock(struct sos_kmutex *mutex, + struct sos_time *timeout); + + +/* + * Try to lock the mutex without blocking. + * + * @return -SOS_EBUSY when locking the mutex would block + */ +sos_ret_t sos_kmutex_trylock(struct sos_kmutex *mutex); + + +/** + * Unlock the mutex, eventually waking up a thread + * + * @return -SOS_EPERM when the calling thread is NOT the owner of the + * mutex + */ +sos_ret_t sos_kmutex_unlock(struct sos_kmutex *mutex); + + +#endif /* _SOS_KSYNCH_H_ */ diff --git a/sos-code-article6.5/sos/kwaitq.c b/sos-code-article6.5/sos/kwaitq.c new file mode 100644 index 0000000..b4a451e --- /dev/null +++ b/sos-code-article6.5/sos/kwaitq.c @@ -0,0 +1,249 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +#include <sos/klibc.h> +#include <sos/list.h> +#include <sos/assert.h> +#include <hwcore/irq.h> + +#include "kwaitq.h" + + +sos_ret_t sos_kwaitq_init(struct sos_kwaitq *kwq, + const char *name) +{ + memset(kwq, 0x0, sizeof(struct sos_kwaitq)); + +#ifdef SOS_KWQ_DEBUG + if (! name) + name = "<unknown>"; + strzcpy(kwq->name, name, SOS_KWQ_DEBUG_MAX_NAMELEN); +#endif + list_init_named(kwq->waiting_list, + prev_entry_in_kwaitq, next_entry_in_kwaitq); + + return SOS_OK; +} + + +sos_ret_t sos_kwaitq_dispose(struct sos_kwaitq *kwq) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + if (list_is_empty_named(kwq->waiting_list, + prev_entry_in_kwaitq, next_entry_in_kwaitq)) + retval = SOS_OK; + else + retval = -SOS_EBUSY; + + sos_restore_IRQs(flags); + return retval; +} + + +sos_bool_t sos_kwaitq_is_empty(const struct sos_kwaitq *kwq) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + retval = list_is_empty_named(kwq->waiting_list, + prev_entry_in_kwaitq, next_entry_in_kwaitq); + + sos_restore_IRQs(flags); + return retval; +} + + +sos_ret_t sos_kwaitq_init_entry(struct sos_kwaitq_entry *kwq_entry) +{ + memset(kwq_entry, 0x0, sizeof(struct sos_kwaitq_entry)); + kwq_entry->thread = sos_thread_get_current(); + return SOS_OK; +} + + +/** Internal helper function equivalent to sos_kwaitq_add_entry(), but + without interrupt protection scheme, and explicit priority + ordering */ +inline static sos_ret_t _kwaitq_add_entry(struct sos_kwaitq *kwq, + struct sos_kwaitq_entry *kwq_entry) +{ + /* This entry is already added in the kwaitq ! */ + SOS_ASSERT_FATAL(NULL == kwq_entry->kwaitq); + + /* sos_kwaitq_init_entry() has not been called ?! */ + SOS_ASSERT_FATAL(NULL != kwq_entry->thread); + + /* (Re-)Initialize wakeup status of the entry */ + kwq_entry->wakeup_triggered = FALSE; + kwq_entry->wakeup_status = SOS_OK; + + /* Add the thread in the list */ + list_add_tail_named(kwq->waiting_list, kwq_entry, + prev_entry_in_kwaitq, next_entry_in_kwaitq); + + /* Update the list of waitqueues for the thread */ + list_add_tail_named(kwq_entry->thread->kwaitq_list, kwq_entry, + prev_entry_for_thread, next_entry_for_thread); + + kwq_entry->kwaitq = kwq; + + return SOS_OK; +} + + +sos_ret_t sos_kwaitq_add_entry(struct sos_kwaitq *kwq, + struct sos_kwaitq_entry *kwq_entry) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + retval = _kwaitq_add_entry(kwq, kwq_entry); + sos_restore_IRQs(flags); + + return retval; +} + + +/** Internal helper function equivalent to sos_kwaitq_remove_entry(), + but without interrupt protection scheme */ +inline static sos_ret_t +_kwaitq_remove_entry(struct sos_kwaitq *kwq, + struct sos_kwaitq_entry *kwq_entry) +{ + SOS_ASSERT_FATAL(kwq_entry->kwaitq == kwq); + + list_delete_named(kwq->waiting_list, kwq_entry, + prev_entry_in_kwaitq, next_entry_in_kwaitq); + + list_delete_named(kwq_entry->thread->kwaitq_list, kwq_entry, + prev_entry_for_thread, next_entry_for_thread); + + kwq_entry->kwaitq = NULL; + return SOS_OK; +} + + +sos_ret_t sos_kwaitq_remove_entry(struct sos_kwaitq *kwq, + struct sos_kwaitq_entry *kwq_entry) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + retval = _kwaitq_remove_entry(kwq, kwq_entry); + sos_restore_IRQs(flags); + + return retval; +} + + +sos_ret_t sos_kwaitq_wait(struct sos_kwaitq *kwq, + struct sos_time *timeout) +{ + sos_ui32_t flags; + sos_ret_t retval; + struct sos_kwaitq_entry kwq_entry; + + sos_kwaitq_init_entry(& kwq_entry); + + sos_disable_IRQs(flags); + + retval = _kwaitq_add_entry(kwq, & kwq_entry); + + /* Wait for wakeup or timeout */ + sos_thread_sleep(timeout); + /* Woken up ! */ + + /* Sleep delay elapsed ? */ + if (! kwq_entry.wakeup_triggered) + { + /* Yes (timeout occured, or wakeup on another waitqueue): remove + the waitq entry by ourselves */ + _kwaitq_remove_entry(kwq, & kwq_entry); + retval = -SOS_EINTR; + } + else + { + retval = kwq_entry.wakeup_status; + } + + sos_restore_IRQs(flags); + + /* We were correctly awoken: position return status */ + return retval; +} + + +sos_ret_t sos_kwaitq_wakeup(struct sos_kwaitq *kwq, + unsigned int nb_threads, + sos_ret_t wakeup_status) +{ + sos_ui32_t flags; + + sos_disable_IRQs(flags); + + /* Wake up as much threads waiting in waitqueue as possible (up to + nb_threads), scanning the list in FIFO order */ + while (! list_is_empty_named(kwq->waiting_list, + prev_entry_in_kwaitq, next_entry_in_kwaitq)) + { + struct sos_kwaitq_entry *kwq_entry + = list_get_head_named(kwq->waiting_list, + prev_entry_in_kwaitq, next_entry_in_kwaitq); + + /* Enough threads woken up ? */ + if (nb_threads <= 0) + break; + + /* + * Ok: wake up the thread for this entry + */ + + /* Thread already woken up ? */ + if (SOS_THR_RUNNING == sos_thread_get_state(kwq_entry->thread)) + { + /* Yes => Do nothing because WE are that woken-up thread. In + particular: don't call set_ready() here because this + would result in an inconsistent configuration (currently + running thread marked as "waiting for CPU"...). */ + continue; + } + else + { + /* No => wake it up now. */ + sos_sched_set_ready(kwq_entry->thread); + } + + /* Remove this waitq entry */ + _kwaitq_remove_entry(kwq, kwq_entry); + kwq_entry->wakeup_triggered = TRUE; + kwq_entry->wakeup_status = wakeup_status; + + /* Next iteration... */ + nb_threads --; + } + + sos_restore_IRQs(flags); + + return SOS_OK; +} diff --git a/sos-code-article6.5/sos/kwaitq.h b/sos-code-article6.5/sos/kwaitq.h new file mode 100644 index 0000000..4f879aa --- /dev/null +++ b/sos-code-article6.5/sos/kwaitq.h @@ -0,0 +1,180 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_KWAITQ_H_ +#define _SOS_KWAITQ_H_ + +#include <sos/errno.h> +#include <sos/thread.h> +#include <sos/time.h> +#include <sos/sched.h> + + +/** + * @kwaitq.h + * + * Low-level functions to manage queues of threads waiting for a + * resource. These functions are public. For higher-level + * synchronization primitives such as mutex, semaphores, conditions, + * ... prefer looking at the corresponding libraries. + */ + + +/** + * Define this if you want to know the names of the kwaitq + */ +// #define SOS_KWQ_DEBUG + + +/* Forward declaration */ +struct sos_kwaitq_entry; + + +/** + * Definition of a waitqueue. In a kwaitq, the threads are ordererd in + * FIFO order. + */ +struct sos_kwaitq +{ +#ifdef SOS_KWQ_DEBUG +# define SOS_KWQ_DEBUG_MAX_NAMELEN 32 + char name[SOS_KWQ_DEBUG_MAX_NAMELEN]; +#endif + struct sos_kwaitq_entry *waiting_list; +}; + + +/** + * Definition of an entry for a thread waiting in the waitqueue + */ +struct sos_kwaitq_entry +{ + /** The thread associted with this entry */ + struct sos_thread *thread; + + /** The kwaitqueue this entry belongs to */ + struct sos_kwaitq *kwaitq; + + /** TRUE when somebody woke up this entry */ + sos_bool_t wakeup_triggered; + + /** The status of wakeup for this entry. @see wakeup_status argument + of sos_kwaitq_wakeup() */ + sos_ret_t wakeup_status; + + /** Other entries in this kwaitqueue */ + struct sos_kwaitq_entry *prev_entry_in_kwaitq, *next_entry_in_kwaitq; + + /** Other entries for the thread */ + struct sos_kwaitq_entry *prev_entry_for_thread, *next_entry_for_thread; +}; + + +/** + * Initialize an empty waitqueue. + * + * @param name Used only if SOS_KWQ_DEBUG is defined (safe [deep + * copied]) + */ +sos_ret_t sos_kwaitq_init(struct sos_kwaitq *kwq, + const char *name); + + +/** + * Release a waitqueue, making sure that no thread is in it. + * + * @return -SOS_EBUSY in case a thread is still in the waitqueue. + */ +sos_ret_t sos_kwaitq_dispose(struct sos_kwaitq *kwq); + + +/** + * Return whether there are no threads in the waitq + */ +sos_bool_t sos_kwaitq_is_empty(const struct sos_kwaitq *kwq); + + +/** + * Initialize a waitqueue entry. Mainly consists in updating the + * "thread" field of the entry (set to current running thread), and + * initializing the remaining of the entry as to indicate it does not + * belong to any waitq. + */ +sos_ret_t sos_kwaitq_init_entry(struct sos_kwaitq_entry *kwq_entry); + + +/** + * Add an entry (previously initialized with sos_kwaitq_init_entry()) + * in the given waitqueue. + * + * @note: No state change/context switch can occur here ! Among other + * things: the current executing thread is not preempted. + */ +sos_ret_t sos_kwaitq_add_entry(struct sos_kwaitq *kwq, + struct sos_kwaitq_entry *kwq_entry); + + +/** + * Remove the given kwaitq_entry from the kwaitq. + * + * @note: No state change/context switch can occur here ! Among other + * things: the thread associated with the entry is not necessarilly + * the same as the one currently running, and does not preempt the + * current running thread if they are different. + */ +sos_ret_t sos_kwaitq_remove_entry(struct sos_kwaitq *kwq, + struct sos_kwaitq_entry *kwq_entry); + + +/** + * Helper function to make the current running thread block in the + * given kwaitq, waiting to be woken up by somedy else or by the given + * timeout. It calls the sos_kwaitq_add_entry() and + * sos_kwaitq_remove_entry(). + * + * @param timeout The desired timeout (can be NULL => wait for + * ever). It is updated by the function to reflect the remaining + * timeout in case the thread has been woken-up prior to its + * expiration. + * + * @return -SOS_EINTR when the thread is resumed while it has not be + * explicitely woken up by someone calling sos_kwaitq_wakeup() upon + * the same waitqueue... This can only happen 1/ if the timeout + * expired, or 2/ if the current thread is also in another kwaitq + * different to "kwq". Otherwise return the value set by + * sos_kwaitq_wakeup(). The timeout remaining is updated in timeout. + * + * @note This is a BLOCKING FUNCTION + */ +sos_ret_t sos_kwaitq_wait(struct sos_kwaitq *kwq, + struct sos_time *timeout); + + +/** + * Wake up as much as nb_thread threads (SOS_KWQ_WAKEUP_ALL to wake + * up all threads) in the kwaitq kwq, in FIFO order. + * + * @param wakeup_status The value returned by sos_kwaitq_wait() when + * the thread will effectively woken up due to this wakeup. + */ +sos_ret_t sos_kwaitq_wakeup(struct sos_kwaitq *kwq, + unsigned int nb_threads, + sos_ret_t wakeup_status); +#define SOS_KWQ_WAKEUP_ALL (~((unsigned int)0)) + + +#endif /* _SOS_KWAITQ_H_ */ diff --git a/sos-code-article6.5/sos/list.h b/sos-code-article6.5/sos/list.h new file mode 100644 index 0000000..67e72f3 --- /dev/null +++ b/sos-code-article6.5/sos/list.h @@ -0,0 +1,186 @@ +/* Copyright (C) 2001 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_LIST_H_ +#define _SOS_LIST_H_ + +/** + * @file list.h + * + * Circular doubly-linked lists implementation entirely based on C + * macros + */ + + +/* *_named are used when next and prev links are not exactly next + and prev. For instance when we have next_in_team, prev_in_team, + prev_global and next_global */ + +#define list_init_named(list,prev,next) \ + ((list) = NULL) + +#define list_singleton_named(list,item,prev,next) ({ \ + (item)->next = (item)->prev = (item); \ + (list) = (item); \ +}) + +#define list_is_empty_named(list,prev,next) \ + ((list) == NULL) + +#define list_get_head_named(list,prev,next) \ + (list) + +#define list_get_tail_named(list,prev,next) \ + ((list)?((list)->prev):NULL) + +/* Internal macro : insert before the head == insert at tail */ +#define __list_insert_atleft_named(before_this,item,prev,next) ({ \ + (before_this)->prev->next = (item); \ + (item)->prev = (before_this)->prev; \ + (before_this)->prev = (item); \ + (item)->next = (before_this); \ +}) + +/* @note Before_this and item are expected to be valid ! */ +#define list_insert_before_named(list,before_this,item,prev,next) ({ \ + __list_insert_atleft_named(before_this,item,prev,next); \ + if ((list) == (before_this)) (list) = (item); \ +}) + +/** @note After_this and item are expected to be valid ! */ +#define list_insert_after_named(list,after_this,item,prev,next) ({ \ + (after_this)->next->prev = (item); \ + (item)->next = (after_this)->next; \ + (after_this)->next = (item); \ + (item)->prev = (after_this); \ +}) + +#define list_add_head_named(list,item,prev,next) ({ \ + if (list) \ + list_insert_before_named(list,list,item,prev,next); \ + else \ + list_singleton_named(list,item,prev,next); \ + (list) = (item); \ +}) + +#define list_add_tail_named(list,item,prev,next) ({ \ + if (list) \ + __list_insert_atleft_named(list,item,prev,next); \ + else \ + list_singleton_named(list,item,prev,next); \ +}) + +/** @note NO check whether item really is in list ! */ +#define list_delete_named(list,item,prev,next) ({ \ + if ( ((item)->next == (item)) && ((item)->prev == (item)) ) \ + (item)->next = (item)->prev = (list) = NULL; \ + else { \ + (item)->prev->next = (item)->next; \ + (item)->next->prev = (item)->prev; \ + if ((item) == (list)) (list) = (item)->next; \ + (item)->prev = (item)->next = NULL; \ + } \ +}) + +#define list_pop_head_named(list,prev,next) ({ \ + typeof(list) __ret_elt = (list); \ + list_delete_named(list,__ret_elt,prev,next); \ + __ret_elt; }) + +/** Loop statement that iterates through all of its elements, from + head to tail */ +#define list_foreach_forward_named(list,iterator,nb_elements,prev,next) \ + for (nb_elements=0, (iterator) = (list) ; \ + (iterator) && (!nb_elements || ((iterator) != (list))) ; \ + nb_elements++, (iterator) = (iterator)->next ) + +/** Loop statement that iterates through all of its elements, from + tail back to head */ +#define list_foreach_backward_named(list,iterator,nb_elements,prev,next) \ + for (nb_elements=0, (iterator) = list_get_tail_named(list,prev,next) ; \ + (iterator) && (!nb_elements || \ + ((iterator) != list_get_tail_named(list,prev,next))) ; \ + nb_elements++, (iterator) = (iterator)->prev ) + +#define list_foreach_named list_foreach_forward_named + +/** True when we exitted early from the foreach loop (ie break) */ +#define list_foreach_early_break(list,iterator,nb_elements) \ + ((list) && ( \ + ((list) != (iterator)) || \ + ( ((list) == (iterator)) && (nb_elements == 0)) )) + +/** Loop statement that also removes the item at each iteration */ +#define list_collapse_named(list,iterator,prev,next) \ + for ( ; ({ ((iterator) = (list)) ; \ + if (list) list_delete_named(list,iterator,prev,next) ; \ + (iterator); }) ; ) + + +/* + * the same macros : assume that the prev and next fields are really + * named "prev" and "next" + */ + +#define list_init(list) \ + list_init_named(list,prev,next) + +#define list_singleton(list,item) \ + list_singleton_named(list,item,prev,next) + +#define list_is_empty(list) \ + list_is_empty_named(list,prev,next) + +#define list_get_head(list) \ + list_get_head_named(list,prev,next) \ + +#define list_get_tail(list) \ + list_get_tail_named(list,prev,next) \ + +/* @note Before_this and item are expected to be valid ! */ +#define list_insert_after(list,after_this,item) \ + list_insert_after_named(list,after_this,item,prev,next) + +/* @note After_this and item are expected to be valid ! */ +#define list_insert_before(list,before_this,item) \ + list_insert_before_named(list,before_this,item,prev,next) + +#define list_add_head(list,item) \ + list_add_head_named(list,item,prev,next) + +#define list_add_tail(list,item) \ + list_add_tail_named(list,item,prev,next) + +/* @note NO check whether item really is in list ! */ +#define list_delete(list,item) \ + list_delete_named(list,item,prev,next) + +#define list_pop_head(list) \ + list_pop_head_named(list,prev,next) + +#define list_foreach_forward(list,iterator,nb_elements) \ + list_foreach_forward_named(list,iterator,nb_elements,prev,next) + +#define list_foreach_backward(list,iterator,nb_elements) \ + list_foreach_backward_named(list,iterator,nb_elements,prev,next) + +#define list_foreach list_foreach_forward + +#define list_collapse(list,iterator) \ + list_collapse_named(list,iterator,prev,next) + +#endif /* _SOS_LIST_H_ */ diff --git a/sos-code-article6.5/sos/macros.h b/sos-code-article6.5/sos/macros.h new file mode 100644 index 0000000..80a05d3 --- /dev/null +++ b/sos-code-article6.5/sos/macros.h @@ -0,0 +1,41 @@ +/* Copyright (C) 2004 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_MACROS_H_ +#define _SOS_MACROS_H_ + +/** Align on a boundary (MUST be a power of 2), so that return value <= val */ +#define SOS_ALIGN_INF(val,boundary) \ + (((unsigned)(val)) & (~((boundary)-1))) + +/** Align on a boundary (MUST be a power of 2), so that return value >= val */ +#define SOS_ALIGN_SUP(val,boundary) \ + ({ unsigned int __bnd=(boundary); \ + (((((unsigned)(val))-1) & (~(__bnd - 1))) + __bnd); }) + +/** Check whether val is aligned on a boundary (MUST be a power of 2) */ +#define SOS_IS_ALIGNED(val,boundary) \ + ( 0 == (((unsigned)(val)) & ((boundary)-1)) ) + +/** + * @return TRUE if val is a power of 2. + * @note val is evaluated multiple times + */ +#define SOS_IS_POWER_OF_2(val) \ + ((((val) - 1) & (val)) == 0) + +#endif /* _SOS_MACROS_H_ */ diff --git a/sos-code-article6.5/sos/main.c b/sos-code-article6.5/sos/main.c new file mode 100644 index 0000000..6201f47 --- /dev/null +++ b/sos-code-article6.5/sos/main.c @@ -0,0 +1,461 @@ +/* Copyright (C) 2004 The SOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +/* Include definitions of the multiboot standard */ +#include <bootstrap/multiboot.h> +#include <hwcore/idt.h> +#include <hwcore/gdt.h> +#include <hwcore/irq.h> +#include <hwcore/exception.h> +#include <hwcore/i8254.h> +#include <sos/list.h> +#include <sos/physmem.h> +#include <hwcore/paging.h> +#include <sos/kmem_vmm.h> +#include <sos/kmalloc.h> +#include <sos/time.h> +#include <sos/thread.h> +#include <sos/klibc.h> +#include <sos/assert.h> +#include <drivers/x86_videomem.h> +#include <drivers/bochs.h> + + +/* Helper function to display each bits of a 32bits integer on the + screen as dark or light carrets */ +void display_bits(unsigned char row, unsigned char col, + unsigned char attribute, + sos_ui32_t integer) +{ + int i; + /* Scan each bit of the integer, MSb first */ + for (i = 31 ; i >= 0 ; i--) + { + /* Test if bit i of 'integer' is set */ + int bit_i = (integer & (1 << i)); + /* Ascii 219 => dark carret, Ascii 177 => light carret */ + unsigned char ascii_code = bit_i?219:177; + sos_x86_videomem_putchar(row, col++, + attribute, + ascii_code); + } +} + + +/* Clock IRQ handler */ +static void clk_it(int intid) +{ + static sos_ui32_t clock_count = 0; + + display_bits(0, 48, + SOS_X86_VIDEO_FG_LTGREEN | SOS_X86_VIDEO_BG_BLUE, + clock_count); + clock_count++; + + /* Execute the expired timeout actions (if any) */ + sos_time_do_tick(); +} + + +/* ====================================================================== + * Page fault exception handling + */ + +/* Helper function to dump a backtrace on bochs and/or the console */ +static void dump_backtrace(const struct sos_cpu_state *cpu_state, + sos_vaddr_t stack_bottom, + sos_size_t stack_size, + sos_bool_t on_console, + sos_bool_t on_bochs) +{ + void backtracer(sos_vaddr_t PC, + sos_vaddr_t params, + sos_ui32_t depth, + void *custom_arg) + { + sos_ui32_t invalid = 0xffffffff, *arg1, *arg2, *arg3, *arg4; + + /* Get the address of the first 3 arguments from the + frame. Among these arguments, 0, 1, 2, 3 arguments might be + meaningful (depending on how many arguments the function may + take). */ + arg1 = (sos_ui32_t*)params; + arg2 = (sos_ui32_t*)(params+4); + arg3 = (sos_ui32_t*)(params+8); + arg4 = (sos_ui32_t*)(params+12); + + /* Make sure the addresses of these arguments fit inside the + stack boundaries */ +#define INTERVAL_OK(b,v,u) ( ((b) <= (sos_vaddr_t)(v)) \ + && ((sos_vaddr_t)(v) < (u)) ) + if (!INTERVAL_OK(stack_bottom, arg1, stack_bottom + stack_size)) + arg1 = &invalid; + if (!INTERVAL_OK(stack_bottom, arg2, stack_bottom + stack_size)) + arg2 = &invalid; + if (!INTERVAL_OK(stack_bottom, arg3, stack_bottom + stack_size)) + arg3 = &invalid; + if (!INTERVAL_OK(stack_bottom, arg4, stack_bottom + stack_size)) + arg4 = &invalid; + + /* Print the function context for this frame */ + if (on_bochs) + sos_bochs_printf("[%d] PC=0x%x arg1=0x%x arg2=0x%x arg3=0x%x\n", + (unsigned)depth, (unsigned)PC, + (unsigned)*arg1, (unsigned)*arg2, + (unsigned)*arg3); + + if (on_console) + sos_x86_videomem_printf(23-depth, 3, + SOS_X86_VIDEO_BG_BLUE + | SOS_X86_VIDEO_FG_LTGREEN, + "[%d] PC=0x%x arg1=0x%x arg2=0x%x arg3=0x%x arg4=0x%x", + (unsigned)depth, PC, + (unsigned)*arg1, (unsigned)*arg2, + (unsigned)*arg3, (unsigned)*arg4); + + } + + sos_backtrace(cpu_state, 15, stack_bottom, stack_size, backtracer, NULL); +} + + +/* Page fault exception handler with demand paging for the kernel */ +static void pgflt_ex(int intid, const struct sos_cpu_state *ctxt) +{ + static sos_ui32_t demand_paging_count = 0; + sos_vaddr_t faulting_vaddr = sos_cpu_context_get_EX_faulting_vaddr(ctxt); + sos_paddr_t ppage_paddr; + + /* Check if address is covered by any VMM range */ + if (! sos_kmem_vmm_is_valid_vaddr(faulting_vaddr)) + { + /* No: The page fault is out of any kernel virtual region. For + the moment, we don't handle this. */ + dump_backtrace(ctxt, + bootstrap_stack_bottom, + bootstrap_stack_size, + TRUE, TRUE); + sos_display_fatal_error("Unresolved page Fault at instruction 0x%x on access to address 0x%x (info=%x)!", + sos_cpu_context_get_PC(ctxt), + (unsigned)faulting_vaddr, + (unsigned)sos_cpu_context_get_EX_info(ctxt)); + SOS_ASSERT_FATAL(! "Got page fault (note: demand paging is disabled)"); + } + + + /* + * Demand paging + */ + + /* Update the number of demand paging requests handled */ + demand_paging_count ++; + display_bits(0, 0, + SOS_X86_VIDEO_FG_LTRED | SOS_X86_VIDEO_BG_BLUE, + demand_paging_count); + + /* Allocate a new page for the virtual address */ + ppage_paddr = sos_physmem_ref_physpage_new(FALSE); + if (! ppage_paddr) + SOS_ASSERT_FATAL(! "TODO: implement swap. (Out of mem in demand paging because no swap for kernel yet !)"); + SOS_ASSERT_FATAL(SOS_OK == sos_paging_map(ppage_paddr, + SOS_PAGE_ALIGN_INF(faulting_vaddr), + FALSE, + SOS_VM_MAP_PROT_READ + | SOS_VM_MAP_PROT_WRITE + | SOS_VM_MAP_ATOMIC)); + sos_physmem_unref_physpage(ppage_paddr); + + /* Ok, we can now return to interrupted context */ +} + + +/* ====================================================================== + * Demonstrate the use of SOS kernel threads + * - Kernel Threads are created with various priorities and their + * state is printed on both the console and the bochs' 0xe9 port + * - For tests regarding threads' synchronization, see mouse_sim.c + */ + +struct thr_arg +{ + char character; + int color; + + int col; + int row; +}; + + +static void demo_thread(void *arg) +{ + struct thr_arg *thr_arg = (struct thr_arg*)arg; + int progress = 0; + + sos_bochs_printf("start %c", thr_arg->character); + while (1) + { + progress ++; + display_bits(thr_arg->row, thr_arg->col+1, thr_arg->color, progress); + + sos_bochs_putchar(thr_arg->character); + + /* Yield the CPU to another thread sometimes... */ + if ((random() % 100) == 0) + { + sos_bochs_printf("[37myield(%c)[m\n", thr_arg->character); + sos_x86_videomem_putchar(thr_arg->row, thr_arg->col, 0x1e, 'Y'); + SOS_ASSERT_FATAL(SOS_OK == sos_thread_yield()); + sos_x86_videomem_putchar(thr_arg->row, thr_arg->col, 0x1e, 'R'); + } + + /* Go to sleep some other times... */ + else if ((random() % 200) == 0) + { + struct sos_time t = (struct sos_time){ .sec=0, .nanosec=50000000 }; + sos_bochs_printf("[37msleep1(%c)[m\n", thr_arg->character); + sos_x86_videomem_putchar(thr_arg->row, thr_arg->col, 0x1e, 's'); + SOS_ASSERT_FATAL(SOS_OK == sos_thread_sleep(& t)); + SOS_ASSERT_FATAL(sos_time_is_zero(& t)); + sos_x86_videomem_putchar(thr_arg->row, thr_arg->col, 0x1e, 'R'); + } + + /* Go to sleep for a longer time some other times... */ + else if ((random() % 300) == 0) + { + struct sos_time t = (struct sos_time){ .sec=0, .nanosec=300000000 }; + sos_bochs_printf("[37msleep2(%c)[m\n", thr_arg->character); + sos_x86_videomem_putchar(thr_arg->row, thr_arg->col, 0x1e, 'S'); + SOS_ASSERT_FATAL(SOS_OK == sos_thread_sleep(& t)); + SOS_ASSERT_FATAL(sos_time_is_zero(& t)); + sos_x86_videomem_putchar(thr_arg->row, thr_arg->col, 0x1e, 'R'); + } + + /* Infinite loop otherwise */ + } +} + + +static void test_thread() +{ + /* "static" variables because we want them to remain even when the + function returns */ + static struct thr_arg arg_b, arg_c, arg_d, arg_e, arg_R, arg_S; + sos_ui32_t flags; + + sos_disable_IRQs(flags); + + arg_b = (struct thr_arg) { .character='b', .col=0, .row=21, .color=0x14 }; + sos_create_kernel_thread("YO[b]", demo_thread, (void*)&arg_b); + + arg_c = (struct thr_arg) { .character='c', .col=46, .row=21, .color=0x14 }; + sos_create_kernel_thread("YO[c]", demo_thread, (void*)&arg_c); + + arg_d = (struct thr_arg) { .character='d', .col=0, .row=20, .color=0x14 }; + sos_create_kernel_thread("YO[d]", demo_thread, (void*)&arg_d); + + arg_e = (struct thr_arg) { .character='e', .col=0, .row=19, .color=0x14 }; + sos_create_kernel_thread("YO[e]", demo_thread, (void*)&arg_e); + + arg_R = (struct thr_arg) { .character='R', .col=0, .row=17, .color=0x1c }; + sos_create_kernel_thread("YO[R]", demo_thread, (void*)&arg_R); + + arg_S = (struct thr_arg) { .character='S', .col=0, .row=16, .color=0x1c }; + sos_create_kernel_thread("YO[S]", demo_thread, (void*)&arg_S); + + sos_restore_IRQs(flags); +} + + +/* ====================================================================== + * An operating system MUST always have a ready thread ! Otherwise: + * what would the CPU have to execute ?! + */ +static void idle_thread() +{ + sos_ui32_t idle_twiddle = 0; + + while (1) + { + /* Remove this instruction if you get an "Invalid opcode" CPU + exception (old 80386 CPU) */ + asm("hlt\n"); + + idle_twiddle ++; + display_bits(0, 0, SOS_X86_VIDEO_FG_GREEN | SOS_X86_VIDEO_BG_BLUE, + idle_twiddle); + + /* Lend the CPU to some other thread */ + sos_thread_yield(); + } +} + + +/* ====================================================================== + * The C entry point of our operating system + */ +void sos_main(unsigned long magic, unsigned long addr) +{ + unsigned i; + sos_paddr_t sos_kernel_core_base_paddr, sos_kernel_core_top_paddr; + struct sos_time tick_resolution; + + /* Grub sends us a structure, called multiboot_info_t with a lot of + precious informations about the system, see the multiboot + documentation for more information. */ + multiboot_info_t *mbi; + mbi = (multiboot_info_t *) addr; + + /* Setup bochs and console, and clear the console */ + sos_bochs_setup(); + + sos_x86_videomem_setup(); + sos_x86_videomem_cls(SOS_X86_VIDEO_BG_BLUE); + + /* Greetings from SOS */ + if (magic == MULTIBOOT_BOOTLOADER_MAGIC) + /* Loaded with Grub */ + sos_x86_videomem_printf(1, 0, + SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, + "Welcome From GRUB to %s%c RAM is %dMB (upper mem = 0x%x kB)", + "SOS article 6.5", ',', + (unsigned)(mbi->mem_upper >> 10) + 1, + (unsigned)mbi->mem_upper); + else + /* Not loaded with grub */ + sos_x86_videomem_printf(1, 0, + SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, + "Welcome to SOS article 6.5"); + + sos_bochs_putstring("Message in a bochs: This is SOS article 6.5.\n"); + + /* Setup CPU segmentation and IRQ subsystem */ + sos_gdt_subsystem_setup(); + sos_idt_subsystem_setup(); + + /* Setup SOS IRQs and exceptions subsystem */ + sos_exception_subsystem_setup(); + sos_irq_subsystem_setup(); + + /* Configure the timer so as to raise the IRQ0 at a 100Hz rate */ + sos_i8254_set_frequency(100); + + /* Setup the kernel time subsystem to get prepared to take the timer + ticks into account */ + tick_resolution = (struct sos_time) { .sec=0, .nanosec=10000000UL }; + sos_time_subsysem_setup(& tick_resolution); + + /* We need a multiboot-compliant boot loader to get the size of the RAM */ + if (magic != MULTIBOOT_BOOTLOADER_MAGIC) + { + sos_x86_videomem_putstring(20, 0, + SOS_X86_VIDEO_FG_LTRED + | SOS_X86_VIDEO_BG_BLUE + | SOS_X86_VIDEO_FG_BLINKING, + "I'm not loaded with Grub !"); + /* STOP ! */ + for (;;) + continue; + } + + /* + * Some interrupt handlers + */ + + /* Binding some HW interrupts and exceptions to software routines */ + sos_irq_set_routine(SOS_IRQ_TIMER, + clk_it); + + /* + * Setup physical memory management + */ + + /* Multiboot says: "The value returned for upper memory is maximally + the address of the first upper memory hole minus 1 megabyte.". It + also adds: "It is not guaranteed to be this value." aka "YMMV" ;) */ + sos_physmem_subsystem_setup((mbi->mem_upper<<10) + (1<<20), + & sos_kernel_core_base_paddr, + & sos_kernel_core_top_paddr); + + /* + * Switch to paged-memory mode + */ + + /* Disabling interrupts should seem more correct, but it's not really + necessary at this stage */ + SOS_ASSERT_FATAL(SOS_OK == + sos_paging_subsystem_setup(sos_kernel_core_base_paddr, + sos_kernel_core_top_paddr)); + + /* Bind the page fault exception */ + sos_exception_set_routine(SOS_EXCEPT_PAGE_FAULT, + pgflt_ex); + + /* + * Setup kernel virtual memory allocator + */ + + if (sos_kmem_vmm_subsystem_setup(sos_kernel_core_base_paddr, + sos_kernel_core_top_paddr, + bootstrap_stack_bottom, + bootstrap_stack_bottom + + bootstrap_stack_size)) + sos_bochs_printf("Could not setup the Kernel virtual space allocator\n"); + + if (sos_kmalloc_subsystem_setup()) + sos_bochs_printf("Could not setup the Kmalloc subsystem\n"); + + /* + * Initialize the Kernel thread and scheduler subsystems + */ + + /* Initialize kernel thread subsystem */ + sos_thread_subsystem_setup(bootstrap_stack_bottom, + bootstrap_stack_size); + + /* Initialize the scheduler */ + sos_sched_subsystem_setup(); + + /* Declare the IDLE thread */ + SOS_ASSERT_FATAL(sos_create_kernel_thread("idle", idle_thread, NULL) != NULL); + + /* Enabling the HW interrupts here, this will make the timer HW + interrupt call the scheduler */ + asm volatile ("sti\n"); + + + /* Now run some Kernel threads just for fun ! */ + extern void MouseSim(); + MouseSim(); + test_thread(); + + /* + * We can safely exit from this function now, for there is already + * an idle Kernel thread ready to make the CPU busy working... + * + * However, we must EXPLICITELY call sos_thread_exit() because a + * simple "return" will return nowhere ! Actually this first thread + * was initialized by the Grub bootstrap stage, at a time when the + * word "thread" did not exist. This means that the stack was not + * setup in order for a return here to call sos_thread_exit() + * automagically. Hence we must call it manually. This is the ONLY + * kernel thread where we must do this manually. + */ + sos_bochs_printf("Bye from primary thread !\n"); + sos_thread_exit(); + SOS_FATAL_ERROR("No trespassing !"); +} diff --git a/sos-code-article6.5/sos/mouse_sim.c b/sos-code-article6.5/sos/mouse_sim.c new file mode 100644 index 0000000..c5eb6bf --- /dev/null +++ b/sos-code-article6.5/sos/mouse_sim.c @@ -0,0 +1,803 @@ +/*************************************************************************** + * Copyright (C) 2004 by cyril dupuit * + * cyrildupuit@hotmail.com * + * http://perso.wanadoo.fr/koalys/ * + * (Adaptation for SOS by d2 -- 2004/12/20) * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +//***************************************************************************** +// Nom du module : MouseSim.c +// Description : Creation et destruction de souris mangeuse de fromages +//***************************************************************************** + +#include <sos/assert.h> +#include <sos/klibc.h> +#include <sos/thread.h> +#include <sos/ksynch.h> +#include <sos/kmalloc.h> +#include <drivers/x86_videomem.h> + +// Historique : +// 20/12/04 : Suppr DestroyMap et suppr handler kbd dans version LM (d2) +// 26/11/04 : Bug trouve et resolu dans la fonction DestroyMap +// 21/11/04 : Creation du module V1.0 + +//***************************************************************************** +// Definition des equivalences : +//***************************************************************************** +#define MAP_X 76 +#define MAP_Y 12 +#define MAP_SIZE MAP_X * MAP_Y + +#define MOUSE 0x01 +#define CHEESE 0x02 +#define OBSTACLE 0x04 +#define INPUT 0x08 +#define OUTPUT 0x10 + +#define OBSTACLE_COUNT 100 +#define CHEESE_COUNT 650 + +#define MOUSE_FULL 0x01 +#define MOUSE_EMPTY 0x02 +#define CHEESE_FOUND 0x04 +#define MOUSE_EXITED 0x08 + +#define MOUSE_SPEED_MAX 1000 +#define MOUSE_SPEED_MIN 4 + +typedef unsigned int Color_t; + +struct Point{ + int X; + int Y; + }; + +typedef struct Point Point_t; + +#define Set(Reg, Flag) Reg = (Reg | Flag) +#define Reset(Reg, Flag) Reg = (Reg &(~Flag)) +#define IsSet(Reg, Flag) (Reg & Flag) + + +//***************************************************************************** +// Structure de gestion d'un element +//***************************************************************************** +struct Element{ + sos_ui32_t Type;//Type d'element + sos_ui32_t Status; + Color_t Color;//Couleur de l'element + Point_t P;//Coordonnees de l'element + struct sos_thread * ThreadID;//Thread associe a la souris + int Way;//Direction de la souris + }; + +typedef struct Element Element_t; + +//***************************************************************************** +// Prototypes des fonctions/procedures : +//***************************************************************************** +static void MouseCommander(void); +static void DrawMap(void); +static sos_ret_t CreateMap(void); +static sos_ret_t InitMapInput(Element_t * * pMap); +static sos_ret_t InitMapOutput(Element_t * * pMap); +static sos_ret_t ElementInit(Element_t * * pMap, unsigned int Type); +static void Mouse(unsigned long Param); +static void MouseMove(Point_t * P); +static Point_t ChoosePosition(Element_t * pMouse, int Positions[], int Count); +static int EvaluatePositions(Point_t Org, int Positions[], Point_t * Cheese); +static sos_bool_t IsCollision(Point_t Org, Point_t p, Point_t *Cheese); +static sos_bool_t AffectMovement(Point_t Org, Point_t p); +static void MouseCreator(void); +static sos_ret_t CreateMouse(void); + +//***************************************************************************** +// Variables globales de ce module : +//***************************************************************************** + +static Element_t * * pMap; +static struct sos_ksema SemMap; +static struct sos_ksema SemMouse; +static int MouseCount = 0; +static int CheeseCount = 0; +static int ObstacleCount = 0; +static int MouseSpeed = 100; + +//***************************************************************************** +// Koalys Glue +//***************************************************************************** +void DrawPixel(int x, int y, Color_t color) +{ + sos_x86_videomem_putchar(y+3, x+2, color, 219); +} + + + +//***************************************************************************** +// Point d'entre de la 'simulation' +//***************************************************************************** +void MouseSim(void) +{ + //Creation du semaphore de protection de la carte + SOS_ASSERT_FATAL(SOS_OK == sos_ksema_init(& SemMap, "SemMap", 1)); + + //Creation du semaphore de creation de souris + SOS_ASSERT_FATAL(SOS_OK == sos_ksema_init(& SemMouse, "SemMouse", 2)); + + //Creation de la carte + SOS_ASSERT_FATAL(SOS_OK == CreateMap()); + + //Creation du thread createur de souris + SOS_ASSERT_FATAL(sos_create_kernel_thread("MouseCreator", + (sos_kernel_thread_start_routine_t)MouseCreator, + 0) != NULL); + +} + + +//***************************************************************************** +// But de la fonction : Creer et initialiser la carte +// Entree : Aucune +// Parametre retourne : ERROR si la memoire est insuffisante, TRUE sinon +//***************************************************************************** +static sos_ret_t CreateMap(void) +{ + pMap = (Element_t * *)sos_kmalloc(MAP_SIZE * sizeof(Element_t *), 0); + if(pMap == NULL) return -SOS_ENOMEM; + + //Mettre la carte a 0 + memset(pMap, 0, MAP_SIZE * sizeof(Element_t *)); + + //Initialisation de l'entree de la carte + if(SOS_OK != InitMapInput(pMap)) + {//Memoire insuffisante + return -SOS_EFATAL; + } + + //Initialisation de la sortie de la carte + if(InitMapOutput(pMap) != SOS_OK) + {//Memoire insuffisante + return -SOS_EFATAL; + } + + //Initialisation du fromage + if(ElementInit(pMap, CHEESE) != SOS_OK) + {//Memoire insuffisante + return -SOS_EFATAL; + } + + //Initialisation des obstacles + if(ElementInit(pMap, OBSTACLE) != SOS_OK) + {//Memoire insuffisante + return -SOS_EFATAL; + } + + DrawMap();//Afficher la carte creee + + return SOS_OK; +} + +//***************************************************************************** +// But de la procedure : Dessiner la carte a l'ecran +// Entree : Aucune +// Sortie : Aucune +//***************************************************************************** +static void DrawMap(void) +{ + unsigned int I; + + for(I = 0; I < MAP_SIZE; I++) + { + if(pMap[I] != NULL) + { + DrawPixel(I % MAP_X, I/MAP_X, pMap[I]->Color); + } + else DrawPixel(I % MAP_X, I/MAP_X, SOS_X86_VIDEO_FG_BLACK); + } + sos_x86_videomem_printf(23, 0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, + "Souris = %d; Fromages = %d; Obstacles = %d ", + MouseCount, CheeseCount, ObstacleCount); +} + +//***************************************************************************** +// But de la fonction : Initialiser l'entree de la carte +// Entree : +// pMap : Pointeur sur la carte +// Parametre retourne : ERROR si memoire insuffisante, TRUE sinon +//***************************************************************************** +static sos_ret_t InitMapInput(Element_t * * pMap) +{ + Element_t * pElement; + + //Definir le point d'entree + pElement = (Element_t *)sos_kmalloc(sizeof(Element_t), 0); + if(pElement == NULL) return -SOS_ENOMEM; + + //Initialiser l'entree + pElement->Type = INPUT; + pElement->Status = 0; + pElement->Color = SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE; + pElement->P.X = 0; + pElement->P.Y = MAP_Y / 2; + pElement->ThreadID = 0; + + pMap[(pElement->P.Y * MAP_X) + pElement->P.X] = pElement; + + return SOS_OK; +} + +//***************************************************************************** +// But de la fonction : Initialiser la sortie de la carte +// Entree : +// pMap : Pointeur sur la carte +// Parametre retourne : ERROR si memoire insuffisante, TRUE sinon +//***************************************************************************** +static sos_ret_t InitMapOutput(Element_t * * pMap) +{ + Element_t * pElement; + + //Definir le point de sortie + pElement = (Element_t *)sos_kmalloc(sizeof(Element_t), 0); + if(pElement == NULL) return -SOS_ENOMEM; + + //Initialiser l'entree + pElement->Type = OUTPUT; + pElement->Status = 0; + pElement->Color = SOS_X86_VIDEO_FG_LTBLUE; + pElement->P.X = MAP_X - 1; + pElement->P.Y = MAP_Y / 2; + pElement->ThreadID = 0; + + pMap[(pElement->P.Y * MAP_X) + pElement->P.X] = pElement; + + return SOS_OK; +} + +//***************************************************************************** +// But de la fonction : Initialiser un type d'objet sur la carte +// Entree : +// pMap : Pointeur sur la carte +// Type : Type d'objet a initialiser +// Parametre retourne : ERROR si memoire insuffisante, TRUE sinon +//***************************************************************************** +static sos_ret_t ElementInit(Element_t * * pMap, unsigned int Type) +{ + unsigned int I, J; + unsigned int Max; + Color_t Color; + + if(Type == CHEESE) + {//Type d'element = fromage + Max = CHEESE_COUNT; + Color = SOS_X86_VIDEO_FG_YELLOW; + } + else if(Type == OBSTACLE) + {//Type d'element = Obstacle + Max = OBSTACLE_COUNT; + Color = SOS_X86_VIDEO_FG_GREEN; + } + else + {//Aucune autre type reconnu + return -SOS_EINVAL; + } + + for(I = 0; I < Max; I++) + {//Tirer les fromages + J = random(); + J += random(); + J %= MAP_SIZE; + if(pMap[J] == NULL) + {//Si l'emplacement est libre + pMap[J] = (Element_t *)sos_kmalloc(sizeof(Element_t), + 0); + if(pMap[J] == NULL) return -SOS_ENOMEM; + + pMap[J]->Type = Type; + //Initialiser l'element + if(Type == CHEESE) + {//Type d'element = fromage + CheeseCount++; + } + else if(Type == OBSTACLE) + {//Type d'element = Obstacle + ObstacleCount++; + } + + pMap[J]->Color = Color; + pMap[J]->Status = 0; + pMap[J]->Color = Color; + pMap[J]->P.X = J % MAP_X; + pMap[J]->P.Y = J / MAP_X; + pMap[J]->ThreadID = 0; + } + } + + return SOS_OK; +} + + +//***************************************************************************** +// But du thread : Deplacer la souris sur la carte selon les regles etablies. +// Regles : +// - La souris doit se placer devant l'entree puis commence a recolter du +// fromage. +// - Des que la souris a ramasse un morceau de fromage, elle doit aller en +// entree de la carte afin de deposer sa recolte. +// - Si une souris a prouve sa recolte, une autre souris est creee. +// - Si une souris prend la sortie, elle est eliminee. +//***************************************************************************** +static void Mouse(unsigned long Param) +{ + Element_t * pMouse = (Element_t *)Param; + Point_t P; + + SOS_ASSERT_FATAL(pMouse != NULL); + + //Position de depart de la souris + P = pMouse->P; + P = pMouse->P; + + while(1) + { + int delay_ms; + struct sos_time delay; + + //La souris doit se deplacer + sos_ksema_down(& SemMap, NULL); + + MouseMove(&P); + + sos_ksema_up(& SemMap); + + // Est-ce que la souris est sortie ? + if (IsSet(pMouse->Status, MOUSE_EXITED)) + // Oui => on sort + break; + + // Delai entre MOUSE_SPEED_MIN et MouseSpeed - 1 + delay_ms = MOUSE_SPEED_MIN + (random() % MouseSpeed); + delay.sec = delay_ms / 1000; + delay.nanosec = (delay_ms % 1000) * 1000000; + sos_thread_sleep(& delay); + } + + // Libere la structure associee + sos_kfree((sos_vaddr_t)pMouse); +} + +//***************************************************************************** +// But de la procedure : Deplacer la souris de maniere aleatoire sur la carte +// Entrees : +// P : Position courante de la souris +// Sorties : +// P : Position suivante de la souris +//***************************************************************************** +static void MouseMove(Point_t * P) +{ + Point_t Org; + Point_t p; + Point_t Cheese; + int Positions[8]; + int Count = 0; + Element_t * pMouse; + + Org = *P; + + pMouse = pMap[Org.X + (Org.Y * MAP_X)]; + + Count = EvaluatePositions(Org, Positions, &Cheese); + + if(Count == 0) return; + + p = Org; + + if(IsSet(pMouse->Status, CHEESE_FOUND)) + {//Prendre le fromage + Reset(pMouse->Status, CHEESE_FOUND); + p = Cheese; + } + else + {//Choisir une position au hasard + p = ChoosePosition(pMouse, Positions, Count); + } + if(AffectMovement(Org, p) == FALSE) return; + //Deplacer la souris + pMap[Org.X + (Org.Y * MAP_X)] = NULL; + pMap[p.X + (p.Y * MAP_X)] = pMouse; + pMouse->P = p; + //Mettre a jour l'affichage + DrawPixel(Org.X, Org.Y, SOS_X86_VIDEO_FG_BLACK); + DrawPixel(p.X, p.Y, pMouse->Color); + sos_x86_videomem_printf( 23,0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, "Souris = %d; Fromages = %d; Obstacles = %d ", MouseCount, CheeseCount, ObstacleCount); + //Mettre a jour les coordonnees + *P = p; +} + +//***************************************************************************** +// But de la fonction : Choisir un mouvement +// Entree : +// pMouse : Pointeur sur la souris +// Positions : Tableau de position possible +// Count :Nombre de positions valides +// Sortie : Aucune +// Parametre retourne : Position choisie +//***************************************************************************** +static Point_t ChoosePosition(Element_t * pMouse, int Positions[], int Count) +{ + int I, J; + Point_t p; + + for(J = 0; J < Count; J++) + {//Chercher dans le tableau si cette position est disponible + I = Positions[J]; + if(I == pMouse->Way) + {//Poursuivre ce sens d'avance + p = pMouse->P; + switch(I) + { + case 0: + p.Y++; + break; + case 1: + p.X++; + p.Y++; + break; + case 2: + p.X++; + break; + case 3: + p.Y--; + p.X++; + break; + case 4: + p.Y--; + break; + case 5: + p.Y--; + p.X--; + break; + case 6: + p.X--; + break; + case 7: + p.X--; + p.Y++; + break; + } + return p; + } + } + + J = random() % Count; + I = Positions[J]; + if(((I + 4) % 8) == pMouse->Way) + {//Eviter le sens inverse + J = (J + 1) % Count; + I = Positions[J]; + } + + p = pMouse->P; + switch(I) + {//Repere le deplacement + case 0: + p.Y++; + break; + case 1: + p.X++; + p.Y++; + break; + case 2: + p.X++; + break; + case 3: + p.Y--; + p.X++; + break; + case 4: + p.Y--; + break; + case 5: + p.Y--; + p.X--; + break; + case 6: + p.X--; + break; + case 7: + p.X--; + p.Y++; + break; + } + + pMouse->Way = I;//Memoriser la direction selectionnee + + return p; +} + +//***************************************************************************** +// But de la fonction : Evaluer les positions possibles et les memoriser dans +// un tableau de positions si aucun fromage n'a ete detecte. Si du fromage a +// ete detecte, il sera selectionne en premier. La presence d'un fromage est +// indiquee par le drapeau CHEESE_FOUND +// Entree : +// Org : Position de la souris +// Sorties : +// Positions : Tableau de positions valides +// Cheese : Position du fromage +// Parametre retourne : Nombre de positions valides +//***************************************************************************** +static int EvaluatePositions(Point_t Org, int Positions[], Point_t * Cheese) +{ + int I; + int Count = 0; + Point_t p; + Point_t CheesePos; + + for(I = 0; I < 8; I++) + {//Explorer toute les directions + p = Org; + switch(I) + {//Repere le deplacement + case 0: + p.Y++; + break; + case 1: + p.X++; + p.Y++; + break; + case 2: + p.X++; + break; + case 3: + p.Y--; + p.X++; + break; + case 4: + p.Y--; + break; + case 5: + p.Y--; + p.X--; + break; + case 6: + p.X--; + break; + case 7: + p.X--; + p.Y++; + break; + } + //Tester la collision + if(IsCollision(Org, p, &CheesePos) == FALSE) + {//La souris n'a rencontre aucun obstacle + Positions[Count] = I; + Count++; + } + } + + *Cheese = CheesePos; + + return Count; +} + +//***************************************************************************** +// But de la fonction : Affecter un mouvement a la souris +// Entrees : +// Org : Coordonnees de la souris +// p : Coordonnees voulu par la souris +// Parametre retourne : TRUE si le mouvement a eu lieu, FALSE sinon +//***************************************************************************** +static sos_bool_t AffectMovement(Point_t Org, Point_t p) +{ + Element_t * pMouse = pMap[Org.X + (Org.Y * MAP_X)]; + Element_t * pElement; + + pElement = pMap[p.X + (p.Y * MAP_X)]; + + //La place est libre + if(pElement == NULL) return TRUE;//Autoriser le mouvement + + switch(pElement->Type) + { + case CHEESE: + // Liberer l'emplacement memoire du fromage + sos_kfree((sos_vaddr_t)pElement); + pMap[p.X + (p.Y * MAP_X)] = NULL; + + //Donner le fromage a la souris + Set(pMouse->Status, MOUSE_FULL); + Reset(pMouse->Status, MOUSE_EMPTY); + pMouse->Color = SOS_X86_VIDEO_FG_MAGENTA; + CheeseCount--; + return TRUE; + case OUTPUT: + //Supprimer la souris + pMap[Org.X + (Org.Y * MAP_X)] = NULL; + MouseCount--; + DrawPixel(Org.X, Org.Y, SOS_X86_VIDEO_FG_BLACK); + sos_x86_videomem_printf( 23,0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, + "Souris = %d; Fromages = %d; Obstacles = %d ", + MouseCount, CheeseCount, + ObstacleCount); + Set(pMouse->Status, MOUSE_EXITED); + return FALSE; + default : + return FALSE; + } + + return FALSE; +} + +//***************************************************************************** +// But de la fonction : Tester si une collision a eu lieu avec un obstacle +// Entrees : +// Org : Coordonnees de la souris +// p : coordonnees desirees par la souris +// Sortie : +// Cheese : Coordonnees du fromage +// Parametre retourne : TRUE si une collision a eu lieu, FALSE sinon +//***************************************************************************** +static sos_bool_t IsCollision(Point_t Org, Point_t p, Point_t *Cheese) +{ + Element_t * pMouse = pMap[Org.X + (Org.Y * MAP_X)]; + Element_t * pElement; + + //Tester les bordures de la map + if((p.X < 0)||(p.Y < 0)) return TRUE; + + if((p.Y >= MAP_Y)||(p.X >= MAP_X)) return TRUE; + + pElement = pMap[p.X + (p.Y * MAP_X)]; + + //L'element est vide + if(pElement == NULL) return FALSE; + + //Si du fromage a ete trouve, stopper la recherche + if(IsSet(pMouse->Status, CHEESE_FOUND)) return FALSE; + + switch(pElement->Type) + { + case CHEESE: + if(IsSet(pMouse->Status, MOUSE_FULL)) return TRUE; + //Indiquer que du fromage a ete trouve + Set(pMouse->Status, CHEESE_FOUND); + //Retenir la position du fromage + (*Cheese).X = p.X; + (*Cheese).Y = p.Y; + break; + case INPUT: + if(IsSet(pMouse->Status, MOUSE_EMPTY)) return TRUE; + //Remplir les reserves de fromage + Set(pMouse->Status, MOUSE_EMPTY); + Reset(pMouse->Status, MOUSE_FULL); + pMouse->Color = SOS_X86_VIDEO_FG_LTRED; + //Autoriser la creation d'une autre souris + sos_ksema_up(& SemMouse); + return TRUE; + case OUTPUT: + break; + default : + return TRUE; + } + + return FALSE;//Aucune collision +} + +//***************************************************************************** +// But du thread : Creer une souris et la placer autour de l'entree +//***************************************************************************** +static void MouseCreator(void) +{ + while(1) + { + sos_ksema_down(& SemMouse, NULL); + sos_ksema_down(& SemMap, NULL); + CreateMouse(); + sos_ksema_up(& SemMap); + } +} + +//***************************************************************************** +// But de la fonction : Creer une souris et l'inserer dans la carte +// Entree : Aucune +// Parametre retourne : ERROR si memoire insuffisante, FALSE si souris non +// cree, TRUE sinon +//***************************************************************************** +static sos_ret_t CreateMouse(void) +{ + Element_t * pElement; + unsigned int I; + + Point_t p; + + for(I = 0; I < 8; I++) + {//Explorer tous les emplacements + p.X = 0; + p.Y = MAP_Y / 2; + switch(I) + {//Repere le deplacement + case 0: + p.Y++; + break; + case 1: + p.X++; + p.Y++; + break; + case 2: + p.X++; + break; + case 3: + p.Y--; + p.X++; + break; + case 4: + p.Y--; + break; + case 5: + p.Y--; + p.X--; + break; + case 6: + p.X--; + break; + case 7: + p.X--; + p.Y++; + break; + } + if((p.X >= 0)&&(p.Y >= 0)&&(p.X < MAP_X)&&(p.Y < MAP_Y)) + {//L'emplacement est valide + pElement = pMap[p.X + (p.Y * MAP_X)]; + if(pElement == NULL) + {//Creer la souris + pElement = (Element_t *)sos_kmalloc(sizeof(Element_t), 0); + if(pElement != NULL) + {//Initialiser l'entree + pElement->Type = MOUSE; + Set(pElement->Status, MOUSE_EMPTY); + pElement->Color = SOS_X86_VIDEO_FG_LTRED; + pElement->P = p; + pElement->Way = 0; + pElement->ThreadID + = sos_create_kernel_thread("Mouse", + (sos_kernel_thread_start_routine_t)Mouse, + pElement); + if(pElement->ThreadID == 0) + { + sos_kfree((sos_vaddr_t)pElement); + pElement = NULL; + return -SOS_ENOMEM; + } + pMap[p.X + (p.Y * MAP_X)] = pElement; + MouseCount++; + + DrawPixel(p.X, p.Y, pElement->Color); + sos_x86_videomem_printf(23, 0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, "Souris = %d; Fromages = %d; Obstacles = %d ", MouseCount, CheeseCount, ObstacleCount); + + return SOS_OK; + } + } + } + } + return -SOS_EBUSY; +} + +//***************************************************************************** +// C'est fini !!!! +//***************************************************************************** diff --git a/sos-code-article6.5/sos/physmem.c b/sos-code-article6.5/sos/physmem.c new file mode 100644 index 0000000..daf730f --- /dev/null +++ b/sos-code-article6.5/sos/physmem.c @@ -0,0 +1,319 @@ +/* Copyright (C) 2004 David Decotigny + Copyright (C) 2000 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#include <sos/list.h> +#include <sos/macros.h> +#include <sos/assert.h> +#include <sos/klibc.h> + +#include "physmem.h" + +/** A descriptor for a physical page in SOS */ +struct physical_page_descr +{ + /** The physical base address for the page */ + sos_paddr_t paddr; + + /** The reference count for this physical page. > 0 means that the + page is in the used list. */ + sos_count_t ref_cnt; + + /** Some data associated with the page when it is mapped in kernel space */ + struct sos_kmem_range *kernel_range; + + /** The other pages on the list (used, free) */ + struct physical_page_descr *prev, *next; +}; + +/** These are some markers present in the executable file (see sos.lds) */ +extern char __b_kernel, __e_kernel; + +/** The array of ppage descriptors will be located at this address */ +#define PAGE_DESCR_ARRAY_ADDR \ + SOS_PAGE_ALIGN_SUP((sos_paddr_t) (& __e_kernel)) +static struct physical_page_descr * physical_page_descr_array; + +/** The list of physical pages currently available */ +static struct physical_page_descr *free_ppage; + +/** The list of physical pages currently in use */ +static struct physical_page_descr *used_ppage; + +/** We will store here the interval of valid physical addresses */ +static sos_paddr_t physmem_base, physmem_top; + +/** We store the number of pages used/free */ +static sos_count_t physmem_total_pages, physmem_used_pages; + +sos_ret_t sos_physmem_subsystem_setup(sos_size_t ram_size, + /* out */sos_paddr_t *kernel_core_base, + /* out */sos_paddr_t *kernel_core_top) +{ + /* The iterator over the page descriptors */ + struct physical_page_descr *ppage_descr; + + /* The iterator over the physical addresses */ + sos_paddr_t ppage_addr; + + /* Make sure ram size is aligned on a page boundary */ + ram_size = SOS_PAGE_ALIGN_INF(ram_size);/* Yes, we may lose at most a page */ + + /* Reset the used/free page lists before building them */ + free_ppage = used_ppage = NULL; + physmem_total_pages = physmem_used_pages = 0; + + /* Make sure that there is enough memory to store the array of page + descriptors */ + *kernel_core_base = SOS_PAGE_ALIGN_INF((sos_paddr_t)(& __b_kernel)); + *kernel_core_top + = PAGE_DESCR_ARRAY_ADDR + + SOS_PAGE_ALIGN_SUP( (ram_size >> SOS_PAGE_SHIFT) + * sizeof(struct physical_page_descr)); + if (*kernel_core_top > ram_size) + return -SOS_ENOMEM; + + /* Page 0-4kB is not available in order to return address 0 as a + means to signal "no page available" */ + physmem_base = SOS_PAGE_SIZE; + physmem_top = ram_size; + + /* Setup the page descriptor arrray */ + physical_page_descr_array + = (struct physical_page_descr*)PAGE_DESCR_ARRAY_ADDR; + + /* Scan the list of physical pages */ + for (ppage_addr = 0, + ppage_descr = physical_page_descr_array ; + ppage_addr < physmem_top ; + ppage_addr += SOS_PAGE_SIZE, + ppage_descr ++) + { + enum { PPAGE_MARK_RESERVED, PPAGE_MARK_FREE, + PPAGE_MARK_KERNEL, PPAGE_MARK_HWMAP } todo; + + memset(ppage_descr, 0x0, sizeof(struct physical_page_descr)); + + /* Init the page descriptor for this page */ + ppage_descr->paddr = ppage_addr; + + /* Reserved : 0 ... base */ + if (ppage_addr < physmem_base) + todo = PPAGE_MARK_RESERVED; + + /* Free : base ... BIOS */ + else if ((ppage_addr >= physmem_base) + && (ppage_addr < BIOS_N_VIDEO_START)) + todo = PPAGE_MARK_FREE; + + /* Used : BIOS */ + else if ((ppage_addr >= BIOS_N_VIDEO_START) + && (ppage_addr < BIOS_N_VIDEO_END)) + todo = PPAGE_MARK_HWMAP; + + /* Free : BIOS ... kernel */ + else if ((ppage_addr >= BIOS_N_VIDEO_END) + && (ppage_addr < (sos_paddr_t) (& __b_kernel))) + todo = PPAGE_MARK_FREE; + + /* Used : Kernel code/data/bss + physcal page descr array */ + else if ((ppage_addr >= *kernel_core_base) + && (ppage_addr < *kernel_core_top)) + todo = PPAGE_MARK_KERNEL; + + /* Free : first page of descr ... end of RAM */ + else + todo = PPAGE_MARK_FREE; + + /* Actually does the insertion in the used/free page lists */ + physmem_total_pages ++; + switch (todo) + { + case PPAGE_MARK_FREE: + ppage_descr->ref_cnt = 0; + list_add_head(free_ppage, ppage_descr); + break; + + case PPAGE_MARK_KERNEL: + case PPAGE_MARK_HWMAP: + ppage_descr->ref_cnt = 1; + list_add_head(used_ppage, ppage_descr); + physmem_used_pages ++; + break; + + default: + /* Reserved page: nop */ + break; + } + } + + return SOS_OK; +} + + +sos_paddr_t sos_physmem_ref_physpage_new(sos_bool_t can_block) +{ + struct physical_page_descr *ppage_descr; + + if (! free_ppage) + return (sos_paddr_t)NULL; + + /* Retrieve a page in the free list */ + ppage_descr = list_pop_head(free_ppage); + + /* The page is assumed not to be already used */ + SOS_ASSERT_FATAL(ppage_descr->ref_cnt == 0); + + /* Mark the page as used (this of course sets the ref count to 1) */ + ppage_descr->ref_cnt ++; + + /* No associated kernel range by default */ + ppage_descr->kernel_range = NULL; + + /* Put the page in the used list */ + list_add_tail(used_ppage, ppage_descr); + physmem_used_pages ++; + + return ppage_descr->paddr; +} + + +/** + * Helper function to get the physical page descriptor for the given + * physical page address. + * + * @return NULL when out-of-bounds or non-page-aligned + */ +inline static struct physical_page_descr * +get_page_descr_at_paddr(sos_paddr_t ppage_paddr) +{ + /* Don't handle non-page-aligned addresses */ + if (ppage_paddr & SOS_PAGE_MASK) + return NULL; + + /* Don't support out-of-bounds requests */ + if ((ppage_paddr < physmem_base) || (ppage_paddr >= physmem_top)) + return NULL; + + return physical_page_descr_array + (ppage_paddr >> SOS_PAGE_SHIFT); +} + + +sos_ret_t sos_physmem_ref_physpage_at(sos_paddr_t ppage_paddr) +{ + struct physical_page_descr *ppage_descr + = get_page_descr_at_paddr(ppage_paddr); + + if (! ppage_descr) + return -SOS_EINVAL; + + /* Increment the reference count for the page */ + ppage_descr->ref_cnt ++; + + /* If the page is newly referenced (ie we are the only owners of the + page => ref cnt == 1), transfer it in the used pages list */ + if (ppage_descr->ref_cnt == 1) + { + list_delete(free_ppage, ppage_descr); + + /* No associated kernel range by default */ + ppage_descr->kernel_range = NULL; + + list_add_tail(used_ppage, ppage_descr); + physmem_used_pages ++; + + /* The page is newly referenced */ + return FALSE; + } + + /* The page was already referenced by someone */ + return TRUE; +} + + +sos_ret_t +sos_physmem_unref_physpage(sos_paddr_t ppage_paddr) +{ + /* By default the return value indicates that the page is still + used */ + sos_ret_t retval = FALSE; + + struct physical_page_descr *ppage_descr + = get_page_descr_at_paddr(ppage_paddr); + + if (! ppage_descr) + return -SOS_EINVAL; + + /* Don't do anything if the page is not in the used list */ + if (ppage_descr->ref_cnt <= 0) + return -SOS_EINVAL; + + /* Unreference the page, and, when no mapping is active anymore, put + the page in the free list */ + ppage_descr->ref_cnt--; + if (ppage_descr->ref_cnt <= 0) + { + /* Reset associated kernel range */ + ppage_descr->kernel_range = NULL; + + /* Transfer the page, considered USED, to the free list */ + list_delete(used_ppage, ppage_descr); + physmem_used_pages --; + list_add_head(free_ppage, ppage_descr); + + /* Indicate that the page is now unreferenced */ + retval = TRUE; + } + + return retval; +} + + +struct sos_kmem_range* sos_physmem_get_kmem_range(sos_paddr_t ppage_paddr) +{ + struct physical_page_descr *ppage_descr + = get_page_descr_at_paddr(ppage_paddr); + + if (! ppage_descr) + return NULL; + + return ppage_descr->kernel_range; +} + + +sos_ret_t sos_physmem_set_kmem_range(sos_paddr_t ppage_paddr, + struct sos_kmem_range *range) +{ + struct physical_page_descr *ppage_descr + = get_page_descr_at_paddr(ppage_paddr); + + if (! ppage_descr) + return -SOS_EINVAL; + + ppage_descr->kernel_range = range; + return SOS_OK; +} + +sos_ret_t sos_physmem_get_state(/* out */sos_count_t *total_ppages, + /* out */sos_count_t *used_ppages) +{ + if (total_ppages) + *total_ppages = physmem_total_pages; + if (used_ppages) + *used_ppages = physmem_used_pages; + return SOS_OK; +} diff --git a/sos-code-article6.5/sos/physmem.h b/sos-code-article6.5/sos/physmem.h new file mode 100644 index 0000000..7b4cd2b --- /dev/null +++ b/sos-code-article6.5/sos/physmem.h @@ -0,0 +1,147 @@ +/* Copyright (C) 2004 David Decotigny + Copyright (C) 2000 The KOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_PHYSMEM_H_ +#define _SOS_PHYSMEM_H_ + +/** + * @file physmem.h + * + * Physical pages of memory + */ + +#include <sos/errno.h> +#include <sos/types.h> +#include <sos/macros.h> + +/** The size of a physical page (arch-dependent) */ +#define SOS_PAGE_SIZE (4*1024) + +/** The corresponding shift */ +#define SOS_PAGE_SHIFT 12 /* 4 kB = 2^12 B */ + +/** The corresponding mask */ +#define SOS_PAGE_MASK ((1<<12) - 1) + +#define SOS_PAGE_ALIGN_INF(val) \ + SOS_ALIGN_INF((val), SOS_PAGE_SIZE) +#define SOS_PAGE_ALIGN_SUP(val) \ + SOS_ALIGN_SUP((val), SOS_PAGE_SIZE) +#define SOS_IS_PAGE_ALIGNED(val) \ + SOS_IS_ALIGNED((val), SOS_PAGE_SIZE) + +/** + * This is the reserved physical interval for the x86 video memory and + * BIOS area. In physmem.c, we have to mark this area as "used" in + * order to prevent from allocating it. And in paging.c, we'd better + * map it in virtual space if we really want to be able to print to + * the screen (for debugging purpose, at least): for this, the + * simplest is to identity-map this area in virtual space (note + * however that this mapping could also be non-identical). + */ +#define BIOS_N_VIDEO_START 0xa0000 +#define BIOS_N_VIDEO_END 0x100000 + + +/** + * Initialize the physical memory subsystem, for the physical area [0, + * ram_size). This routine takes into account the BIOS and video + * areas, to prevent them from future allocations. + * + * @param ram_size The size of the RAM that will be managed by this subsystem + * + * @param kernel_core_base The lowest address for which the kernel + * assumes identity mapping (ie virtual address == physical address) + * will be stored here + * + * @param kernel_core_top The top address for which the kernel + * assumes identity mapping (ie virtual address == physical address) + * will be stored here + */ +sos_ret_t sos_physmem_subsystem_setup(sos_size_t ram_size, + /* out */sos_paddr_t *kernel_core_base, + /* out */sos_paddr_t *kernel_core_top); + +/** + * Retrieve the total number of pages, and the number of free pages + */ +sos_ret_t sos_physmem_get_state(/* out */sos_count_t *total_ppages, + /* out */sos_count_t *used_ppages); + + +/** + * Get a free page. + * + * @return The (physical) address of the (physical) page allocated, or + * NULL when none currently available. + * + * @param can_block TRUE if the function is allowed to block + * @note The page returned has a reference count equal to 1. + */ +sos_paddr_t sos_physmem_ref_physpage_new(sos_bool_t can_block); + + +/** + * Increment the reference count of a given physical page. Useful for + * VM code which tries to map a precise physical address. + * + * @param ppage_paddr Physical address of the page (MUST be page-aligned) + * + * @return TRUE when the page was previously in use, FALSE when the + * page was previously in the free list, <0 when the page address is + * invalid. + */ +sos_ret_t sos_physmem_ref_physpage_at(sos_paddr_t ppage_paddr); + + +/** + * Decrement the reference count of the given physical page. When this + * reference count reaches 0, the page is marked free, ie is available + * for future sos_physmem_get_physpage() + * + * @param ppage_paddr Physical address of the page (MUST be page-aligned) + * + * @return FALSE when the page is still in use, TRUE when the page is now + * unreferenced, <0 when the page address is invalid + */ +sos_ret_t sos_physmem_unref_physpage(sos_paddr_t ppage_paddr); + + +#include <sos/kmem_vmm.h> + +/** + * Return the kernel memory allocation range associated with the given + * physical page, or NULL when page has no associated range + * + * @param ppage_paddr Physical address of the page (MUST be page-aligned) + */ +struct sos_kmem_range* sos_physmem_get_kmem_range(sos_paddr_t ppage_paddr); + + +/** + * Set the kernel memory allocation range associated to the given + * physical page. + * + * @param ppage_paddr Physical address of the page (MUST be page-aligned) + * + * @return error if page is invalid + */ +sos_ret_t sos_physmem_set_kmem_range(sos_paddr_t ppage_paddr, + struct sos_kmem_range *range); + +#endif /* _SOS_PHYSMEM_H_ */ diff --git a/sos-code-article6.5/sos/sched.c b/sos-code-article6.5/sos/sched.c new file mode 100644 index 0000000..b68dcaf --- /dev/null +++ b/sos-code-article6.5/sos/sched.c @@ -0,0 +1,133 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +#include <sos/errno.h> +#include <sos/klibc.h> +#include <sos/assert.h> +#include <sos/list.h> + +#include "sched.h" + + +/** + * The definition of the scheduler queue. We could have used a normal + * kwaitq here, it would have had the same properties. But, in the + * definitive version (O(1) scheduler), the structure has to be a bit + * more complicated. So, in order to keep the changes as small as + * possible between this version and the definitive one, we don't use + * kwaitq here. + */ +static struct +{ + unsigned int nr_threads; + struct sos_thread *thread_list; +} ready_queue; + + +sos_ret_t sos_sched_subsystem_setup() +{ + memset(& ready_queue, 0x0, sizeof(ready_queue)); + + return SOS_OK; +} + + +/** + * Helper function to add a thread in a ready queue AND to change the + * state of the given thread to "READY". + * + * @param insert_at_tail TRUE to tell to add the thread at the end of + * the ready list. Otherwise it is added at the head of it. + */ +static sos_ret_t add_in_ready_queue(struct sos_thread *thr, + sos_bool_t insert_at_tail) +{ + + SOS_ASSERT_FATAL( (SOS_THR_CREATED == thr->state) + || (SOS_THR_RUNNING == thr->state) /* Yield */ + || (SOS_THR_BLOCKED == thr->state) ); + + /* Add the thread to the CPU queue */ + if (insert_at_tail) + list_add_tail_named(ready_queue.thread_list, thr, + ready.rdy_prev, ready.rdy_next); + else + list_add_head_named(ready_queue.thread_list, thr, + ready.rdy_prev, ready.rdy_next); + ready_queue.nr_threads ++; + + /* Ok, thread is now really ready to be (re)started */ + thr->state = SOS_THR_READY; + + return SOS_OK; +} + + +sos_ret_t sos_sched_set_ready(struct sos_thread *thr) +{ + sos_ret_t retval; + + /* Don't do anything for already ready threads */ + if (SOS_THR_READY == thr->state) + return SOS_OK; + + /* Real-time thread: schedule it for the present turn */ + retval = add_in_ready_queue(thr, TRUE); + + return retval; +} + + +struct sos_thread * sos_reschedule(struct sos_thread *current_thread, + sos_bool_t do_yield) +{ + + if (SOS_THR_ZOMBIE == current_thread->state) + { + /* Don't think of returning to this thread since it is + terminated */ + /* Nop */ + } + else if (SOS_THR_BLOCKED != current_thread->state) + { + /* Take into account the current executing thread unless it is + marked blocked */ + if (do_yield) + /* Ok, reserve it for next turn */ + add_in_ready_queue(current_thread, TRUE); + else + /* Put it at the head of the active list */ + add_in_ready_queue(current_thread, FALSE); + } + + /* The next thread is that at the head of the ready list */ + if (ready_queue.nr_threads > 0) + { + struct sos_thread *next_thr; + + /* Queue is not empty: take the thread at its head */ + next_thr = list_pop_head_named(ready_queue.thread_list, + ready.rdy_prev, ready.rdy_next); + ready_queue.nr_threads --; + + return next_thr; + } + + SOS_FATAL_ERROR("No kernel thread ready ?!"); + return NULL; +} diff --git a/sos-code-article6.5/sos/sched.h b/sos-code-article6.5/sos/sched.h new file mode 100644 index 0000000..123fe7d --- /dev/null +++ b/sos-code-article6.5/sos/sched.h @@ -0,0 +1,75 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_SCHED_H_ +#define _SOS_SCHED_H_ + + +/** + * @file sched.h + * + * A basic scheduler with simple FIFO threads' ordering. + * + * The functions below manage CPU queues, and are NEVER responsible + * for context switches (see thread.h for that) or synchronizations + * (see kwaitq.h or the higher levels primitives [mutex, semaphore, + * ...] for that). + * + * @note IMPORTANT: all the functions below are meant to be called + * ONLY by the thread/timer/kwaitq subsystems. DO NOT use them + * directly from anywhere else: use ONLY the thread/kwaitq functions! + * If you still want to call them directly despite this disclaimer, + * simply disable interrupts before clling them. + */ + +#include <sos/errno.h> + + +#include <sos/thread.h> + + +/** + * Initialize the scheduler + * + * @note: The use of this function is RESERVED + */ +sos_ret_t sos_sched_subsystem_setup(); + + +/** + * Mark the given thread as ready + * + * @note: The use of this function is RESERVED + */ +sos_ret_t sos_sched_set_ready(struct sos_thread * thr); + + +/** + * Return the identifier of the next thread to run. Also removes it + * from the ready list, but does NOT set is as current_thread ! + * + * @param current_thread TCB of the thread calling the function + * + * @param do_yield When TRUE, put the current executing thread at the + * end of the ready list. Otherwise it is kept at the head of it. + * + * @note: The use of this function is RESERVED + */ +struct sos_thread * sos_reschedule(struct sos_thread * current_thread, + sos_bool_t do_yield); + +#endif /* _SOS_WAITQUEUE_H_ */ diff --git a/sos-code-article6.5/sos/thread.c b/sos-code-article6.5/sos/thread.c new file mode 100644 index 0000000..766b804 --- /dev/null +++ b/sos-code-article6.5/sos/thread.c @@ -0,0 +1,441 @@ +/* Copyright (C) 2004,2005 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +#include <sos/physmem.h> +#include <sos/kmem_slab.h> +#include <sos/kmalloc.h> +#include <sos/klibc.h> +#include <sos/list.h> +#include <sos/assert.h> + +#include <hwcore/irq.h> + +#include "thread.h" + + +/** + * The size of the stack of a kernel thread + */ +#define SOS_THREAD_KERNEL_STACK_SIZE (1*SOS_PAGE_SIZE) + + +/** + * The identifier of the thread currently running on CPU. + * + * We only support a SINGLE processor, ie a SINGLE thread + * running at any time in the system. This greatly simplifies the + * implementation of the system, since we don't have to complicate + * things in order to retrieve the identifier of the threads running + * on the CPU. On multiprocessor systems the current_thread below is + * an array indexed by the id of the CPU, so that the challenge is to + * retrieve the identifier of the CPU. This is usually done based on + * the stack address (Linux implementation) or on some form of TLS + * ("Thread Local Storage": can be implemented by way of LDTs for the + * processes, accessed through the fs or gs registers). + */ +static volatile struct sos_thread *current_thread = NULL; + + +/* + * The list of threads currently in the system. + * + * @note We could have used current_thread for that... + */ +static struct sos_thread *thread_list = NULL; + + +/** + * The Cache of thread structures + */ +static struct sos_kslab_cache *cache_thread; + + +struct sos_thread *sos_thread_get_current() +{ + SOS_ASSERT_FATAL(current_thread->state == SOS_THR_RUNNING); + return (struct sos_thread*)current_thread; +} + + +inline static sos_ret_t _set_current(struct sos_thread *thr) +{ + SOS_ASSERT_FATAL(thr->state == SOS_THR_READY); + current_thread = thr; + current_thread->state = SOS_THR_RUNNING; + return SOS_OK; +} + + +sos_ret_t sos_thread_subsystem_setup(sos_vaddr_t init_thread_stack_base_addr, + sos_size_t init_thread_stack_size) +{ + struct sos_thread *myself; + + /* Allocate the cache of threads */ + cache_thread = sos_kmem_cache_create("thread", + sizeof(struct sos_thread), + 2, + 0, + SOS_KSLAB_CREATE_MAP + | SOS_KSLAB_CREATE_ZERO); + if (! cache_thread) + return -SOS_ENOMEM; + + /* Allocate a new thread structure for the current running thread */ + myself = (struct sos_thread*) sos_kmem_cache_alloc(cache_thread, + SOS_KSLAB_ALLOC_ATOMIC); + if (! myself) + return -SOS_ENOMEM; + + /* Initialize the thread attributes */ + strzcpy(myself->name, "[kinit]", SOS_THR_MAX_NAMELEN); + myself->state = SOS_THR_CREATED; + myself->kernel_stack_base_addr = init_thread_stack_base_addr; + myself->kernel_stack_size = init_thread_stack_size; + + /* Do some stack poisoning on the bottom of the stack, if needed */ + sos_cpu_state_prepare_detect_kernel_stack_overflow(myself->cpu_state, + myself->kernel_stack_base_addr, + myself->kernel_stack_size); + + /* Add the thread in the global list */ + list_singleton_named(thread_list, myself, gbl_prev, gbl_next); + + /* Ok, now pretend that the running thread is ourselves */ + myself->state = SOS_THR_READY; + _set_current(myself); + + return SOS_OK; +} + + +struct sos_thread * +sos_create_kernel_thread(const char *name, + sos_kernel_thread_start_routine_t start_func, + void *start_arg) +{ + __label__ undo_creation; + sos_ui32_t flags; + struct sos_thread *new_thread; + + if (! start_func) + return NULL; + + /* Allocate a new thread structure for the current running thread */ + new_thread + = (struct sos_thread*) sos_kmem_cache_alloc(cache_thread, + SOS_KSLAB_ALLOC_ATOMIC); + if (! new_thread) + return NULL; + + /* Initialize the thread attributes */ + strzcpy(new_thread->name, ((name)?name:"[NONAME]"), SOS_THR_MAX_NAMELEN); + new_thread->state = SOS_THR_CREATED; + + /* Allocate the stack for the new thread */ + new_thread->kernel_stack_base_addr = sos_kmalloc(SOS_THREAD_KERNEL_STACK_SIZE, 0); + new_thread->kernel_stack_size = SOS_THREAD_KERNEL_STACK_SIZE; + if (! new_thread->kernel_stack_base_addr) + goto undo_creation; + + /* Initialize the CPU context of the new thread */ + if (SOS_OK + != sos_cpu_kstate_init(& new_thread->cpu_state, + (sos_cpu_kstate_function_arg1_t*) start_func, + (sos_ui32_t) start_arg, + new_thread->kernel_stack_base_addr, + new_thread->kernel_stack_size, + (sos_cpu_kstate_function_arg1_t*) sos_thread_exit, + (sos_ui32_t) NULL)) + goto undo_creation; + + /* Add the thread in the global list */ + sos_disable_IRQs(flags); + list_add_tail_named(thread_list, new_thread, gbl_prev, gbl_next); + sos_restore_IRQs(flags); + + /* Mark the thread ready */ + if (SOS_OK != sos_sched_set_ready(new_thread)) + goto undo_creation; + + /* Normal non-erroneous end of function */ + return new_thread; + + undo_creation: + if (new_thread->kernel_stack_base_addr) + sos_kfree((sos_vaddr_t) new_thread->kernel_stack_base_addr); + sos_kmem_cache_free((sos_vaddr_t) new_thread); + return NULL; +} + + +/** Function called after thr has terminated. Called from inside the context + of another thread, interrupts disabled */ +static void delete_thread(struct sos_thread *thr) +{ + sos_ui32_t flags; + + sos_disable_IRQs(flags); + list_delete_named(thread_list, thr, gbl_prev, gbl_next); + sos_restore_IRQs(flags); + + sos_kfree((sos_vaddr_t) thr->kernel_stack_base_addr); + memset(thr, 0x0, sizeof(struct sos_thread)); + sos_kmem_cache_free((sos_vaddr_t) thr); +} + + +void sos_thread_exit() +{ + sos_ui32_t flags; + struct sos_thread *myself, *next_thread; + + /* Interrupt handlers are NOT allowed to exit the current thread ! */ + SOS_ASSERT_FATAL(! sos_servicing_irq()); + + myself = sos_thread_get_current(); + + /* Refuse to end the current executing thread if it still holds a + resource ! */ + SOS_ASSERT_FATAL(list_is_empty_named(myself->kwaitq_list, + prev_entry_for_thread, + next_entry_for_thread)); + + /* Prepare to run the next thread */ + sos_disable_IRQs(flags); + myself->state = SOS_THR_ZOMBIE; + next_thread = sos_reschedule(myself, FALSE); + + /* Make sure that the next_thread is valid */ + sos_cpu_state_detect_kernel_stack_overflow(next_thread->cpu_state, + next_thread->kernel_stack_base_addr, + next_thread->kernel_stack_size); + + /* No need for sos_restore_IRQs() here because the IRQ flag will be + restored to that of the next thread upon context switch */ + + /* Immediate switch to next thread */ + _set_current(next_thread); + sos_cpu_context_exit_to(next_thread->cpu_state, + (sos_cpu_kstate_function_arg1_t*) delete_thread, + (sos_ui32_t) myself); +} + + +sos_thread_state_t sos_thread_get_state(struct sos_thread *thr) +{ + if (! thr) + thr = (struct sos_thread*)current_thread; + + return thr->state; +} + + +typedef enum { YIELD_MYSELF, BLOCK_MYSELF } switch_type_t; +/** + * Helper function to initiate a context switch in case the current + * thread becomes blocked, waiting for a timeout, or calls yield. + */ +static sos_ret_t _switch_to_next_thread(switch_type_t operation) +{ + struct sos_thread *myself, *next_thread; + + SOS_ASSERT_FATAL(current_thread->state == SOS_THR_RUNNING); + + /* Interrupt handlers are NOT allowed to block ! */ + SOS_ASSERT_FATAL(! sos_servicing_irq()); + + myself = (struct sos_thread*)current_thread; + + /* Make sure that if we are to be marked "BLOCKED", we have any + reason of effectively being blocked */ + if (BLOCK_MYSELF == operation) + { + myself->state = SOS_THR_BLOCKED; + } + + /* Identify the next thread */ + next_thread = sos_reschedule(myself, YIELD_MYSELF == operation); + + /* Avoid context switch if the context does not change */ + if (myself != next_thread) + { + /* Sanity checks for the next thread */ + sos_cpu_state_detect_kernel_stack_overflow(next_thread->cpu_state, + next_thread->kernel_stack_base_addr, + next_thread->kernel_stack_size); + + + /* + * Actual CPU context switch + */ + _set_current(next_thread); + sos_cpu_context_switch(& myself->cpu_state, next_thread->cpu_state); + + /* Back here ! */ + SOS_ASSERT_FATAL(current_thread == myself); + SOS_ASSERT_FATAL(current_thread->state == SOS_THR_RUNNING); + } + else + { + /* No context switch but still update ID of current thread */ + _set_current(next_thread); + } + + return SOS_OK; +} + + +sos_ret_t sos_thread_yield() +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + + retval = _switch_to_next_thread(YIELD_MYSELF); + + sos_restore_IRQs(flags); + return retval; +} + + +/** + * Internal sleep timeout management + */ +struct sleep_timeout_params +{ + struct sos_thread *thread_to_wakeup; + sos_bool_t timeout_triggered; +}; + + +/** + * Callback called when a timeout happened + */ +static void sleep_timeout(struct sos_timeout_action *act) +{ + struct sleep_timeout_params *sleep_timeout_params + = (struct sleep_timeout_params*) act->routine_data; + + /* Signal that we have been woken up by the timeout */ + sleep_timeout_params->timeout_triggered = TRUE; + + /* Mark the thread ready */ + SOS_ASSERT_FATAL(SOS_OK == + sos_thread_force_unblock(sleep_timeout_params + ->thread_to_wakeup)); +} + + +sos_ret_t sos_thread_sleep(struct sos_time *timeout) +{ + sos_ui32_t flags; + struct sleep_timeout_params sleep_timeout_params; + struct sos_timeout_action timeout_action; + sos_ret_t retval; + + /* Block forever if no timeout is given */ + if (NULL == timeout) + { + sos_disable_IRQs(flags); + retval = _switch_to_next_thread(BLOCK_MYSELF); + sos_restore_IRQs(flags); + + return retval; + } + + /* Initialize the timeout action */ + sos_time_init_action(& timeout_action); + + /* Prepare parameters used by the sleep timeout callback */ + sleep_timeout_params.thread_to_wakeup + = (struct sos_thread*)current_thread; + sleep_timeout_params.timeout_triggered = FALSE; + + sos_disable_IRQs(flags); + + /* Now program the timeout ! */ + SOS_ASSERT_FATAL(SOS_OK == + sos_time_register_action_relative(& timeout_action, + timeout, + sleep_timeout, + & sleep_timeout_params)); + + /* Prepare to block: wait for sleep_timeout() to wakeup us in the + timeout kwaitq, or for someone to wake us up in any other + waitq */ + retval = _switch_to_next_thread(BLOCK_MYSELF); + /* Unblocked by something ! */ + + /* Unblocked by timeout ? */ + if (sleep_timeout_params.timeout_triggered) + { + /* Yes */ + SOS_ASSERT_FATAL(sos_time_is_zero(& timeout_action.timeout)); + retval = SOS_OK; + } + else + { + /* No: We have probably been woken up while in some other + kwaitq */ + SOS_ASSERT_FATAL(SOS_OK == sos_time_unregister_action(& timeout_action)); + retval = -SOS_EINTR; + } + + sos_restore_IRQs(flags); + + /* Update the remaining timeout */ + memcpy(timeout, & timeout_action.timeout, sizeof(struct sos_time)); + + return retval; +} + + +sos_ret_t sos_thread_force_unblock(struct sos_thread *thread) +{ + sos_ret_t retval; + sos_ui32_t flags; + + if (! thread) + return -SOS_EINVAL; + + sos_disable_IRQs(flags); + + /* Thread already woken up ? */ + retval = SOS_OK; + switch(sos_thread_get_state(thread)) + { + case SOS_THR_RUNNING: + case SOS_THR_READY: + /* Do nothing */ + break; + + case SOS_THR_ZOMBIE: + retval = -SOS_EFATAL; + break; + + default: + retval = sos_sched_set_ready(thread); + break; + } + + sos_restore_IRQs(flags); + + return retval; +} diff --git a/sos-code-article6.5/sos/thread.h b/sos-code-article6.5/sos/thread.h new file mode 100644 index 0000000..02ea833 --- /dev/null +++ b/sos-code-article6.5/sos/thread.h @@ -0,0 +1,207 @@ +/* Copyright (C) 2004,2005 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_THREAD_H_ +#define _SOS_THREAD_H_ + +/** + * @file thread.h + * + * SOS Thread management API + */ + +#include <sos/errno.h> + +/* Forward declaration */ +struct sos_thread; + +#include <hwcore/cpu_context.h> +#include <sos/sched.h> +#include <sos/kwaitq.h> +#include <sos/time.h> + +/** + * The possible states of a valid thread + */ +typedef enum { SOS_THR_CREATED, /**< Thread created, not fully initialized */ + SOS_THR_READY, /**< Thread fully initialized or + waiting for CPU after having been + blocked or preempted */ + SOS_THR_RUNNING, /**< Thread currently running on CPU */ + SOS_THR_BLOCKED, /**< Thread waiting for I/O (+ in at LEAST + one kwaitq) and/or sleeping (+ in NO + kwaitq) */ + SOS_THR_ZOMBIE, /**< Thread terminated execution, waiting to + be deleted by kernel */ + } sos_thread_state_t; + + +/** + * TCB (Thread Control Block): structure describing a thread. Don't + * access these fields directly: prefer using the accessor functions + * below. + */ +struct sos_thread +{ +#define SOS_THR_MAX_NAMELEN 32 + char name[SOS_THR_MAX_NAMELEN]; + + sos_thread_state_t state; + + /** + * The hardware context of the thread. + * + * It will reflect the CPU state of the thread: + * - From an interrupt handler: the state of the thread at the time + * of the OUTERMOST irq. An IRQ is not allowed to make context + * switches, so this context will remain valid from the begining of + * the outermost IRQ handler to the end of it, no matter if there + * are other IRQ handlers nesting in one another. You may safely + * use it from IRQ handlers to query the state of the interrupted + * thread, no matter if there has been other IRQ handlers + * executing meanwhile. + * - From normal kernel code, exceptions and syscall: the state of + * the thread the last time there was a context switch from this + * thread to another one. Thus this field WON'T reflect the + * current's thread cpu_state in these cases. So, in these cases, + * simply DO NOT USE IT outside thread.c ! Note: for syscall and + * exception handlers, the VALID state of the interrupted thread is + * passed as an argument to the handlers. + */ + struct sos_cpu_state *cpu_state; + + /* Kernel stack parameters */ + sos_vaddr_t kernel_stack_base_addr; + sos_size_t kernel_stack_size; + + /* Data specific to each state */ + union + { + struct + { + struct sos_thread *rdy_prev, *rdy_next; + } ready; + }; /* Anonymous union (gcc extenion) */ + + + /* + * Data used by the kwaitq subsystem: list of kwaitqueues the thread + * is waiting for. + * + * @note: a RUNNING or READY thread might be in one or more + * waitqueues ! The only property we have is that, among these + * waitqueues (if any), _at least_ one has woken the thread. + */ + struct sos_kwaitq_entry *kwaitq_list; + + + /** + * Chaining pointers for global ("gbl") list of threads (debug) + */ + struct sos_thread *gbl_prev, *gbl_next; +}; + + +/** + * Definition of the function executed by a kernel thread + */ +typedef void (*sos_kernel_thread_start_routine_t)(void *arg); + + +/** + * Initialize the subsystem responsible for thread management + * + * Initialize the primary kernel thread so that it can be handled the + * same way as an ordinary thread created by sos_thread_create(). + */ +sos_ret_t sos_thread_subsystem_setup(sos_vaddr_t init_thread_stack_base_addr, + sos_size_t init_thread_stack_size); + + +/** + * Create a new kernel thread + */ +struct sos_thread * +sos_create_kernel_thread(const char *name, + sos_kernel_thread_start_routine_t start_func, + void *start_arg); + + +/** + * Terminate the execution of the current thread. For kernel threads, + * it is called by default when the start routine returns. + */ +void sos_thread_exit() __attribute__((noreturn)); + + +/** + * Get the identifier of the thread currently running on CPU. Trivial + * function. + */ +struct sos_thread *sos_thread_get_current(); + + +/** + * If thr == NULL, get the state of the current thread. Trivial + * function. + * + * @note NOT protected against interrupts + */ +sos_thread_state_t sos_thread_get_state(struct sos_thread *thr); + + +/** + * Yield CPU to another ready thread. + * + * @note This is a BLOCKING FUNCTION + */ +sos_ret_t sos_thread_yield(); + + +/** + * Release the CPU for (at least) the given delay. + * + * @param delay The delay to wait for. If delay == NULL then wait + * forever that any event occurs. + * + * @return SOS_OK when delay expired (and delay is reset to zero), + * -SOS_EINTR otherwise (and delay contains the amount of time + * remaining). + * + * @note This is a BLOCKING FUNCTION + */ +sos_ret_t sos_thread_sleep(/* in/out */struct sos_time *delay); + + +/** + * Mark the given thread as READY (if not already ready) even if it is + * blocked in a kwaitq or in a sleep ! As a result, the interrupted + * kwaitq/sleep function call of the thread will return with + * -SOS_EINTR. + * + * @return -SOS_EINVAL if thread does not exist, or -SOS_EFATAL if + * marked ZOMBIE. + * + * @note As a result, the semaphore/mutex/conditions/... functions + * return values SHOULD ALWAYS be checked ! If they are != SOS_OK, + * then the caller should consider that the resource is not aquired + * because somebody woke the thread by some way. + */ +sos_ret_t sos_thread_force_unblock(struct sos_thread *thread); + + +#endif /* _SOS_THREAD_H_ */ diff --git a/sos-code-article6.5/sos/time.c b/sos-code-article6.5/sos/time.c new file mode 100644 index 0000000..5181959 --- /dev/null +++ b/sos-code-article6.5/sos/time.c @@ -0,0 +1,355 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +#include <sos/assert.h> +#include <sos/klibc.h> +#include <hwcore/irq.h> +#include <sos/list.h> + +#include "time.h" + + +/** + * Number of nanoseconds in 1 second + */ +#define NS_IN_SEC 1000000000UL + + +/** + * The list of timeout actions waiting for a timeout. The timeout + * actions are stored in the list in increasing initial timeout + * order. Actually, the "timeout" field won't reflect this initial + * timeout: for each element in the list, it stores the timeout + * _difference_ between the timeout action and the previous in the + * list. + */ +static struct sos_timeout_action *timeout_action_list; + + +/** + * Current resolution of a time tick + */ +static struct sos_time tick_resolution; + + +/** + * Time elapsed between boot and last timer tick + * + * @note No 'volatile' here because the tick value is NEVER modified + * while in any of the functions below: it is modified only out of + * these functions by the IRQ timer handler because these functions + * are protected against timer IRQ and are "one shot" (no busy waiting + * for a change in the tick's value). + */ +static struct sos_time last_tick_time; + + +sos_ret_t sos_time_inc(struct sos_time *dest, + const struct sos_time *to_add) +{ + /* nanosec is always < 1e9 so that their sum is always < 2e9, which + is smaller than 2^32-1 */ + sos_ui32_t sigma_ns = dest->nanosec + to_add->nanosec; + + dest->sec += to_add->sec; + dest->sec += sigma_ns / NS_IN_SEC; + dest->nanosec = sigma_ns % NS_IN_SEC; + return SOS_OK; +} + + +sos_ret_t sos_time_dec(struct sos_time *dest, + const struct sos_time *to_dec) +{ + /* nanosec is always < 1e9 so that their difference is always in + (-1e9, 1e9), which is compatible with the (-2^31, 2^31 - 1) + cpacity of a signed dword */ + sos_si32_t diff_ns = ((sos_si32_t)dest->nanosec) + - ((sos_si32_t)to_dec->nanosec); + + /* Make sure substraction is possible */ + SOS_ASSERT_FATAL(dest->sec >= to_dec->sec); + if (dest->sec == to_dec->sec) + SOS_ASSERT_FATAL(dest->nanosec >= to_dec->nanosec); + + dest->sec -= to_dec->sec; + if (diff_ns > 0) + dest->sec += diff_ns / NS_IN_SEC; + else + dest->sec -= ((-diff_ns) / NS_IN_SEC); + dest->nanosec = (NS_IN_SEC + diff_ns) % NS_IN_SEC; + if (diff_ns < 0) + dest->sec --; + return SOS_OK; +} + + +int sos_time_cmp(const struct sos_time *t1, + const struct sos_time *t2) +{ + /* Compare seconds */ + if (t1->sec < t2->sec) + return -1; + else if (t1->sec > t2->sec) + return 1; + + /* seconds are equal => compare nanoseconds */ + else if (t1->nanosec < t2->nanosec) + return -1; + else if (t1->nanosec > t2->nanosec) + return 1; + + /* else: sec and nanosecs are equal */ + return 0; +} + + +sos_bool_t sos_time_is_zero(const struct sos_time *tm) +{ + return ( (0 == tm->sec) && (0 == tm->nanosec) ); +} + + +sos_ret_t sos_time_subsysem_setup(const struct sos_time *initial_resolution) +{ + timeout_action_list = NULL; + last_tick_time = (struct sos_time) { .sec = 0, .nanosec = 0 }; + memcpy(& tick_resolution, initial_resolution, sizeof(struct sos_time)); + + return SOS_OK; +} + + +sos_ret_t sos_time_get_tick_resolution(struct sos_time *resolution) +{ + sos_ui32_t flags; + sos_disable_IRQs(flags); + + memcpy(resolution, & tick_resolution, sizeof(struct sos_time)); + + sos_restore_IRQs(flags); + return SOS_OK; +} + + +sos_ret_t sos_time_set_tick_resolution(const struct sos_time *resolution) +{ + sos_ui32_t flags; + + sos_disable_IRQs(flags); + memcpy(& tick_resolution, resolution, sizeof(struct sos_time)); + sos_restore_IRQs(flags); + + return SOS_OK; +} + + +sos_ret_t sos_time_get_now(struct sos_time *now) +{ + sos_ui32_t flags; + sos_disable_IRQs(flags); + + memcpy(now, & last_tick_time, sizeof(struct sos_time)); + + sos_restore_IRQs(flags); + return SOS_OK; +} + + +/** + * Helper routine to add the action in the list. MUST be called with + * interrupts disabled ! + */ +static sos_ret_t _add_action(struct sos_timeout_action *act, + const struct sos_time *due_date, + sos_bool_t is_relative_due_date, + sos_timeout_routine_t *routine, + void *routine_data) +{ + struct sos_timeout_action *other, *insert_before; + int nb_act; + + /* Delay must be specified */ + if (due_date == NULL) + return -SOS_EINVAL; + + /* Action container MUST be specified */ + if (act == NULL) + return -SOS_EINVAL; + + /* Refuse to add an empty action */ + if (NULL == routine) + return -SOS_EINVAL; + + /* Refuse to add the action if it is already added */ + if (NULL != act->tmo_next) + return -SOS_EBUSY; + + /* Compute action absolute due date */ + if (is_relative_due_date) + { + /* The provided due_date is relative to the current time */ + memcpy(& act->timeout, & last_tick_time, sizeof(struct sos_time)); + sos_time_inc(& act->timeout, due_date); + } + else + { + /* The provided due_date is absolute (ie relative to the system + boot instant) */ + + if (sos_time_cmp(due_date, & last_tick_time) < 0) + /* Refuse to add a past action ! */ + return -SOS_EINVAL; + + memcpy(& act->timeout, due_date, sizeof(struct sos_time)); + } + + /* Prepare the action data structure */ + act->routine = routine; + act->routine_data = routine_data; + + /* Find the right place in the list of the timeout action. */ + insert_before = NULL; + list_foreach_forward_named(timeout_action_list, + other, nb_act, + tmo_prev, tmo_next) + { + if (sos_time_cmp(& act->timeout, & other->timeout) < 0) + { + insert_before = other; + break; + } + + /* Loop over to next timeout */ + } + + /* Now insert the action in the list */ + if (insert_before != NULL) + list_insert_before_named(timeout_action_list, insert_before, act, + tmo_prev, tmo_next); + else + list_add_tail_named(timeout_action_list, act, + tmo_prev, tmo_next); + + return SOS_OK; +} + + +sos_ret_t +sos_time_register_action_relative(struct sos_timeout_action *act, + const struct sos_time *delay, + sos_timeout_routine_t *routine, + void *routine_data) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + retval = _add_action(act, delay, TRUE, routine, routine_data); + sos_restore_IRQs(flags); + + return retval; +} + + +sos_ret_t +sos_time_register_action_absolute(struct sos_timeout_action *act, + const struct sos_time *date, + sos_timeout_routine_t *routine, + void *routine_data) +{ + sos_ui32_t flags; + sos_ret_t retval; + + sos_disable_IRQs(flags); + retval = _add_action(act, date, FALSE, routine, routine_data); + sos_restore_IRQs(flags); + + return retval; +} + + +/** + * Helper routine to remove the action from the list. MUST be called + * with interrupts disabled ! + */ +static sos_ret_t _remove_action(struct sos_timeout_action *act) +{ + /* Don't do anything if action is not in timeout list */ + if (NULL == act->tmo_next) + return -SOS_EINVAL; + + /* Update the action's remaining timeout */ + if (sos_time_cmp(& act->timeout, & last_tick_time) <= 0) + act->timeout = (struct sos_time){ .sec=0, .nanosec=0 }; + else + sos_time_dec(& act->timeout, & last_tick_time); + + /* Actually remove the action from the list */ + list_delete_named(timeout_action_list, act, + tmo_prev, tmo_next); + act->tmo_prev = act->tmo_next = NULL; + + return SOS_OK; +} + + +sos_ret_t sos_time_unregister_action(struct sos_timeout_action *act) +{ + sos_ret_t retval; + sos_ui32_t flags; + + sos_disable_IRQs(flags); + retval = _remove_action(act); + sos_restore_IRQs(flags); + + return SOS_OK; +} + + +sos_ret_t sos_time_do_tick() +{ + sos_ui32_t flags; + + sos_disable_IRQs(flags); + + /* Update kernel time */ + sos_time_inc(& last_tick_time, & tick_resolution); + + while (! list_is_empty_named(timeout_action_list, tmo_prev, tmo_next)) + { + struct sos_timeout_action *act; + act = list_get_head_named(timeout_action_list, tmo_prev, tmo_next); + + /* Did we go too far in the actions' list ? */ + if (sos_time_cmp(& last_tick_time, & act->timeout) < 0) + { + /* Yes: No need to look further. */ + break; + } + + /* Remove the action from the list */ + _remove_action(act); + + /* Call the action's routine */ + act->routine(act); + } + + sos_restore_IRQs(flags); + return SOS_OK; +} diff --git a/sos-code-article6.5/sos/time.h b/sos-code-article6.5/sos/time.h new file mode 100644 index 0000000..7d3a90b --- /dev/null +++ b/sos-code-article6.5/sos/time.h @@ -0,0 +1,222 @@ +/* Copyright (C) 2004 David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_TIME_H_ +#define _SOS_TIME_H_ + +/** + * @file time.h + * + * Primitives and callbacks related to kernel time management (timer + * IRQ) + */ + +#include <sos/types.h> +#include <sos/errno.h> +#include <sos/klibc.h> + + +/* ======================================================================= + * Library of time manipulation functions + */ +struct sos_time +{ + sos_ui32_t sec; + sos_ui32_t nanosec; +}; + +sos_ret_t sos_time_inc(struct sos_time *dest, + const struct sos_time *to_add); + +sos_ret_t sos_time_dec(struct sos_time *dest, + const struct sos_time *to_dec); + +int sos_time_cmp(const struct sos_time *t1, + const struct sos_time *t2); + +sos_bool_t sos_time_is_zero(const struct sos_time *tm); + + + +/* ======================================================================= + * Kernel time management. This is not the same as the "system-time", + * ie it does not not take into account the system-time adjustments + * (NTP, daylight saving times, etc...): this is the job of a + * system-time subsystem. + */ + + +/** + * Initialize kernel time subsystem. + * + * @param initial_resolution The initial time resolution. MUST be + * consistent with that of the hardware timer + */ +sos_ret_t sos_time_subsysem_setup(const struct sos_time *initial_resolution); + + +/** + * Value of the interval between 2 time ticks. Should be consistent + * with the configuration of the hardware timer. + */ +sos_ret_t sos_time_get_tick_resolution(struct sos_time *resolution); + + +/** + * Change the value of the interval between 2 time ticks. Must be + * called each time the hardware timer is reconfigured. + * + * @note MUST be consistent with that of the hardware timer + */ +sos_ret_t sos_time_set_tick_resolution(const struct sos_time *resolution); + + +/** + * Get the time elapsed since system boot. Does not take into account + * the system-time adjustment (NTP, daylight saving times, etc...): + * this is the job of a system-time subsystem. + */ +sos_ret_t sos_time_get_now(struct sos_time *now); + + + +/* ======================================================================= + * Routines to schedule future execution of routines: "timeout" actions + */ + +/* Forward declaration */ +struct sos_timeout_action; + +/** + * Prototype of a timeout routine. Called with IRQ disabled ! + */ +typedef void (sos_timeout_routine_t)(struct sos_timeout_action *); + + +/** + * The structure of a timeout action. This structure should have been + * opaque to the other parts of the kernel. We keep it public here so + * that struct sos_timeout_action can be allocated on the stack from + * other source files in the kernel. However, all the fields should be + * considered read-only for modules others than time.{ch}. + * + * @note After an action has been allocated (on the stack or kmalloc), + * it MUST be initialized with sos_time_init_action below ! + */ +struct sos_timeout_action +{ + /** PUBLIC: Address of the timeout routine */ + sos_timeout_routine_t *routine; + + /** PUBLIC: (Custom) data available for this routine */ + void *routine_data; + + /** PUBLIC: 2 meanings: + * - before and while in the timeout list: absolute due date of the + * timeout action + * - once removed from timeout list: the time remaining in the + * initial timeout (might be 0 if timeout expired) at removal + * time + */ + struct sos_time timeout; + + /** PRIVATE: To chain the timeout actions */ + struct sos_timeout_action *tmo_prev, *tmo_next; +}; + + +/** + * Initialize a timeout action. MUST be called immediately after + * (stack or kmalloc) allocation of the action. + * + * @param ptr_act Pointer to the action to initialize. + */ +#define sos_time_init_action(ptr_act) \ + ({ (ptr_act)->tmo_prev = (ptr_act)->tmo_next = NULL; /* return */ SOS_OK; }) + + +/** + * Add the given action in the timeout list, so that it will be + * triggered after the specified delay RELATIVE to the time when the + * function gets called. The action is always inserted in the list. + * + * @param act The action to be initialized by the function upon + * insertion in the timeout list. + * + * @param delay Delay until the action is fired. If 0, then it is + * fired at next timer IRQ. The action will be fired in X ticks, with + * X integer and >= delay. + * + * @param routine The timeout routine to call when the timeout will be + * triggered. + * + * @param routine_data The data available to the routine when it will + * be called. + * + * @note 'act' MUST remain valid until it is either fired or removed + * (with sos_time_remove_action) + */ +sos_ret_t +sos_time_register_action_relative(struct sos_timeout_action *act, + const struct sos_time *delay, + sos_timeout_routine_t *routine, + void *routine_data); + + +/** + * Add the given action in the timeout list, so that it will be + * triggered after the specified ABSOLUTE date (relative to system + * boot time). The action is always inserted in the list. + * + * @param act The action to be initialized by the function upon + * insertion in the timeout list. + * + * @param date Absolute date (relative to system boot time) when the + * action will be triggered. + * + * @param routine The timeout routine to call when the timeout will be + * triggered. + * + * @param routine_data The data available to the routine when it will + * be called. + * + * @note 'act' MUST remain valid until it is either fired or removed + * (with sos_time_remove_action) + */ +sos_ret_t +sos_time_register_action_absolute(struct sos_timeout_action *act, + const struct sos_time *date, + sos_timeout_routine_t *routine, + void *routine_data); + + +/** + * The action is removed and its timeout is updated to reflect the + * time remaining. + */ +sos_ret_t sos_time_unregister_action(struct sos_timeout_action *act); + + +/** + * Timer IRQ callback. Call and remove expired actions from the list. + * + * @note The use of this function is RESERVED (to timer IRQ) + */ +sos_ret_t sos_time_do_tick(); + + +#endif /* _SOS_TIME_H_ */ diff --git a/sos-code-article6.5/sos/types.h b/sos-code-article6.5/sos/types.h new file mode 100644 index 0000000..bf04314 --- /dev/null +++ b/sos-code-article6.5/sos/types.h @@ -0,0 +1,52 @@ +/* Copyright (C) 2004 The SOS Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ +#ifndef _SOS_TYPES_H_ +#define _SOS_TYPES_H_ + +/** + * @file types.h + * + * SOS basic types definition + */ + +/** Physical address */ +typedef unsigned int sos_paddr_t; + +/** Kernel virtual address */ +typedef unsigned int sos_vaddr_t; + +/** Memory size of an object (positive) */ +typedef unsigned int sos_size_t; +/** Generic count of objects */ +typedef unsigned int sos_count_t; + +/** Low-level sizes */ +typedef unsigned long int sos_ui32_t; /* 32b unsigned */ +typedef unsigned short int sos_ui16_t; /* 16b unsigned */ +typedef unsigned char sos_ui8_t; /* 8b unsigned */ +typedef signed long int sos_si32_t; /* 32b signed */ +typedef signed short int sos_si16_t; /* 16b signed */ +typedef signed char sos_si8_t; /* 8b signed */ + +typedef enum { FALSE=0, TRUE } sos_bool_t; + +/** Not a proper type, but highly useful with basic type + manipulations */ +#define NULL ((void*)0) + +#endif /* _SOS_TYPES_H_ */ diff --git a/sos-code-article6.5/support/build_image.sh b/sos-code-article6.5/support/build_image.sh new file mode 100755 index 0000000..43929cd --- /dev/null +++ b/sos-code-article6.5/support/build_image.sh @@ -0,0 +1,215 @@ +#!/bin/sh +# Copyright (C) 2003, David Decotigny + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, +# USA. + +# 1) What does it do ? +# +# 1) Check where Grub is installed (lookup_grub) +# 2) Assign some local variables using the shell script arguments. +# a) Argument 1 : the destination (either a file or a drive, like a:) +# b) Argument 2 : the loader (i.e kernel) +# c) Argument 3 : options passed to the loader +# d) Argument 4 : the modules (that can be loaded optionally by Grub) +# 3) Test whether destination is a drive or a file +# 4) Create the directory structure inside the drive +# 5) Copy the loader in the drive +# 6) Generate the 'menu.txt' file used by Grub to generate the boot menu +# 7) Copy all modules +# 8) Copy the menu.txt file +# +# 2) Why is it so complex ? +# Because it must support various Grub/mtools installations and versions +# +# In fact, this shell script is used in the KOS (kos.enix.org) +# project. This operating system consists in a loader and many many +# modules that are linked together at boot time. It is much more +# complex that a simple monolithic kernel. +# +# For your simple monolithic kernel, you only need to give argument 1 +# and 2. + +print_usage () { + echo "Usage: $0 [X:|image] path/to/loader option path/to/modules..." + echo " where X: is a valid floppy drive on your computer" + echo " where image is any file name" + exit 1 +} + +grub_dirs_common="/usr/local/share/grub/i386-freebsd /usr/local/share/grub/i386-pc /usr/share/grub/i386-pc /usr/lib/grub/i386-pc /usr/local/grub /usr/share/grub/i386-redhat /usr/local/src/grub-0.5.94 $HOME/share/grub/i386-pc/" +sbin_grub_path="/usr/local/sbin /usr/sbin /sbin $HOME/sbin" + +PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin +export PATH + +MTOOLSRC=mtoolsrc +export MTOOLSRC + +# Redefined variables +FLOPPY_DRIVE=A: +IMG_FNAME=fd.img + +## +## Format disk image +## +init_image () { + echo "Initialize disk image $IMG_FILE..." + if [ ! -f $IMG_FNAME ] ; then + dd if=/dev/zero of=$IMG_FNAME bs=18k count=80 1>/dev/null 2>&1 + fi + + rm -f $MTOOLSRC + echo "drive u: file=\"$IMG_FNAME\" 1.44M filter" > $MTOOLSRC + + if mformat U: ; then : ; else + rm -f $MTOOLSRC + echo "drive u: file=\"$IMG_FNAME\" 1.44M" > $MTOOLSRC + if mformat U: ; then : ; else + rm -f $MTOOLSRC + echo "drive u: file=\"$IMG_FNAME\"" > $MTOOLSRC + mformat U: + fi + fi +} + + +## +## Format (real) floppy disk +## +init_floppy () { + echo "Formatting floppy..." + mformat $FLOPPY_DRIVE || exit 1 +} + + +lookup_grub () { + # Look for a correct GRUBDIR + for d in $grub_dirs_common ; do + if [ -d $d ] ; then + GRUBDIR=$d + break + fi + done + + # Try to guess with locate + if [ ! -d "$GRUBDIR" ] ; then + GRUBDIR=`locate stage2 | head -1 | xargs dirname 2>/dev/null` + fi + + # Look for a correct sbin/grub + for d in $sbin_grub_path ; do + if [ -x $d/grub ] ; then + SBIN_GRUB=$d/grub + break + fi + done + + if [ -d "$GRUBDIR" -a -x "$SBIN_GRUB" ] ; then + echo "Found correct grub installation in $GRUBDIR" + echo "Found correct /sbin/grub at $SBIN_GRUB" + else + echo "Couldn't find a correct grub installation." + exit 1 + fi +} + +## +## setup_disk [drive] +## => setup disk directory structure / copy files +## +setup_disk () { + echo "Setup destination disk..." + + mmd $1/boot + mmd $1/boot/grub + + if [ -d $GRUBDIR/stage1 ] ; then + mcopy $GRUBDIR/stage1/stage1 $1/boot/grub/ + mcopy $GRUBDIR/stage2/stage2 $1/boot/grub/ + else + mcopy $GRUBDIR/stage1 $1/boot/grub/ + mcopy $GRUBDIR/stage2 $1/boot/grub/ + fi + mmd $1/system + mmd $1/modules + + $SBIN_GRUB --batch <<EOT 1>/dev/null 2>/dev/null || exit 1 +device (fd0) $IMG_FNAME +install (fd0)/boot/grub/stage1 (fd0) (fd0)/boot/grub/stage2 p (fd0)/boot/grub/menu.txt +quit +EOT +} + + + +################################################# +## Real start +## +#[ "$#" -lt 3 ] && print_usage + +lookup_grub + +dest="$1" ; shift +loader_fname="$1" ; shift +options="$1" ; shift +modules="$*" + +# Init destination disk +case x$dest in + x*:) + drive=$dest + IMG_FNAME=$dest + FLOPPY_DRIVE=$dest + init_floppy + ;; + x*) + drive=U: + IMG_FNAME=$dest + init_image + ;; +esac + +# Create directory structure +setup_disk $drive + +# Copy the loader +mcopy -bo $loader_fname $drive/system/`basename $loader_fname` + +# Generate the menu.txt file +rm -f menu.txt +cat <<EOF > menu.txt +timeout 0 +default 0 +title Simple OS +root (fd0) +kernel /system/`basename $loader_fname` $options +EOF + +# Copy the modules +for f in $modules ; do + if [ ! -f $f ] ; then + echo "ERROR: module $f not correctly compiled in." + exit 1 + fi + if ! mcopy -bo $f $drive/modules/`basename $f` ; then + echo "ERROR: module $f could not be transferred to floppy." + exit 1 + fi + echo module /modules/`basename $f` >> menu.txt +done + +# Transfers the menu.txt file to floppy +mcopy -bo menu.txt $drive/boot/grub/ diff --git a/sos-code-article6.5/support/sos.lds b/sos-code-article6.5/support/sos.lds new file mode 100644 index 0000000..497c9b5 --- /dev/null +++ b/sos-code-article6.5/support/sos.lds @@ -0,0 +1,115 @@ +/* Copyright (C) 2003, Thomas Petazzoni + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +/* We generate binary in the ELF format */ +OUTPUT_FORMAT("elf32-i386","elf32-i386","elf32-i386"); + +/* The entry point is _start (defined in boot.S) */ +ENTRY(_start) + +/* The architecture is i386 */ +OUTPUT_ARCH("i386") + +SECTIONS +{ + /* our kernel is loaded at 0x200000 */ + . = 0x200000; + __b_load = .; + + /* the multiboot header MUST come early enough in the output + object file */ + .multiboot : + { + /* The multiboot section (containing the multiboot header) + goes here */ + *(.multiboot); + + /* + * With the following line, we force this section to be + * allocated in the output file as soon as possible, no matter + * when the file containing the multiboot header (multiboot.S) + * is compiled. This is to conform to the multiboot spec, which + * says "The Multiboot header must be contained completely + * within the first 8192 bytes of the OS image, and must be + * longword (32-bit) aligned." + */ + LONG(0); + } + + /* Defines a symbol '__b_kernel to mark the start of the kernel + code/data */ + . = ALIGN(4096); + __b_kernel = .; + + /* Beginning of the text section */ + .text ALIGN(4096) : + { + /* This section includes the code */ + *(.text*) + /* Defines the 'etext' and '_etext' at the end */ + PROVIDE(etext = .); + PROVIDE(_etext = .); + } + + /* Beginning of the data section */ + .data . : + { *(.data*) + PROVIDE(edata = .); + PROVIDE(_edata = .); + } + + /* Beginning of the read-only data section */ + .rodata . : + { *(.rodata*) + PROVIDE(erodata = .); + PROVIDE(_erodata = .); + } + + /* We take note of the end of the data to load */ + __e_load = .; + + /* Beginning of the BSS section (global uninitialized data) */ + .bss SIZEOF(.rodata) + ADDR(.rodata) : + { *(.bss) + *(COMMON) + + /* We put the stack of the bootstrap thread on a page + boundary, because it can be un-allocated later */ + . = ALIGN(4096); + *(.init_stack) + + PROVIDE(ebss = .); + PROVIDE(_ebss = .); + } + + /* We take note of the end of the kernel: this is where the GPFM + will begin */ + __e_kernel = .; + + /* We don't care of the note, indent, comment, etc.. sections + generated by gcc */ + /DISCARD/ :{ + *(.note*) + *(.indent) + *(.comment) + *(.stab) + *(.stabstr) + } + +} + diff --git a/sos-code-article6.5/userland/Makefile b/sos-code-article6.5/userland/Makefile new file mode 100644 index 0000000..35fd0e3 --- /dev/null +++ b/sos-code-article6.5/userland/Makefile @@ -0,0 +1,82 @@ +## Copyright (C) 2005 David Decotigny +## +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 2 +## of the License, or (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, +## USA. + +CC=gcc +AR=ar +CFLAGS = -Wall -nostdinc -ffreestanding -I. -I.. +LDFLAGS = -Wl,--warn-common -nostdlib -Wl,-Tldscript.lds + +# Main target +all: userprogs.kimg + +-include .mkvars + + +PWD := $(shell pwd) + +# Programs generation +$(PROGS): + $(CC) -static $(LDFLAGS) -o $@ $^ + +# Generation of the libC +libc.a: libc.o + +# Create a program image to be integrated into the Kernel +userprogs.kimg: $(PROGS) + @echo "# Generating ELF images for inclusion into the kernel image: $@" + @echo "SECTIONS { .rodata . : { " > .userprogs.lds + @i=0 ; \ + for f in $^ ; do \ + i=`expr $$i + 1` ; \ + echo "extern char _begin_userprog$$i, _end_userprog$$i;" \ + > .userprog$$i.c ; \ + echo "char *_userprog"$$i"_entry[]" >> .userprog$$i.c ; \ + echo " __attribute__((section(\".userprogs_table\")))" \ + >> .userprog$$i.c ; \ + echo " = { \"$$f\", &_begin_userprog$$i, &_end_userprog$$i };" \ + >> .userprog$$i.c ; \ + $(CC) $(CFLAGS) -c .userprog$$i.c -o .userprog$$i.o ; \ + objcopy --add-section .userprog$$i=$$f .userprog$$i.o \ + .userprog$$i.kimg ; \ + echo " _begin_userprog$$i = .;" >> .userprogs.lds ; \ + echo " .userprog$$i.kimg(.userprog$$i);" >> .userprogs.lds ; \ + echo " _end_userprog$$i = .;" >> .userprogs.lds ; \ + echo " .userprog$$i.kimg(.rodata); .userprog$$i.kimg(.data);" \ + >> .userprogs.lds ; \ + done + @echo " _userprogs_table = .; *(.userprogs_table) ; LONG(0);" \ + >> .userprogs.lds + @echo "} /DISCARD/ : { *(.text) *(.data) *(.bss) } }" \ + >> .userprogs.lds + @ld -r -o $@ -T.userprogs.lds + +# Create libraries from object files +%.a: + $(AR) rcv $@ $^ + +# Create objects from C source code +%.o: %.c + $(CC) -I$(PWD) -c $< $(CFLAGS) -o $@ + +# Create objects from assembler (.S) source code +%.o: %.S + $(CC) -I$(PWD) -c $< $(CFLAGS) -DASM_SOURCE=1 -o $@ + +# Clean directory +clean: + $(RM) *.o *.a *~ $(PROGS) *.kimg + $(RM) .userprog* diff --git a/sos-code-article6.5/userland/ldscript.lds b/sos-code-article6.5/userland/ldscript.lds new file mode 100644 index 0000000..b0ee954 --- /dev/null +++ b/sos-code-article6.5/userland/ldscript.lds @@ -0,0 +1,74 @@ +/* Copyright (C) 2005, David Decotigny + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. +*/ + +/* We generate binary in the ELF format */ +OUTPUT_FORMAT("elf32-i386","elf32-i386","elf32-i386"); + +/* The entry point of the program is _start (defined in crt.c) */ +ENTRY(_start) + +/* The architecture is i386 */ +OUTPUT_ARCH("i386") + +SECTIONS +{ + /* our program is loaded at 2G */ + . = 0x80000000; + + /* Beginning of the text section */ + .text : + { + /* This section includes the code */ + *(.text*) + /* Defines the 'etext' and '_etext' at the end */ + PROVIDE(etext = .); + PROVIDE(_etext = .); + } + + /* Beginning of the data section */ + .data . : + { *(.data*) + PROVIDE(edata = .); + PROVIDE(_edata = .); + } + + /* Beginning of the read-only data section */ + .rodata . : + { *(.rodata*) + PROVIDE(erodata = .); + PROVIDE(_erodata = .); + } + + /* Beginning of the BSS section (global uninitialized data) */ + .bss SIZEOF(.rodata) + ADDR(.rodata) : + { *(.bss) + *(COMMON) + PROVIDE(ebss = .); + PROVIDE(_ebss = .); + } + + /* We don't care of the note, indent, comment, etc.. sections + generated by gcc */ + /DISCARD/ :{ + *(.note*) + *(.indent) + *(.comment) + *(.stab) + *(.stabstr) + } +} |