Browse Source

Initial import

master
Mathieu Maret 3 years ago
commit
7a37e94da3
  1. 156
      INSTALL
  2. 340
      LICENSE
  3. 100
      Makefile
  4. 89
      README
  5. 14
      VERSION
  6. 85
      bootstrap/multiboot.S
  7. 135
      bootstrap/multiboot.h
  8. 123
      drivers/bochs.c
  9. 55
      drivers/bochs.h
  10. 42
      drivers/console.c
  11. 37
      drivers/console.h
  12. 63
      drivers/devices.h
  13. 3212
      drivers/fs_fat.c
  14. 36
      drivers/fs_fat.h
  15. 1054
      drivers/fs_virtfs.c
  16. 40
      drivers/fs_virtfs.h
  17. 785
      drivers/ide.c
  18. 32
      drivers/ide.h
  19. 118
      drivers/kbd.c
  20. 37
      drivers/kbd.h
  21. 282
      drivers/kbdmapfr.c
  22. 489
      drivers/mem.c
  23. 36
      drivers/mem.h
  24. 216
      drivers/part.c
  25. 34
      drivers/part.h
  26. 292
      drivers/serial.c
  27. 40
      drivers/serial.h
  28. 318
      drivers/tty.c
  29. 39
      drivers/tty.h
  30. 270
      drivers/x86_videomem.c
  31. 108
      drivers/x86_videomem.h
  32. 452
      drivers/zero.c
  33. 46
      drivers/zero.h
  34. 46
      extra/Makefile
  35. 85
      extra/README
  36. 438
      extra/bootsect.S
  37. 31
      extra/dot.mkvars
  38. BIN
      extra/grub.img.gz
  39. BIN
      extra/hd20M.img.gz
  40. 2
      extra/mtoolsrc
  41. 212
      extra/patch-qemu-port-e9.diff
  42. 42
      extra/patch-qemu-pty.diff
  43. 64
      extra/sos_bsect.lds
  44. 130
      extra/termslave.c
  45. 177
      hwcore/bitmap.c
  46. 79
      hwcore/bitmap.h
  47. 1008
      hwcore/cpu_context.c
  48. 433
      hwcore/cpu_context.h
  49. 159
      hwcore/cpu_context_switch.S
  50. 183
      hwcore/exception.c
  51. 83
      hwcore/exception.h
  52. 271
      hwcore/exception_wrappers.S
  53. 203
      hwcore/gdt.c
  54. 52
      hwcore/gdt.h
  55. 79
      hwcore/i8254.c
  56. 35
      hwcore/i8254.h
  57. 79
      hwcore/i8259.c
  58. 40
      hwcore/i8259.h
  59. 159
      hwcore/idt.c
  60. 84
      hwcore/idt.h
  61. 84
      hwcore/ioports.h
  62. 100
      hwcore/irq.c
  63. 97
      hwcore/irq.h
  64. 305
      hwcore/irq_wrappers.S
  65. 321
      hwcore/mm_context.c
  66. 113
      hwcore/mm_context.h
  67. 1056
      hwcore/paging.c
  68. 252
      hwcore/paging.h
  69. 73
      hwcore/segment.h
  70. 50
      hwcore/swintr.c
  71. 43
      hwcore/swintr.h
  72. 116
      hwcore/swintr_wrappers.S
  73. 47
      sos/assert.c
  74. 45
      sos/assert.h
  75. 462
      sos/binfmt_elf32.c
  76. 42
      sos/binfmt_elf32.h
  77. 500
      sos/blkcache.c
  78. 97
      sos/blkcache.h
  79. 1197
      sos/blkdev.c
  80. 225
      sos/blkdev.h
  81. 396
      sos/calcload.c
  82. 117
      sos/calcload.h
  83. 463
      sos/chardev.c
  84. 155
      sos/chardev.h
  85. 58
      sos/errno.h
  86. 2263
      sos/fs.c
  87. 1140
      sos/fs.h
  88. 527
      sos/fs_nscache.c
  89. 284
      sos/fs_nscache.h
  90. 642
      sos/fs_pagecache.c
  91. 214
      sos/fs_pagecache.h
  92. 271
      sos/hash.c
  93. 153
      sos/hash.h
  94. 398
      sos/klibc.c
  95. 111
      sos/klibc.h
  96. 114
      sos/kmalloc.c
  97. 63
      sos/kmalloc.h
  98. 814
      sos/kmem_slab.c
  99. 206
      sos/kmem_slab.h
  100. 607
      sos/kmem_vmm.c

156
INSTALL

@ -0,0 +1,156 @@
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,
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)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: This technique assumes that INT 15H is supported by the
machine's BIOS. This should be OK for the vast majority of targets
(bochs, qemu, recent machines), but we do not guarantee it. In case
of doubt, please use Grub.
NOTE : recommended versions of the tools
----------------------------------------
Release development platform:
- OS : Linux 2.6.14.3 x86_64
- gcc : i586-pc-elf-gcc-4.0.2 (GCC) 4.0.2
- GNU binutils : GNU ld version 2.16
- GNU make : GNU Make 3.80
Usual development platforms known to work:
+ amd64 (x86_64)/debian sarge:
- OS : Linux 2.6.14.3 x86_64
- gcc : i586-pc-elf-gcc-4.0.2 (GCC) 4.0.2
- GNU binutils : GNU ld version 2.16
+ x86/debian sarge:
- OS : Linux 2.6.11.7-d2-1 i686
- gcc : gcc (GCC) 3.3.6 (Debian 1:3.3.6-5)
- GNU binutils : GNU ld version 2.15
+ ppc/debian sarge:
- OS : Linux 2.6.10-powerpc ppc
- gcc : gcc (GCC) 3.2.2
- GNU binutils : GNU ld version 2.13.2
+ x86/windows with cygwin (http://sos.enix.org/SOSFaq#TOC_0_2_1):
- OS : MS Windows XP Pro SP2 / Cygwin
- gcc : gcc 3.4.4
- GNU binutils : GNU ld version 2.16
- GNU make : GNU Make 3.80
--
David Decotigny

340
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.

100
Makefile

@ -0,0 +1,100 @@
## 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=gcc
LD=ld
CP=cp
STRIP=strip
CFLAGS = -Wall -nostdinc -ffreestanding -DKERNEL_SOS -g
LIBGCC = $(shell $(CC) -print-libgcc-file-name) # To benefit from FP/64bits artihm.
LDFLAGS = --warn-common -nostdlib
OBJECTS = bootstrap/multiboot.o \
hwcore/idt.o hwcore/gdt.o \
hwcore/swintr.o hwcore/swintr_wrappers.o \
hwcore/exception.o hwcore/exception_wrappers.o \
hwcore/irq.o hwcore/irq_wrappers.o hwcore/i8259.o \
hwcore/paging.o hwcore/i8254.o \
hwcore/cpu_context.o hwcore/cpu_context_switch.o \
hwcore/mm_context.o hwcore/bitmap.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/process.o sos/syscall.o \
sos/assert.o sos/main.o sos/mouse_sim.o \
sos/uaccess.o sos/calcload.o \
sos/umem_vmm.o sos/binfmt_elf32.o \
drivers/x86_videomem.o drivers/bochs.o \
drivers/zero.o drivers/mem.o \
drivers/ide.o drivers/kbd.o drivers/kbdmapfr.o \
drivers/serial.o drivers/tty.o drivers/part.o \
drivers/console.o \
sos/hash.o sos/fs.o sos/fs_nscache.o \
drivers/fs_virtfs.o drivers/fs_fat.o \
sos/chardev.o sos/blkdev.o \
sos/blkcache.o sos/fs_pagecache.o \
userland/userprogs.kimg
KERNEL_OBJ = sos.elf
KERNEL_LOAD = sos.gz
MULTIBOOT_IMAGE = fd.img
PWD := $(shell pwd | sed 's/"/\\\"/g;s/\$$/\\\$$/g')
# Main target
all: $(MULTIBOOT_IMAGE)
$(MULTIBOOT_IMAGE): $(KERNEL_LOAD)
./support/build_image.sh $@ $<
$(KERNEL_LOAD): $(KERNEL_OBJ)
$(CP) $< $<.strip && $(STRIP) -sx -R .comment $<.strip
gzip < $<.strip > $@
$(KERNEL_OBJ): $(OBJECTS) ./support/sos.lds
$(LD) $(LDFLAGS) -T ./support/sos.lds -o $@ $(OBJECTS) $(LIBGCC)
-nm -C $@ | cut -d ' ' -f 1,3 > sos.map
size $@
-include .mkvars
# Create the userland programs to include in the kernel image
userland/userprogs.kimg: FORCE
$(MAKE) -C userland
# 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) fd*.img *.o mtoolsrc *~ menu.txt *.elf *.bin *.strip *.map
$(RM) *.log *.out bochs* sos.gz
$(RM) bootstrap/*.o bootstrap/*~
$(RM) drivers/*.o drivers/*~
$(RM) hwcore/*.o hwcore/*~
$(RM) sos/*.o sos/*~
$(RM) support/*~
$(RM) extra/*~
$(MAKE) -C extra clean
$(MAKE) -C userland clean

89
README

@ -0,0 +1,89 @@
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
- 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/ext2 filesystem, Unix-style
mountpoints, hard/soft links
- basic network stack (ARP/IP/UDP)
- 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.
- 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.

14
VERSION

@ -0,0 +1,14 @@
SOS -- Simple OS
Copyright (C) 2003,2004,2005 The SOS Team (David Decotigny & Thomas Petazzoni)
Version "Article 9 (2nd part)" -- User interactions with the devices drivers
through the VFS: block devices, block cache,
page cache and IDE driver with PC-style
partitions support
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.

85
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

135
bootstrap/multiboot.h

@ -0,0 +1,135 @@
#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__ */

123
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_subsystem_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);
}

55
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_subsystem_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

42
drivers/console.c

@ -0,0 +1,42 @@
/* Copyright (C) 2005 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 <drivers/x86_videomem.h>
#include <drivers/kbd.h>
#include <drivers/tty.h>
#include <drivers/devices.h>
#include "console.h"
sos_ret_t
sos_console_subsystem_setup (void)
{
sos_ret_t ret;
struct tty_device *tty;
ret = sos_screen_init();
if (SOS_OK != ret)
return ret;
ret = tty_create (SOS_CHARDEV_CONSOLE_MINOR,
sos_screen_putchar, & tty);
if (SOS_OK != ret)
return ret;
return sos_kbd_subsystem_setup (tty);
}

37
drivers/console.h

@ -0,0 +1,37 @@
/* Copyright (C) 2005 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.
*/
#ifndef _SOS_DRV_CONSOLE_H_
#define _SOS_DRV_CONSOLE_H_
/**
* @file console.h
*
* Simple tty device binding together the physical keyboard (@see
* kbd.h) and the text-mode screen (@see x86_videomem.h)
*/
#include <sos/errno.h>
/**
* Create a new TTY device (minor CHARDEV_CONSOLE_MINOR) that controls
* the keyboard/screen
*/
sos_ret_t sos_console_subsystem_setup (void);
#endif /* _SOS_DRV_CONSOLE_H_ */

63
drivers/devices.h

@ -0,0 +1,63 @@
/* Copyright (C) 2005 David Decotigny, 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.
*/
#ifndef _SOS_DEVICES_H_
#define _SOS_DEVICES_H_
/**
*@file devices.h
*
* This file contains definitions concerning device drivers that must
* be shared between userspace applications and the kernel. They are
* mostly major numbers, IOCTL commands and parameters.
*
* This file must be built so that it can be safely included both from
* user applications and the kernel.
*/
/* For /dev/zero & /dev/null devices */
#define SOS_CHARDEV_ZERO_MAJOR 1
#define SOS_CHARDEV_ZERO_MINOR 0
#define SOS_CHARDEV_NULL_MINOR 1
/* For memory char devices (/dev/mem and /dev/kmem) */
#define SOS_CHARDEV_MEM_MAJOR 2
#define SOS_CHARDEV_KMEM_MINOR 0 /* /dev/kmem */
#define SOS_CHARDEV_PHYSMEM_MINOR 1 /* /dev/mem */
/* TTY major and minor*/
#define SOS_CHARDEV_TTY_MAJOR 3
#define SOS_CHARDEV_CONSOLE_MINOR 0
#define SOS_CHARDEV_SERIAL_MINOR 1
/* TTY IOCTL commands */
#define SOS_IOCTL_TTY_SETPARAM 1
#define SOS_IOCTL_TTY_RESETPARAM 2
/* TTY IOCTL command parameters for SOS_IOCTL_TTY_SETPARAM and
SOS_IOCTL_TTY_RESETPARAM */
#define SOS_IOCTLPARAM_TTY_CANON 1
#define SOS_IOCTLPARAM_TTY_ECHO 2
/** IDE major */
#define SOS_BLOCKDEV_IDE_MAJOR 1
/** Generic hardrive IOCTL: flush caches to disk */
#define SOS_IOCTL_BLOCKDEV_SYNC 1
#endif /* _SOS_DEVICE_H_ */

3212
drivers/fs_fat.c

File diff suppressed because it is too large

36
drivers/fs_fat.h

@ -0,0 +1,36 @@
/* Copyright (C) 2007 Anthoine Bourgeois
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_FS_FAT_H_
#define _SOS_FS_FAT_H_
#include <sos/errno.h>
/**
* @file fs_fat.h
*
* Basic implementation of the FAT16/32 file system for SOS.
*/
/**
* Solely register the "FAT" FS type into the list of supported FS types
*/
sos_ret_t sos_fs_fat_subsystem_setup(void);
#endif

1054
drivers/fs_virtfs.c

File diff suppressed because it is too large

40
drivers/fs_virtfs.h

@ -0,0 +1,40 @@
/* 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.
*/
#ifndef _SOS_FS_VIRTFS_H_
#define _SOS_FS_VIRTFS_H_
#include <sos/errno.h>
/**
* @file fs_virtfs.h
*
* Fake test file system. All the sos_fs_nodes and their contents are
* stored in kernel memory. Thus, with "virtfs", the "disk" is
* actually the kernel memory. In a sos_fs_node, the storage_location
* field corresponds to the kernel address of the corresponding struct
* sos_fs_node.
*/
/**
* Solely register the "virtfs" FS type into the list of supported FS types
*/
sos_ret_t sos_fs_virtfs_subsystem_setup(void);
#endif

785
drivers/ide.c

@ -0,0 +1,785 @@
/* Copyright (C) 2005 Thomas Petazzoni, 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/types.h>
#include <sos/errno.h>
#include <sos/assert.h>
#include <sos/ksynch.h>
#include <drivers/devices.h>
#include <drivers/bochs.h>
#include <drivers/part.h>
#include <hwcore/irq.h>
#include <hwcore/ioports.h>
/**
* @file ide.c
*
* Basic PIO IDE implementation based on the ATA standards
* http://www.t13.org/
*/
/**
* Busy-wait for a given time
*
* @param delay Delay to wait
*
* @todo Implement a calibration routine
*/
static void udelay(int delay)
{
int i;
for (i = 0; i < (delay * 1000) ; i++)
{
i++; i--;
}
}
/*
* Each IDE controller is controlled through a set of 9 I/O ports,
* starting from a base address, which is different for each IDE
* controller. On a standard PC with two onboard IDE controllers, the
* I/O registers of the first controller start at 0x1F0
* (IDE_CONTROLLER_0_BASE), the I/O registers of the second controller
* start at 0x170 (IDE_CONTROLLER_1_BASE). These I/O registers are
* 8-bits wide, and can be read using the inb() macro, and written
* using the outb() macro.
*
* The first controller is generally known as "primary" controller,
* and the second one is know as "secondary" controller. Each of them
* can handle at most two devices : the "master" device and the
* "slave" device.
*
* The registers can be used to issue commands and send data to the
* IDE controller, for example to identify a device, to read or write
* a device. Then are different ways of transmitting data to the IDE
* controller :
*
* - program the IDE controller, send the data, and wait for the
* completion of the request. This method is called "polling".
*
* - program the IDE controller, send the data, block the current
* process and do something else. The completion of the request will
* be signaled asynchronously by an interrupt (IRQ), which will
* allow to restart the sleeping process.
*
* - program the IDE controller and the DMA controller. There's no
* need to transfer the data to the IDE controller : the DMA
* controller will do it. This allows to use the CPU to do something
* useful during the transfer of data between the main memory and
* the IDE controller.
*
* In this driver, we use the two first methods. The polling method is
* used to fetch the identification of the devices, while the IRQ
* method is used to read and write to devices.
*/
#define IDE_CONTROLLER_0_BASE 0x1F0
#define IDE_CONTROLLER_1_BASE 0x170
#define IDE_CONTROLLER_0_IRQ 14
#define IDE_CONTROLLER_1_IRQ 15
/*
* All defines below are relative to the base address of the I/O
* registers (IDE_CONTROLLER_0_BASE and IDE_CONTROLLER_1_BASE)
*/
/**
* Read/write register that allows to transfer the data to be written
* or to fetch the read data from the IDE controller.
*/
#define ATA_DATA 0x00
/**
* Read only register that gives information about errors that occured
* during operation
*/
#define ATA_ERROR 0x01
/**
* Write only register to set precompensation. It is in fact the same
* register as the ATA_ERROR register. It simply has a different
* behaviour when reading and writing it.
*/
#define ATA_PRECOMP 0x01
/**
* Write only register used to set the count of sectors on which the
* request applies.
*/
#define ATA_SECTOR_COUNT 0x02
/**
* Write only register used to set the number of the starting sector
* of the request.
*/
#define ATA_SECTOR_NUMBER 0x03
/**
* Write only register used to set the 8 lower bits of the starting
* cylinder number of the request
*/
#define ATA_CYL_LSB 0x04
/**
* Write only register used to set the 8 higher bits of the starting
* cylinder number of the request
*/
#define ATA_CYL_MSB 0x05
/**
* Write only register that allows to select whether the LBA mode
* should be used, and to select whether the request concerns the
* slave or master device.
*/
#define ATA_DRIVE 0x06
#define ATA_D_IBM 0xa0 /* bits that must be set */
#define ATA_D_LBA 0x40 /* use LBA ? */
#define ATA_D_MASTER 0x00 /* select master */
#define ATA_D_SLAVE 0x10 /* select slave */
/**
* Read only register that contains the status of the controller. Each
* bit of this register as a different signification.
*/
#define ATA_STATUS 0x07
#define ATA_S_ERROR 0x01 /* error */
#define ATA_S_INDEX 0x02 /* index */
#define ATA_S_CORR 0x04 /* data corrected */
#define ATA_S_DRQ 0x08 /* data request */
#define ATA_S_DSC 0x10 /* drive Seek Completed */
#define ATA_S_DWF 0x20 /* drive write fault */
#define ATA_S_DRDY 0x40 /* drive ready */
#define ATA_S_BSY 0x80 /* busy */
/**
* Write only register used to set the command that the IDE controller
* should process. In our driver, only ATA_C_ATA_IDENTIFY, ATA_C_READ
* and ATA_C_WRITE are used.
*/
#define ATA_CMD 0x07
#define ATA_C_ATA_IDENTIFY 0xec /* get ATA params */
#define ATA_C_ATAPI_IDENTIFY 0xa1 /* get ATAPI params*/
#define ATA_C_READ 0x20 /* read command */
#define ATA_C_WRITE 0x30 /* write command */
#define ATA_C_READ_MULTI 0xc4 /* read multi command */
#define ATA_C_WRITE_MULTI 0xc5 /* write multi command */
#define ATA_C_SET_MULTI 0xc6 /* set multi size command */
#define ATA_C_PACKET_CMD 0xa0 /* set multi size command */
/**
* Read register that contains more information about the status of
* the controller
*/
#define ATA_ALTPORT 0x206 /* (R) alternate
Status register */
/**
* Write only register that allows to control the controller
*/
#define ATA_DEVICE_CONTROL 0x206 /* (W) device control
register */
#define ATA_A_nIEN 0x02 /* disable interrupts */
#define ATA_A_RESET 0x04 /* RESET controller */
#define ATA_A_4BIT 0x08 /* 4 head bits */
/** Magic numbers used to detect ATAPI devices */
#define ATAPI_MAGIC_LSB 0x14
#define ATAPI_MAGIC_MSB 0xeb
/* This structure describe the informations returned by the Identify
Device command (imposed by the ATA standard) */
struct ide_device_info
{
sos_ui16_t general_config_info; /* 0 */
sos_ui16_t nb_logical_cylinders; /* 1 */
sos_ui16_t reserved1; /* 2 */
sos_ui16_t nb_logical_heads; /* 3 */
sos_ui16_t unformatted_bytes_track; /* 4 */
sos_ui16_t unformatted_bytes_sector; /* 5 */
sos_ui16_t nb_logical_sectors; /* 6 */
sos_ui16_t vendor1[3]; /* 7-9 */
sos_ui8_t serial_number[20]; /* 10-19 */
sos_ui16_t buffer_type; /* 20 */
sos_ui16_t buffer_size; /* 21 */
sos_ui16_t ecc_bytes; /* 22 */
sos_ui8_t firmware_revision[8]; /* 23-26 */
sos_ui8_t model_number[40]; /* 27-46 */
sos_ui8_t max_multisect; /* 47 */
sos_ui8_t vendor2;
sos_ui16_t dword_io; /* 48 */
sos_ui8_t vendor3; /* 49 */
sos_ui8_t capabilities;
sos_ui16_t reserved2; /* 50 */
sos_ui8_t vendor4; /* 51 */
sos_ui8_t pio_trans_mode;
sos_ui8_t vendor5; /* 52 */
sos_ui8_t dma_trans_mode;
sos_ui16_t fields_valid; /* 53 */
sos_ui16_t cur_logical_cylinders; /* 54 */
sos_ui16_t cur_logical_heads; /* 55 */
sos_ui16_t cur_logical_sectors; /* 56 */
sos_ui16_t capacity1; /* 57 */
sos_ui16_t capacity0; /* 58 */
sos_ui8_t multsect; /* 59 */
sos_ui8_t multsect_valid;
sos_ui32_t lba_capacity; /* 60-61 */
sos_ui16_t dma_1word; /* 62 */
sos_ui16_t dma_multiword; /* 63 */
sos_ui16_t pio_modes; /* 64 */
sos_ui16_t min_mword_dma; /* 65 */
sos_ui16_t recommended_mword_dma; /* 66 */
sos_ui16_t min_pio_cycle_time; /* 67 */
sos_ui16_t min_pio_cycle_time_iordy; /* 68 */
sos_ui16_t reserved3[11]; /* 69-79 */
sos_ui16_t major_version; /* 80 */
sos_ui16_t minor_version; /* 81 */
sos_ui16_t command_sets1; /* 82 */
sos_ui16_t command_sets2; /* 83 dixit lk : b14 (smart enabled) */
sos_ui16_t reserved4[4]; /* 84-87 */
sos_ui16_t dma_ultra; /* 88 dixit lk */
sos_ui16_t reserved5[37]; /* 89-125 */
sos_ui16_t last_lun; /* 126 */
sos_ui16_t reserved6; /* 127 */
sos_ui16_t security; /* 128 */
sos_ui16_t reserved7[127];
} __attribute__((packed));
#define MAX_IDE_CONTROLLERS 2
#define MAX_IDE_DEVICES 2
#define IDE_BLK_SIZE 512
#define IDE_DEVICE(ctrl,device) (((ctrl) * 2) + (device))
#define IDE_MINOR(ctrl,device) (IDE_DEVICE(ctrl,device)*16)
typedef enum { IDE_DEVICE_NONE,
IDE_DEVICE_HARDDISK,
IDE_DEVICE_CDROM } ide_device_type_t;
struct ide_controller;
struct ide_device {
int id;
ide_device_type_t type;
enum { IDE_DEVICE_MASTER, IDE_DEVICE_SLAVE } position;
int cyls;
int heads;
int sectors;
int blocks;
sos_bool_t support_lba;
struct ide_controller *ctrl;
};
struct ide_controller {
int id;
int ioaddr;
int irq;
enum { IDE_CTRL_NOT_PRESENT, IDE_CTRL_PRESENT } state;
struct ide_device devices[MAX_IDE_DEVICES];
struct sos_kmutex mutex;
struct sos_ksema ack_io_sem;
};
struct ide_controller ide_controllers[MAX_IDE_CONTROLLERS] = {
{
.id = 0,
.ioaddr = IDE_CONTROLLER_0_BASE,
.irq = IDE_CONTROLLER_0_IRQ,
.state = IDE_CTRL_NOT_PRESENT,
},
{
.id = 1,