init
This commit is contained in:
commit
2c423554f6
28 changed files with 2317 additions and 0 deletions
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[build]
|
||||
target = "x86_64-unknown-none"
|
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
**/debug
|
||||
**/target
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
# Generated by cargo mutants
|
||||
# Contains mutation testing data
|
||||
**/mutants.out*/
|
190
Cargo.lock
generated
Normal file
190
Cargo.lock
generated
Normal file
|
@ -0,0 +1,190 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "allocator-api2"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||
|
||||
[[package]]
|
||||
name = "atomalloc"
|
||||
version = "0.1.0"
|
||||
source = "git+https://forge.gaycatgirl.sex/hypericum/atomalloc.git?rev=6f6687429e5e89c16e813dbeab4af30e05e2e8a2#6f6687429e5e89c16e813dbeab4af30e05e2e8a2"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "bit_field"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
||||
|
||||
[[package]]
|
||||
name = "elf"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
|
||||
dependencies = [
|
||||
"allocator-api2",
|
||||
"equivalent",
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kernel"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"atomalloc",
|
||||
"elf",
|
||||
"hashbrown",
|
||||
"libgoatweed",
|
||||
"limine",
|
||||
"linked_list_allocator",
|
||||
"spin",
|
||||
"tar-no-std",
|
||||
"x86_64",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libgoatweed"
|
||||
version = "0.1.0"
|
||||
source = "git+https://forge.gaycatgirl.sex/hypericum/libgoatweed.git?rev=31ac8671a3cfb338ae66957c963a9ced72fa2251#31ac8671a3cfb338ae66957c963a9ced72fa2251"
|
||||
|
||||
[[package]]
|
||||
name = "limine"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af6d2ee42712e7bd2c787365cd1dab06ef59a61becbf87bec7b32b970bd2594b"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linked_list_allocator"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
|
||||
dependencies = [
|
||||
"spinning_top",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spinning_top"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tar-no-std"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15574aa79d3c04a12f3cb53ff976d5571e53b9d8e0bdbe4021df0a06473dd1c9"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"log",
|
||||
"memchr",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "volatile"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
|
||||
|
||||
[[package]]
|
||||
name = "x86_64"
|
||||
version = "0.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f042214de98141e9c8706e8192b73f56494087cc55ebec28ce10f26c5c364ae"
|
||||
dependencies = [
|
||||
"bit_field",
|
||||
"bitflags",
|
||||
"rustversion",
|
||||
"volatile",
|
||||
]
|
34
Cargo.toml
Normal file
34
Cargo.toml
Normal file
|
@ -0,0 +1,34 @@
|
|||
[package]
|
||||
name = "kernel"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "GPL-2.0-only"
|
||||
|
||||
[dependencies]
|
||||
libgoatweed = { git = "https://forge.gaycatgirl.sex/hypericum/libgoatweed.git", rev = "31ac8671a3cfb338ae66957c963a9ced72fa2251" , default-features = false }
|
||||
atomalloc = { git = "https://forge.gaycatgirl.sex/hypericum/atomalloc.git", rev = "6f6687429e5e89c16e813dbeab4af30e05e2e8a2", default-features = false, features = ["no_alloc"] }
|
||||
|
||||
# bootloader info structs
|
||||
limine = "0.5.0"
|
||||
|
||||
# sync prims
|
||||
spin = "0.10.0"
|
||||
|
||||
# some niceities
|
||||
x86_64 = "0.15.2"
|
||||
|
||||
# alloc
|
||||
linked_list_allocator = "0.10.5"
|
||||
|
||||
# stuff for init
|
||||
tar-no-std = { version = "0.3.3", features = ["alloc", "unstable"] }
|
||||
elf = { version = "0.7.4", default-features = false, features = [] }
|
||||
|
||||
# hashmaps and hashsets
|
||||
hashbrown = "0.15.2"
|
||||
|
||||
[[bin]]
|
||||
name = "kernel"
|
||||
test = false
|
||||
doctest = false
|
||||
bench = false
|
339
LICENSE
Normal file
339
LICENSE
Normal file
|
@ -0,0 +1,339 @@
|
|||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 Lesser 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) <year> <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.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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) year 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 Lesser General
|
||||
Public License instead of this License.
|
0
README.md
Normal file
0
README.md
Normal file
4
build.rs
Normal file
4
build.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
fn main() {
|
||||
println!("cargo:rustc-link-arg=-Tlinkers/x86_64.ld");
|
||||
println!("cargo:rerun-if-changed=linkers/x86_64.ld");
|
||||
}
|
72
linkers/x86_64.ld
Normal file
72
linkers/x86_64.ld
Normal file
|
@ -0,0 +1,72 @@
|
|||
/* Tell the linker that we want an x86_64 ELF64 output file */
|
||||
OUTPUT_FORMAT(elf64-x86-64)
|
||||
OUTPUT_ARCH(i386:x86-64)
|
||||
|
||||
/* We want the symbol _kmain to be our entry point */
|
||||
ENTRY(_kmain)
|
||||
|
||||
/* Define the program headers we want so the bootloader gives us the right */
|
||||
/* MMU permissions */
|
||||
PHDRS
|
||||
{
|
||||
text PT_LOAD FLAGS(0x05); /* Execute + Read */
|
||||
rodata PT_LOAD FLAGS(0x04); /* Read only */
|
||||
data PT_LOAD FLAGS(0x06); /* Write + Read */
|
||||
dynamic PT_DYNAMIC FLAGS(0x06); /* Dynamic PHDR for relocations */
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
/* We wanna be placed in the topmost 2GiB of the address space, for optimisations */
|
||||
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
|
||||
/* that is the beginning of the region. */
|
||||
. = 0xffffffff80000000;
|
||||
|
||||
.text : {
|
||||
*(.text .text.*)
|
||||
} :text
|
||||
|
||||
/* Move to the next memory page for .rodata */
|
||||
. = ALIGN(CONSTANT(MAXPAGESIZE));
|
||||
|
||||
.rodata : {
|
||||
*(.rodata .rodata.*)
|
||||
} :rodata
|
||||
|
||||
/* Move to the next memory page for .data */
|
||||
. = ALIGN(CONSTANT(MAXPAGESIZE));
|
||||
|
||||
.data : {
|
||||
*(.data .data.*)
|
||||
|
||||
/* Place the sections that contain the requests as part of the .data */
|
||||
/* output section. */
|
||||
KEEP(*(.requests_start_marker))
|
||||
KEEP(*(.requests))
|
||||
KEEP(*(.requests_end_marker))
|
||||
} :data
|
||||
|
||||
/* Dynamic section for relocations, both in its own PHDR and inside data PHDR */
|
||||
.dynamic : {
|
||||
*(.dynamic)
|
||||
} :data :dynamic
|
||||
|
||||
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
|
||||
/* unnecessary zeros will be written to the binary. */
|
||||
/* If you need, for example, .init_array and .fini_array, those should be placed */
|
||||
/* above this. */
|
||||
.bss : {
|
||||
*(.bss .bss.*)
|
||||
*(COMMON)
|
||||
} :data
|
||||
|
||||
/* Discard .note.* and .eh_frame* since they may cause issues on some hosts. */
|
||||
/* Also discard the program interpreter section since we do not need one. This is */
|
||||
/* more or less equivalent to the --no-dynamic-linker linker flag, except that it */
|
||||
/* works with ld.gold. */
|
||||
/DISCARD/ : {
|
||||
*(.eh_frame*)
|
||||
*(.note .note.*)
|
||||
*(.interp)
|
||||
}
|
||||
}
|
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[toolchain]
|
||||
channel = "nightly"
|
147
src/arch/amd64/devices/i8259.rs
Normal file
147
src/arch/amd64/devices/i8259.rs
Normal file
|
@ -0,0 +1,147 @@
|
|||
use spin::Mutex;
|
||||
use crate::arch::amd64::port::Port;
|
||||
use crate::println;
|
||||
use crate::print;
|
||||
|
||||
pub const PIC_1_OFFSET: u8 = 32;
|
||||
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
||||
|
||||
pub static PICS: Mutex<PicPair> = Mutex::new(unsafe {
|
||||
PicPair::new(PIC_1_OFFSET, PIC_2_OFFSET)
|
||||
});
|
||||
|
||||
pub struct Pic {
|
||||
offset: u8,
|
||||
pub(crate) command: Port,
|
||||
pub(crate) data: Port,
|
||||
}
|
||||
|
||||
impl Pic {
|
||||
fn handles_interrupt(&self, interrupt: u8) -> bool {
|
||||
self.offset <= interrupt && interrupt <= self.offset + 8
|
||||
}
|
||||
|
||||
unsafe fn end_interrupt(&mut self) {
|
||||
unsafe {
|
||||
self.command.write_u8(0x20u8);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn read_interrupt_mask(&mut self) -> u8 {
|
||||
unsafe {
|
||||
self.data.read_u8()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn write_interrupt_mask(&mut self, mask: u8) {
|
||||
unsafe {
|
||||
self.data.write_u8(mask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PicPair {
|
||||
pub(crate) pics: [Pic; 2],
|
||||
}
|
||||
|
||||
impl PicPair {
|
||||
pub const unsafe fn new(offset_one: u8, offset_two: u8) -> Self {
|
||||
PicPair {
|
||||
pics: [
|
||||
Pic {
|
||||
offset: offset_one,
|
||||
command: Port::new(0x20),
|
||||
data: Port::new(0x21),
|
||||
},
|
||||
Pic {
|
||||
offset: offset_two,
|
||||
command: Port::new(0xA0),
|
||||
data: Port::new(0xA1),
|
||||
}
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn init(&mut self) {
|
||||
let mut wait_port = Port::new(0x80);
|
||||
|
||||
unsafe {
|
||||
let mut wait = || wait_port.write_u8(0);
|
||||
|
||||
let mut saved_masks = self.read_masks();
|
||||
|
||||
// init command
|
||||
self.pics[0].command.write_u8(0x11u8);
|
||||
wait();
|
||||
self.pics[1].command.write_u8(0x11u8);
|
||||
wait();
|
||||
|
||||
// set offsets
|
||||
self.pics[0].data.write_u8(self.pics[0].offset);
|
||||
wait();
|
||||
self.pics[1].data.write_u8(self.pics[1].offset);
|
||||
wait();
|
||||
|
||||
// pair the pics together
|
||||
self.pics[0].data.write_u8(4);
|
||||
wait();
|
||||
self.pics[1].data.write_u8(2);
|
||||
wait();
|
||||
|
||||
// set the response mode
|
||||
self.pics[0].data.write_u8(0x01u8);
|
||||
wait();
|
||||
self.pics[1].data.write_u8(0x01u8);
|
||||
wait();
|
||||
|
||||
// Mask all interrupts initially
|
||||
saved_masks[0] = 255;
|
||||
saved_masks[1] = 255;
|
||||
|
||||
// Enable timer
|
||||
saved_masks[0] &= !1;
|
||||
|
||||
// Enable keyboard
|
||||
// saved_masks[0] &= !2;
|
||||
|
||||
self.write_masks(saved_masks[0], saved_masks[1]);
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn read_masks(&mut self) -> [u8; 2] {
|
||||
unsafe {
|
||||
[self.pics[0].read_interrupt_mask(), self.pics[1].read_interrupt_mask()]
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn write_masks(&mut self, mask_one: u8, mask_two: u8) {
|
||||
unsafe {
|
||||
self.pics[0].write_interrupt_mask(mask_one);
|
||||
self.pics[1].write_interrupt_mask(mask_two);
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn disable(&mut self) {
|
||||
unsafe {
|
||||
self.write_masks(u8::MAX, u8::MAX);
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn handles_interrupt(&mut self, interrupt: u8) -> bool {
|
||||
self.pics.iter().any(|p| p.handles_interrupt(interrupt))
|
||||
}
|
||||
|
||||
pub unsafe fn notify_end_interrupt(&mut self, interrupt: u8) {
|
||||
unsafe {
|
||||
if self.handles_interrupt(interrupt) {
|
||||
if self.pics[1].handles_interrupt(interrupt) {
|
||||
self.pics[1].end_interrupt();
|
||||
}
|
||||
|
||||
self.pics[0].end_interrupt();
|
||||
} else {
|
||||
println!("? eoi called for a non-pic interrupt")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
src/arch/amd64/devices/mod.rs
Normal file
3
src/arch/amd64/devices/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod i8259;
|
||||
#[macro_use]
|
||||
pub mod serial;
|
92
src/arch/amd64/devices/serial.rs
Normal file
92
src/arch/amd64/devices/serial.rs
Normal file
|
@ -0,0 +1,92 @@
|
|||
use spin::lazy::Lazy;
|
||||
use spin::mutex::Mutex;
|
||||
use crate::arch::amd64::instructions::{inb, outb};
|
||||
|
||||
// pub static SERIAL_WRITER: Lazy<Mutex<SerialWriter>> = Lazy::new(|| {
|
||||
// Mutex::new(SerialWriter::new(SerialPort::COM1))
|
||||
// });
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(u16)]
|
||||
pub enum SerialPort {
|
||||
COM1 = 0x3F8
|
||||
}
|
||||
|
||||
pub struct SerialWriter {
|
||||
serial_port: SerialPort,
|
||||
}
|
||||
|
||||
impl SerialWriter {
|
||||
pub fn new(serial_port: SerialPort) -> SerialWriter {
|
||||
let port = serial_port as u16;
|
||||
|
||||
unsafe {
|
||||
outb(port + 1, 0x00);
|
||||
outb(port + 3, 0x80);
|
||||
outb(port, 0x03);
|
||||
outb(port + 1, 0x00);
|
||||
outb(port + 3, 0x03);
|
||||
outb(port + 2, 0xC7);
|
||||
outb(port + 4, 0x0B);
|
||||
outb(port + 4, 0x1E);
|
||||
outb(port, 0xAE);
|
||||
|
||||
if inb(port) != 0xAE {
|
||||
panic!();
|
||||
}
|
||||
|
||||
outb(port + 4, 0x0F);
|
||||
}
|
||||
|
||||
SerialWriter {
|
||||
serial_port,
|
||||
}
|
||||
}
|
||||
|
||||
fn write_byte(&mut self, byte: u8) {
|
||||
unsafe {
|
||||
outb(self.serial_port as u16, byte);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_newline(&mut self) {
|
||||
self.write_byte(b'\r');
|
||||
self.write_byte(b'\n');
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Write for SerialWriter {
|
||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||
for i in s.chars() {
|
||||
match i {
|
||||
'\n' => self.write_newline(),
|
||||
_ => self.write_byte(i as u8),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($args:tt)+) => ({
|
||||
use core::fmt::Write;
|
||||
use crate::arch::amd64::devices::serial::{SerialWriter, SerialPort};
|
||||
|
||||
let _ = write!(SerialWriter::new(SerialPort::COM1), $($args)+);
|
||||
});
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! println {
|
||||
() => ({
|
||||
print!("\n")
|
||||
});
|
||||
($fmt:expr) => ({
|
||||
print!(concat!($fmt, "\n"))
|
||||
});
|
||||
($fmt:expr, $($args:tt)+) => ({
|
||||
print!(concat!($fmt, "\n"), $($args)+)
|
||||
});
|
||||
}
|
46
src/arch/amd64/instructions.rs
Normal file
46
src/arch/amd64/instructions.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use core::arch::asm;
|
||||
|
||||
/// Halt
|
||||
///
|
||||
/// Halts the CPU until an interrupt occurs.
|
||||
pub fn hlt() {
|
||||
unsafe {
|
||||
asm!("hlt");
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear Interrupt Flag
|
||||
///
|
||||
/// Clears the IF flag, causing the processor to ignore maskable interrupts.
|
||||
pub fn cli() {
|
||||
unsafe {
|
||||
asm!("cli", options(preserves_flags, nostack));
|
||||
}
|
||||
}
|
||||
|
||||
/// Set Interrupt Flag
|
||||
///
|
||||
/// Sets the IF flag, causing the processor to start accepting maskable interrupts.
|
||||
pub fn sti() {
|
||||
unsafe {
|
||||
asm!("sti", options(preserves_flags, nostack));
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a byte to a port.
|
||||
pub unsafe fn outb(port: u16, data: u8) {
|
||||
unsafe {
|
||||
asm!("outb %al, %dx", in("al") data, in("dx") port, options(att_syntax));
|
||||
}
|
||||
}
|
||||
|
||||
/// Read a byte from a port.
|
||||
pub unsafe fn inb(port: u16) -> u8 {
|
||||
let value: u8;
|
||||
|
||||
unsafe {
|
||||
asm!("inb %dx, %al", in("dx") port, out("al") value, options(att_syntax));
|
||||
}
|
||||
|
||||
value
|
||||
}
|
96
src/arch/amd64/interrupts/gdt.rs
Normal file
96
src/arch/amd64/interrupts/gdt.rs
Normal file
|
@ -0,0 +1,96 @@
|
|||
use core::ptr::addr_of;
|
||||
use spin::lazy::Lazy;
|
||||
use x86_64::PrivilegeLevel::Ring3;
|
||||
use x86_64::registers::segmentation::{CS, DS, ES, SS};
|
||||
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
|
||||
use x86_64::structures::tss::TaskStateSegment;
|
||||
use x86_64::VirtAddr;
|
||||
|
||||
const STACK_SIZE: usize = 1024 * 8 * 16;
|
||||
|
||||
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
||||
pub const PAGE_FAULT_IST_INDEX: u16 = 1;
|
||||
pub const GENERAL_PROTECTION_FAULT_IST_INDEX: u16 = 2;
|
||||
|
||||
|
||||
static TSS: Lazy<TaskStateSegment> = Lazy::new(|| {
|
||||
let mut tss = TaskStateSegment::new();
|
||||
|
||||
tss.privilege_stack_table[0] = {
|
||||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
||||
|
||||
let stack_start = unsafe { addr_of!(STACK) } as u64;
|
||||
let stack_end = stack_start + STACK_SIZE as u64;
|
||||
|
||||
VirtAddr::new(stack_end)
|
||||
};
|
||||
|
||||
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
|
||||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
||||
|
||||
let stack_start = unsafe { addr_of!(STACK) } as u64;
|
||||
let stack_end = stack_start + STACK_SIZE as u64;
|
||||
|
||||
VirtAddr::new(stack_end)
|
||||
};
|
||||
|
||||
tss.interrupt_stack_table[PAGE_FAULT_IST_INDEX as usize] = {
|
||||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
||||
|
||||
let stack_start = unsafe { addr_of!(STACK) } as u64;
|
||||
let stack_end = stack_start + STACK_SIZE as u64;
|
||||
|
||||
VirtAddr::new(stack_end)
|
||||
};
|
||||
|
||||
tss.interrupt_stack_table[GENERAL_PROTECTION_FAULT_IST_INDEX as usize] = {
|
||||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
||||
|
||||
let stack_start = unsafe { addr_of!(STACK) } as u64;
|
||||
let stack_end = stack_start + STACK_SIZE as u64;
|
||||
|
||||
VirtAddr::new(stack_end)
|
||||
};
|
||||
|
||||
tss
|
||||
});
|
||||
|
||||
pub struct GDTSelectors {
|
||||
pub code_selector: SegmentSelector,
|
||||
pub data_selector: SegmentSelector,
|
||||
pub tss_selector: SegmentSelector,
|
||||
pub user_code_selector: SegmentSelector,
|
||||
pub user_data_selector: SegmentSelector,
|
||||
}
|
||||
|
||||
pub static GDT: Lazy<(GlobalDescriptorTable, GDTSelectors)> = Lazy::new(|| {
|
||||
let mut gdt = GlobalDescriptorTable::new();
|
||||
|
||||
let code_selector = gdt.append(Descriptor::kernel_code_segment());
|
||||
let data_selector = gdt.append(Descriptor::kernel_data_segment());
|
||||
let tss_selector = gdt.append(Descriptor::tss_segment(&TSS));
|
||||
|
||||
let mut user_code_selector = gdt.append(Descriptor::user_code_segment());
|
||||
user_code_selector.set_rpl(Ring3);
|
||||
|
||||
let mut user_data_selector = gdt.append(Descriptor::user_data_segment());
|
||||
user_data_selector.set_rpl(Ring3);
|
||||
|
||||
|
||||
(gdt, GDTSelectors { code_selector, data_selector, tss_selector, user_code_selector, user_data_selector })
|
||||
});
|
||||
|
||||
pub fn init() {
|
||||
use x86_64::instructions::tables::load_tss;
|
||||
use x86_64::instructions::segmentation::{CS, Segment};
|
||||
|
||||
GDT.0.load();
|
||||
unsafe {
|
||||
CS::set_reg(GDT.1.code_selector);
|
||||
ES::set_reg(GDT.1.data_selector);
|
||||
DS::set_reg(GDT.1.data_selector);
|
||||
SS::set_reg(GDT.1.data_selector);
|
||||
|
||||
load_tss(GDT.1.tss_selector)
|
||||
}
|
||||
}
|
230
src/arch/amd64/interrupts/idt.rs
Normal file
230
src/arch/amd64/interrupts/idt.rs
Normal file
|
@ -0,0 +1,230 @@
|
|||
use core::arch::asm;
|
||||
use spin::lazy::Lazy;
|
||||
use spin::mutex::Mutex;
|
||||
use x86_64::registers::control::Cr2;
|
||||
use x86_64::{set_general_handler, PrivilegeLevel};
|
||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
|
||||
use crate::arch::amd64::devices::i8259::PicPair;
|
||||
use crate::arch::amd64::interrupts::gdt;
|
||||
use crate::trafficcontrol::Process;
|
||||
|
||||
pub const PIC_1_OFFSET: u8 = 32;
|
||||
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
||||
|
||||
pub static PICS: Mutex<PicPair> = Mutex::new(unsafe {
|
||||
PicPair::new(PIC_1_OFFSET, PIC_2_OFFSET)
|
||||
});
|
||||
|
||||
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
|
||||
let mut idt = InterruptDescriptorTable::new();
|
||||
|
||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||
idt.debug.set_handler_fn(debug_handler);
|
||||
|
||||
unsafe {
|
||||
idt.double_fault.set_handler_fn(double_fault_handler)
|
||||
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
||||
};
|
||||
|
||||
unsafe {
|
||||
idt.general_protection_fault.set_handler_fn(general_protection_fault_handler)
|
||||
.set_stack_index(gdt::GENERAL_PROTECTION_FAULT_IST_INDEX);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
idt.page_fault.set_handler_fn(page_fault_handler)
|
||||
.set_stack_index(gdt::PAGE_FAULT_IST_INDEX);
|
||||
}
|
||||
|
||||
idt[InterruptIndex::Timer as u8].set_handler_fn(timer_interrupt_handler);
|
||||
|
||||
// syscall capture
|
||||
idt[0x80].set_handler_fn(syscall_handler).set_privilege_level(PrivilegeLevel::Ring3);
|
||||
|
||||
set_general_handler!(&mut idt, catch_all_handler, 0);
|
||||
set_general_handler!(&mut idt, catch_all_handler, 2);
|
||||
set_general_handler!(&mut idt, catch_all_handler, 4..=7);
|
||||
set_general_handler!(&mut idt, catch_all_handler, 9..=12);
|
||||
set_general_handler!(&mut idt, catch_all_handler, 15..=31);
|
||||
|
||||
idt
|
||||
});
|
||||
|
||||
#[repr(u8)]
|
||||
pub enum InterruptIndex {
|
||||
Timer = PIC_1_OFFSET,
|
||||
Keyboard,
|
||||
Secondary,
|
||||
SerialTwo,
|
||||
SerialOne,
|
||||
ParallelTwo,
|
||||
Floppy,
|
||||
ParallelOne,
|
||||
RealTimeClock = PIC_2_OFFSET,
|
||||
ACPI,
|
||||
CustomOne,
|
||||
CustomTwo,
|
||||
Mouse,
|
||||
CoProcessor,
|
||||
PrimaryATA,
|
||||
SecondaryATA
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
IDT.load();
|
||||
}
|
||||
|
||||
// CPU interrupts
|
||||
extern "x86-interrupt" fn breakpoint_handler(
|
||||
interrupt_stack_frame: InterruptStackFrame
|
||||
) {
|
||||
println!("breakpoint hit!");
|
||||
println!("{:#?}", interrupt_stack_frame);
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn debug_handler(
|
||||
interrupt_stack_frame: InterruptStackFrame
|
||||
) {
|
||||
println!("debug hit!");
|
||||
println!("{:#?}", interrupt_stack_frame);
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn double_fault_handler(
|
||||
interrupt_stack_frame: InterruptStackFrame,
|
||||
error_code: u64
|
||||
) -> ! {
|
||||
println!("=== DOUBLE FAULT ===");
|
||||
println!("Error code: {:#x}", error_code);
|
||||
println!("Stack frame: {:#?}", interrupt_stack_frame);
|
||||
|
||||
panic!("Double fault occurred!");
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn general_protection_fault_handler(
|
||||
interrupt_stack_frame: InterruptStackFrame,
|
||||
error_code: u64
|
||||
) {
|
||||
println!("=== GENERAL PROTECTION FAULT ===");
|
||||
println!("Error code: {:#x} ({:016b})", error_code, error_code);
|
||||
|
||||
if error_code != 0 {
|
||||
let external = (error_code & 1) != 0;
|
||||
let table = (error_code >> 1) & 0b11;
|
||||
let index = (error_code >> 3) & 0x1FFF;
|
||||
|
||||
println!("\tExternal: {}", external);
|
||||
println!("\tTable: {} ({})", table, match table {
|
||||
0 => "GDT",
|
||||
1 => "IDT",
|
||||
2 => "LDT",
|
||||
3 => "IDT",
|
||||
_ => "Unknown"
|
||||
});
|
||||
println!("\tSelector index: {}", index);
|
||||
}
|
||||
|
||||
println!("Stack frame: {:#?}", interrupt_stack_frame);
|
||||
|
||||
panic!("General protection fault!");
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn page_fault_handler(
|
||||
interrupt_stack_frame: InterruptStackFrame,
|
||||
page_fault_error_code: PageFaultErrorCode
|
||||
) {
|
||||
println!("=== PAGE FAULT ===");
|
||||
println!("Accessed Address: {:?}", Cr2::read());
|
||||
println!("Error Code: {:?}", page_fault_error_code);
|
||||
println!("Stack frame: {:#?}", interrupt_stack_frame);
|
||||
|
||||
panic!("Page fault!");
|
||||
}
|
||||
|
||||
fn catch_all_handler(
|
||||
interrupt_stack_frame: InterruptStackFrame,
|
||||
code: u8,
|
||||
error_code: Option<u64>
|
||||
) {
|
||||
println!("=== UNHANDLED EXCEPTION {} ===", code);
|
||||
if let Some(error_code) = error_code {
|
||||
println!("Error code: {:#x}", error_code);
|
||||
}
|
||||
println!("Stack frame: {:#?}", interrupt_stack_frame);
|
||||
|
||||
panic!("Unhandled exception {} occurred!", code);
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "x86-interrupt" fn timer_interrupt_handler(
|
||||
interrupt_stack_frame: InterruptStackFrame,
|
||||
) {
|
||||
// first thing: save all the gp registers
|
||||
let mut saved_registers: [u64; 15] = [0; 15];
|
||||
unsafe {
|
||||
asm!(
|
||||
"mov [{regs} + 0], rax",
|
||||
"mov [{regs} + 8], rbx",
|
||||
"mov [{regs} + 16], rcx",
|
||||
"mov [{regs} + 24], rdx",
|
||||
"mov [{regs} + 32], rsi",
|
||||
"mov [{regs} + 40], rdi",
|
||||
"mov [{regs} + 48], rbp",
|
||||
"mov [{regs} + 56], r8",
|
||||
"mov [{regs} + 64], r9",
|
||||
"mov [{regs} + 72], r10",
|
||||
"mov [{regs} + 80], r11",
|
||||
"mov [{regs} + 88], r12",
|
||||
"mov [{regs} + 96], r13",
|
||||
"mov [{regs} + 104], r14",
|
||||
"mov [{regs} + 112], r15",
|
||||
regs = in(reg) saved_registers.as_mut_ptr(),
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: debug code
|
||||
println!(".");
|
||||
|
||||
// since we're gonna be jumping, we should notify early bc we wont have a chance to do this
|
||||
unsafe {
|
||||
PICS.lock().notify_end_interrupt(InterruptIndex::Timer as u8);
|
||||
}
|
||||
|
||||
Process::execute_next_round_robin(interrupt_stack_frame, saved_registers);
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn syscall_handler(
|
||||
_interrupt_stack_frame: InterruptStackFrame
|
||||
) {
|
||||
let rax: u64;
|
||||
let rbx: u64;
|
||||
let rcx: u64;
|
||||
let rdx: u64;
|
||||
let rsi: u64;
|
||||
let rdi: u64;
|
||||
|
||||
unsafe {
|
||||
asm!(
|
||||
// llvm will cry about it if we use rbx, move it somewhere else
|
||||
"mov {rbx_out}, rbx",
|
||||
out("rax") rax,
|
||||
rbx_out = out(reg) rbx,
|
||||
out("rcx") rcx,
|
||||
out("rdx") rdx,
|
||||
out("rsi") rsi,
|
||||
out("rdi") rdi,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
|
||||
let result = crate::syscalls::route(rax, rbx, rcx, rdx, rsi, rdi);
|
||||
|
||||
unsafe {
|
||||
// result goes into rax
|
||||
// TODO: figure this out better maybe
|
||||
asm!(
|
||||
"mov rax, {result_in}",
|
||||
result_in = in(reg) result,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
}
|
2
src/arch/amd64/interrupts/mod.rs
Normal file
2
src/arch/amd64/interrupts/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod gdt;
|
||||
pub mod idt;
|
41
src/arch/amd64/memory/kalloc.rs
Normal file
41
src/arch/amd64/memory/kalloc.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use core::ops::Deref;
|
||||
use linked_list_allocator::LockedHeap;
|
||||
use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB};
|
||||
use x86_64::VirtAddr;
|
||||
use crate::arch::amd64::memory::pmm::get_frame_allocator;
|
||||
use crate::arch::amd64::memory::vmm::get_mapper;
|
||||
|
||||
const HEAP_START: usize = 0x2000_0000_0000;
|
||||
const HEAP_SIZE: usize = 1024 * 1024 * 32;
|
||||
|
||||
#[global_allocator]
|
||||
static KALLOC: LockedHeap = LockedHeap::empty();
|
||||
|
||||
pub fn init() {
|
||||
let page_range = {
|
||||
let heap_start = VirtAddr::new(HEAP_START as u64);
|
||||
let heap_end = heap_start + (HEAP_SIZE) as u64;
|
||||
|
||||
let heap_start_page: Page<Size4KiB> = Page::containing_address(heap_start);
|
||||
let heap_end_page: Page<Size4KiB> = Page::containing_address(heap_end);
|
||||
|
||||
Page::range(heap_start_page, heap_end_page)
|
||||
};
|
||||
|
||||
let mut allocator = get_frame_allocator().lock();
|
||||
for page in page_range {
|
||||
if let Some(frame) = allocator.allocate_frame() {
|
||||
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
|
||||
|
||||
unsafe {
|
||||
get_mapper().map_to(page, frame, flags, &mut *allocator).expect("Failed to map").flush();
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
KALLOC.lock().init(HEAP_START as *mut u8, HEAP_SIZE);
|
||||
}
|
||||
}
|
15
src/arch/amd64/memory/mod.rs
Normal file
15
src/arch/amd64/memory/mod.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use core::sync::atomic::Ordering;
|
||||
use x86_64::{PhysAddr, VirtAddr};
|
||||
use x86_64::structures::paging::Translate;
|
||||
|
||||
pub mod kalloc;
|
||||
pub mod pmm;
|
||||
pub mod vmm;
|
||||
|
||||
pub fn phys_to_virt(addr: PhysAddr) -> VirtAddr {
|
||||
VirtAddr::new(addr.as_u64() + vmm::PHYS_OFFSET.load(Ordering::Relaxed) as u64)
|
||||
}
|
||||
|
||||
pub fn virt_to_phys(addr: VirtAddr) -> Option<PhysAddr> {
|
||||
vmm::get_mapper().translate_addr(addr)
|
||||
}
|
233
src/arch/amd64/memory/pmm.rs
Normal file
233
src/arch/amd64/memory/pmm.rs
Normal file
|
@ -0,0 +1,233 @@
|
|||
use crate::print;
|
||||
use crate::println;
|
||||
use core::ptr::slice_from_raw_parts_mut;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use limine::memory_map::{Entry, EntryType};
|
||||
use spin::{Mutex, Once};
|
||||
use x86_64::structures::paging::{FrameAllocator, FrameDeallocator, PhysFrame, Size4KiB};
|
||||
use x86_64::PhysAddr;
|
||||
|
||||
// TODO: remake this entire thing
|
||||
|
||||
// TODO: rethink this
|
||||
static FRAME_ALLOCATOR: Once<Mutex<PFrameAllocator>> = Once::new();
|
||||
|
||||
// TODO: rethink this
|
||||
static mut FRAME_BITMAP: [u8; BITMAP_SIZE] = [0; BITMAP_SIZE];
|
||||
|
||||
// TODO: rethink this
|
||||
const BITMAP_SIZE: usize = 16384 * 4096 * 4;
|
||||
|
||||
pub const MAX_REGIONS: usize = 64;
|
||||
|
||||
static TOTAL_MEMORY: AtomicUsize = AtomicUsize::new(0);
|
||||
static ALLOCATED_FRAMES: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||
pub struct Region {
|
||||
/// The base of the memory region, in *physical space*.
|
||||
pub base: u64,
|
||||
/// The end address of the memory region, in *physical space*
|
||||
pub end: u64,
|
||||
/// The length of the memory region, in bytes.
|
||||
pub length: u64,
|
||||
/// The type of the memory region. See [`EntryType`] for specific values.
|
||||
pub entry_type: EntryType,
|
||||
}
|
||||
|
||||
pub struct PFrameAllocator {
|
||||
usable_regions: [Option<Region>; MAX_REGIONS],
|
||||
bitmap_base: u64,
|
||||
last_allocated: usize,
|
||||
}
|
||||
|
||||
impl PFrameAllocator {
|
||||
pub unsafe fn init(memory_map: &[&Entry]) {
|
||||
let mut i = 0;
|
||||
let mut usable_regions = [None; MAX_REGIONS];
|
||||
let mut total_memory: usize = 0;
|
||||
let mut min_addr = u64::MAX;
|
||||
|
||||
for e in memory_map {
|
||||
let region = Region {
|
||||
base: e.base,
|
||||
end: e.base + e.length,
|
||||
length: e.length,
|
||||
entry_type: e.entry_type,
|
||||
};
|
||||
|
||||
if e.entry_type == EntryType::USABLE {
|
||||
usable_regions[i] = Some(region);
|
||||
total_memory += e.length as usize;
|
||||
min_addr = min_addr.min(e.base);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let bitmap_base = (min_addr / 4096) * 4096;
|
||||
|
||||
TOTAL_MEMORY.store(total_memory, Ordering::Relaxed);
|
||||
|
||||
#[allow(static_mut_refs)]
|
||||
FRAME_BITMAP.fill(0);
|
||||
|
||||
FRAME_ALLOCATOR.call_once(|| {
|
||||
Mutex::new(PFrameAllocator {
|
||||
usable_regions,
|
||||
bitmap_base,
|
||||
last_allocated: 0,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/// Convert physical address to bitmap index
|
||||
fn addr_to_bitmap_index(&self, addr: u64) -> Option<usize> {
|
||||
if addr < self.bitmap_base {
|
||||
return None;
|
||||
}
|
||||
|
||||
let frame_number = (addr - self.bitmap_base) / 4096;
|
||||
let bit_index = frame_number as usize;
|
||||
|
||||
if bit_index >= BITMAP_SIZE * 8 {
|
||||
None
|
||||
} else {
|
||||
Some(bit_index)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert bitmap index to physical address
|
||||
fn bitmap_index_to_addr(&self, index: usize) -> u64 {
|
||||
self.bitmap_base + (index as u64 * 4096)
|
||||
}
|
||||
|
||||
/// Check if a frame is allocated in the bitmap
|
||||
fn is_frame_allocated(&self, bit_index: usize) -> bool {
|
||||
let byte_index = bit_index / 8;
|
||||
let bit_offset = bit_index % 8;
|
||||
|
||||
if byte_index >= BITMAP_SIZE {
|
||||
return true;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
(FRAME_BITMAP[byte_index] & (1 << bit_offset)) != 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a frame as allocated in the bitmap
|
||||
fn set_frame_allocated(&mut self, bit_index: usize) {
|
||||
let byte_index = bit_index / 8;
|
||||
let bit_offset = bit_index % 8;
|
||||
|
||||
if byte_index < BITMAP_SIZE {
|
||||
unsafe {
|
||||
FRAME_BITMAP[byte_index] |= 1 << bit_offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a frame as free in the bitmap
|
||||
fn set_frame_free(&mut self, bit_index: usize) {
|
||||
let byte_index = bit_index / 8;
|
||||
let bit_offset = bit_index % 8;
|
||||
|
||||
if byte_index < BITMAP_SIZE {
|
||||
unsafe {
|
||||
FRAME_BITMAP[byte_index] &= !(1 << bit_offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if an address is in any usable region
|
||||
fn is_addr_usable(&self, addr: u64) -> bool {
|
||||
for region_opt in &self.usable_regions {
|
||||
if let Some(region) = region_opt {
|
||||
if addr >= region.base && addr < region.end {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn find_free_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
|
||||
let max_frames = BITMAP_SIZE * 8;
|
||||
|
||||
for offset in 0..max_frames {
|
||||
let index = (self.last_allocated + offset) % max_frames;
|
||||
|
||||
if !self.is_frame_allocated(index) {
|
||||
let addr = self.bitmap_index_to_addr(index);
|
||||
|
||||
if self.is_addr_usable(addr) {
|
||||
self.last_allocated = index;
|
||||
return Some(PhysFrame::containing_address(PhysAddr::new(addr)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FrameAllocator<Size4KiB> for PFrameAllocator {
|
||||
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
|
||||
if let Some(frame) = self.find_free_frame() {
|
||||
let addr = frame.start_address().as_u64();
|
||||
|
||||
if let Some(bit_index) = self.addr_to_bitmap_index(addr) {
|
||||
print!(
|
||||
"Found frame at {:#x} ({} bytes)",
|
||||
addr,
|
||||
frame.size()
|
||||
);
|
||||
|
||||
self.set_frame_allocated(bit_index);
|
||||
ALLOCATED_FRAMES.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
// ensure memory is cleared
|
||||
unsafe {
|
||||
print!("! Clearing memory...");
|
||||
slice_from_raw_parts_mut(addr as *mut u8, frame.size() as usize).as_mut()?.fill(0);
|
||||
}
|
||||
|
||||
print!(" Allocated!\n");
|
||||
|
||||
Some(frame)
|
||||
} else {
|
||||
println!("cant convert address to bitmap?");
|
||||
|
||||
None
|
||||
}
|
||||
} else {
|
||||
println!("no free frame :(");
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FrameDeallocator<Size4KiB> for PFrameAllocator {
|
||||
unsafe fn deallocate_frame(&mut self, frame: PhysFrame<Size4KiB>) {
|
||||
let addr = frame.start_address().as_u64();
|
||||
|
||||
if !self.is_addr_usable(addr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(bit_index) = self.addr_to_bitmap_index(addr) {
|
||||
if self.is_frame_allocated(bit_index) {
|
||||
self.set_frame_free(bit_index);
|
||||
ALLOCATED_FRAMES.fetch_sub(1, Ordering::Relaxed);
|
||||
|
||||
self.last_allocated = bit_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_frame_allocator() -> &'static Mutex<PFrameAllocator> {
|
||||
FRAME_ALLOCATOR.get().expect("Cannot get frame allocator?")
|
||||
}
|
133
src/arch/amd64/memory/vmm.rs
Normal file
133
src/arch/amd64/memory/vmm.rs
Normal file
|
@ -0,0 +1,133 @@
|
|||
use crate::print;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use spin::Once;
|
||||
use x86_64::registers::control::{Cr3};
|
||||
use x86_64::structures::paging::{FrameAllocator, FrameDeallocator, Mapper, OffsetPageTable, Page, PageTable, PageTableFlags, PhysFrame, Size4KiB};
|
||||
use x86_64::VirtAddr;
|
||||
use crate::arch::amd64::memory::{phys_to_virt, pmm};
|
||||
use crate::println;
|
||||
|
||||
#[allow(static_mut_refs)]
|
||||
pub static mut KERNEL_PAGE_TABLE: Once<OffsetPageTable> = Once::new();
|
||||
pub static PHYS_OFFSET: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
pub unsafe fn init(physical_memory_offset: u64) {
|
||||
PHYS_OFFSET.store(physical_memory_offset as usize, Ordering::Relaxed);
|
||||
|
||||
unsafe {
|
||||
#[allow(static_mut_refs)]
|
||||
KERNEL_PAGE_TABLE.call_once(|| { OffsetPageTable::new(&mut *active_l4t(), VirtAddr::new(physical_memory_offset)) });
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn active_l4t() -> &'static mut PageTable {
|
||||
let (l4tf, _) = Cr3::read();
|
||||
|
||||
let phys = l4tf.start_address();
|
||||
let virt = phys_to_virt(phys);
|
||||
let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
|
||||
|
||||
unsafe { &mut *page_table_ptr }
|
||||
}
|
||||
|
||||
pub fn get_mapper() -> &'static mut OffsetPageTable<'static> {
|
||||
unsafe {
|
||||
#[allow(static_mut_refs)]
|
||||
KERNEL_PAGE_TABLE.get_mut().expect("Cannot get page table?")
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn new_page_table(frame: PhysFrame) -> &'static mut PageTable {
|
||||
let phys_addr = frame.start_address();
|
||||
let virt_addr = phys_to_virt(phys_addr);
|
||||
|
||||
let page_table_ptr: *mut PageTable = virt_addr.as_mut_ptr();
|
||||
|
||||
unsafe {
|
||||
&mut *page_table_ptr
|
||||
}
|
||||
}
|
||||
|
||||
pub fn alloc_pages(page_table: &mut OffsetPageTable, addr: u64, count: usize, flags: PageTableFlags) -> Result<(), ()> {
|
||||
let mut frame_allocator = pmm::get_frame_allocator().lock();
|
||||
|
||||
let start_page: Page<Size4KiB> = Page::containing_address(VirtAddr::new(addr));
|
||||
let end_addr = addr + (count * 4096) as u64 - 1; // Last byte of the allocation
|
||||
let end_page = Page::containing_address(VirtAddr::new(end_addr));
|
||||
|
||||
let pages = Page::range_inclusive(start_page, end_page);
|
||||
|
||||
let parent_table_flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE;
|
||||
|
||||
for page in pages {
|
||||
if let Some(frame) = frame_allocator.allocate_frame() {
|
||||
let res = unsafe { page_table.map_to_with_table_flags(page, frame, flags, parent_table_flags, &mut *frame_allocator) };
|
||||
|
||||
if let Ok(res) = res {
|
||||
res.flush();
|
||||
} else {
|
||||
println!("beep");
|
||||
return Err(());
|
||||
}
|
||||
} else {
|
||||
println!("boop");
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn free_pages(page_table: &mut OffsetPageTable, addr: u64, count: usize) -> Result<(), ()> {
|
||||
let mut frame_allocator = pmm::get_frame_allocator().lock();
|
||||
|
||||
let start_page: Page<Size4KiB> = Page::containing_address(VirtAddr::new(addr));
|
||||
let end_addr = addr + (count * 4096) as u64 - 1;
|
||||
let end_page = Page::containing_address(VirtAddr::new(end_addr));
|
||||
|
||||
let pages = Page::range_inclusive(start_page, end_page);
|
||||
|
||||
for page in pages {
|
||||
if let Ok((frame, flush)) = page_table.unmap(page) {
|
||||
unsafe {
|
||||
frame_allocator.deallocate_frame(frame);
|
||||
}
|
||||
flush.flush();
|
||||
} else {
|
||||
return Err(())
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn find_free_virtual_address(size: usize) -> Option<u64> {
|
||||
let mapper = get_mapper();
|
||||
let page_size = 4096;
|
||||
let pages_needed = size.div_ceil(page_size);
|
||||
|
||||
let mut current_addr = 0x20000000u64;
|
||||
|
||||
while current_addr + (pages_needed as u64 * page_size as u64) < u64::MAX {
|
||||
let mut all_free = true;
|
||||
|
||||
// lets check if all required pages are free
|
||||
for i in 0..pages_needed {
|
||||
let check_addr = current_addr + (i as u64 * page_size as u64);
|
||||
let page: Page<Size4KiB> = Page::containing_address(VirtAddr::new(check_addr));
|
||||
|
||||
if mapper.translate_page(page).is_ok() {
|
||||
all_free = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if all_free {
|
||||
return Some(current_addr);
|
||||
}
|
||||
|
||||
current_addr += page_size as u64;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
26
src/arch/amd64/mod.rs
Normal file
26
src/arch/amd64/mod.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use core::panic;
|
||||
|
||||
pub mod memory;
|
||||
pub mod port;
|
||||
#[macro_use]
|
||||
pub mod devices;
|
||||
pub mod instructions;
|
||||
pub mod interrupts;
|
||||
|
||||
#[panic_handler]
|
||||
fn on_panic(info: &panic::PanicInfo) -> ! {
|
||||
println!();
|
||||
println!("################################### fucky wucky! ###################################");
|
||||
println!("{:#?}", info);
|
||||
println!("halting...");
|
||||
|
||||
catch_fire()
|
||||
}
|
||||
|
||||
pub fn catch_fire() -> ! {
|
||||
instructions::cli();
|
||||
|
||||
loop {
|
||||
instructions::hlt()
|
||||
}
|
||||
}
|
27
src/arch/amd64/port.rs
Normal file
27
src/arch/amd64/port.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use core::arch::asm;
|
||||
|
||||
pub struct Port {
|
||||
port: u16
|
||||
}
|
||||
|
||||
impl Port {
|
||||
pub const fn new(port: u16) -> Port {
|
||||
Port { port }
|
||||
}
|
||||
|
||||
pub unsafe fn write_u8(&mut self, value: u8) {
|
||||
unsafe {
|
||||
asm!("out dx, al", in("dx") self.port, in("al") value, options(nomem, nostack, preserves_flags));
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn read_u8(&mut self) -> u8 {
|
||||
let value: u8;
|
||||
|
||||
unsafe {
|
||||
asm!("in al, dx", in("dx") self.port, out("al") value, options(nomem, nostack, preserves_flags));
|
||||
}
|
||||
|
||||
value
|
||||
}
|
||||
}
|
1
src/arch/mod.rs
Normal file
1
src/arch/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod amd64;
|
173
src/main.rs
Normal file
173
src/main.rs
Normal file
|
@ -0,0 +1,173 @@
|
|||
#![feature(abi_x86_interrupt)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::vec::Vec;
|
||||
use core::arch::asm;
|
||||
use core::ptr;
|
||||
use core::ptr::{addr_of, addr_of_mut};
|
||||
use elf::ElfBytes;
|
||||
use elf::endian::AnyEndian;
|
||||
use limine::BaseRevision;
|
||||
use limine::request::{BootloaderInfoRequest, HhdmRequest, MemoryMapRequest, ModuleRequest, PagingModeRequest};
|
||||
use tar_no_std::TarArchiveRef;
|
||||
use crate::arch::amd64;
|
||||
use crate::arch::amd64::{catch_fire, instructions};
|
||||
use crate::arch::amd64::interrupts::{gdt, idt};
|
||||
use crate::arch::amd64::memory::{kalloc, pmm, vmm};
|
||||
|
||||
mod arch;
|
||||
mod syscalls;
|
||||
mod trafficcontrol;
|
||||
|
||||
pub const KERNEL_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
// Limine requests
|
||||
#[used]
|
||||
static BASE_REVISION: BaseRevision = BaseRevision::new();
|
||||
|
||||
#[used]
|
||||
static BOOTLOADER_INFO_REQUEST: BootloaderInfoRequest = BootloaderInfoRequest::new();
|
||||
|
||||
#[used]
|
||||
static PAGING_MODE_REQUEST: PagingModeRequest = PagingModeRequest::new().with_mode(limine::paging::Mode::FOUR_LEVEL);
|
||||
|
||||
#[used]
|
||||
static MEMORY_MAP_REQUEST: MemoryMapRequest = MemoryMapRequest::new();
|
||||
|
||||
#[used]
|
||||
static HHDM_REQUEST: HhdmRequest = HhdmRequest::new();
|
||||
|
||||
#[used]
|
||||
static MODULE_REQUEST: ModuleRequest = ModuleRequest::new();
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _kmain() -> ! {
|
||||
assert!(BASE_REVISION.is_supported());
|
||||
|
||||
println!("haii from hypericum v{}", KERNEL_VERSION);
|
||||
|
||||
if let Some(inf) = BOOTLOADER_INFO_REQUEST.get_response() {
|
||||
println!("booted using {} v{} r{}", inf.name(), inf.version(), inf.revision());
|
||||
} else {
|
||||
println!("weird... no bootloader info... is this not limine?");
|
||||
}
|
||||
|
||||
print!("init gdt");
|
||||
gdt::init();
|
||||
println!("\t\t\t\t\t[OK]");
|
||||
|
||||
print!("init interrupts");
|
||||
// disable interrupts while we do all this set-up
|
||||
amd64::instructions::cli();
|
||||
idt::init();
|
||||
println!("\t\t\t\t\t[OK]");
|
||||
|
||||
println!("init memory ...");
|
||||
|
||||
// lets see if our paging mode is here (and correct)
|
||||
print!("\tgetting paging mode");
|
||||
if let Some(pag) = PAGING_MODE_REQUEST.get_response() {
|
||||
assert!(pag.mode() == limine::paging::Mode::FOUR_LEVEL);
|
||||
println!("\t\t\t[OK]");
|
||||
} else {
|
||||
println!("\t\t\t[ERR]");
|
||||
panic!("okay we're missing our paging mode, something has gone wrong")
|
||||
}
|
||||
|
||||
// get limine memory map
|
||||
print!("\tgetting memory map data");
|
||||
let mem_map_res = MEMORY_MAP_REQUEST.get_response().expect("missing limine memory map data");
|
||||
let base_mem_offset = HHDM_REQUEST.get_response().expect("missing higher half memory data");
|
||||
println!("\t\t\t[OK]");
|
||||
|
||||
print!("\tgetting frame allocator");
|
||||
unsafe { pmm::PFrameAllocator::init(mem_map_res.entries()) };
|
||||
println!("\t\t\t[OK]");
|
||||
|
||||
print!("\tpaging init");
|
||||
unsafe { vmm::init(base_mem_offset.offset()) };
|
||||
println!("\t\t\t\t[OK]");
|
||||
|
||||
print!("\tkernel memory init");
|
||||
unsafe { kalloc::init() }
|
||||
println!("\t\t\t[OK]");
|
||||
|
||||
print!("starting interrupts");
|
||||
unsafe {
|
||||
// ensure interrupts are disabled
|
||||
amd64::instructions::cli();
|
||||
|
||||
// initialize PICs
|
||||
amd64::devices::i8259::PICS.lock().init();
|
||||
|
||||
// enable interrupts
|
||||
amd64::instructions::sti();
|
||||
};
|
||||
println!("\t\t\t\t[OK]");
|
||||
|
||||
println!("testing kernel ...");
|
||||
|
||||
print!("\ttesting kernel heap: ");
|
||||
let heap_value = Box::new(42);
|
||||
|
||||
assert_eq!(heap_value, Box::new(42_u64));
|
||||
|
||||
print!("{}", heap_value);
|
||||
|
||||
println!("\t\t\t[OK]");
|
||||
|
||||
print!("\ttesting syscall interrupt");
|
||||
libgoatweed::syscalls::k_test();
|
||||
|
||||
println!("kernel load complete");
|
||||
|
||||
println!("loading initramfs");
|
||||
// get the initramfs location in memory from limine and turn it into a slice we can use
|
||||
let initramfs = MODULE_REQUEST.get_response().expect("missing module info!").modules().first().expect("missing initramfs!");
|
||||
let initramfs_bytes = unsafe { &*ptr::slice_from_raw_parts(initramfs.addr(), initramfs.size() as usize) };
|
||||
|
||||
// parse the tar so we can go through it
|
||||
let initramfs = TarArchiveRef::new(initramfs_bytes).expect("initramfs not valid");
|
||||
let mut initramfs_iter = initramfs.entries();
|
||||
|
||||
println!("spawning init");
|
||||
// look through the initramfs tar to find a valid elf called init
|
||||
let init: Option<&[u8]> = loop {
|
||||
if let Some(i) = initramfs_iter.next() {
|
||||
if i.filename().as_str().expect("invalid file name").eq("init") {
|
||||
let elf = ElfBytes::<AnyEndian>::minimal_parse(&i.data());
|
||||
|
||||
match elf {
|
||||
Ok(_) => {
|
||||
break Some(i.data())
|
||||
}
|
||||
Err(_) => {
|
||||
break None
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break None
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// ensure init exists and then copy it into kernel memory
|
||||
let init: Vec<u8> = if let Some(init) = init {
|
||||
init.to_vec()
|
||||
} else {
|
||||
panic!("no init, halting...");
|
||||
};
|
||||
|
||||
// call pspawn to start init
|
||||
// TODO: we do it twice here to debug, we can make it one once we have init working
|
||||
libgoatweed::syscalls::p_spawn(init.as_ptr(), init.len());
|
||||
// libgoatweed::syscalls::p_spawn(init.as_ptr(), init.len());
|
||||
|
||||
loop {
|
||||
instructions::hlt()
|
||||
}
|
||||
}
|
67
src/syscalls/m_map.rs
Normal file
67
src/syscalls/m_map.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use core::sync::atomic::Ordering;
|
||||
use x86_64::registers::control::{Cr3, Cr3Flags};
|
||||
use x86_64::structures::paging::{OffsetPageTable, PageTableFlags};
|
||||
use x86_64::VirtAddr;
|
||||
use crate::arch::amd64::memory::vmm;
|
||||
use crate::arch::amd64::memory::vmm::{active_l4t, alloc_pages, find_free_virtual_address, free_pages};
|
||||
use crate::trafficcontrol::{CURRENT_PROCESS, PROCESSES};
|
||||
|
||||
///
|
||||
pub fn m_map(size: usize) -> *mut u8 {
|
||||
let start_addr = find_free_virtual_address(size);
|
||||
|
||||
if start_addr.is_none() {
|
||||
// TODO: return with Err and a error code
|
||||
return core::ptr::null_mut();
|
||||
}
|
||||
|
||||
let start_addr = start_addr.unwrap();
|
||||
|
||||
// swap to the process page table here
|
||||
let previous_cr3 = Cr3::read();
|
||||
|
||||
let processes = PROCESSES.read();
|
||||
let process = processes.get(&CURRENT_PROCESS.load(Ordering::SeqCst)).expect("Proccess called m_map while missing?");
|
||||
|
||||
unsafe {
|
||||
Cr3::write(process.page_table, Cr3Flags::empty())
|
||||
}
|
||||
|
||||
let mut mapper = unsafe { OffsetPageTable::new(active_l4t(), VirtAddr::new(vmm::PHYS_OFFSET.load(Ordering::Relaxed) as u64)) };
|
||||
let value;
|
||||
|
||||
if alloc_pages(&mut mapper, start_addr, size, PageTableFlags::PRESENT | PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE).is_ok() {
|
||||
value = start_addr as *mut u8
|
||||
} else {
|
||||
value = core::ptr::null_mut()
|
||||
}
|
||||
|
||||
// anddd switch back
|
||||
unsafe {
|
||||
Cr3::write(previous_cr3.0, previous_cr3.1);
|
||||
}
|
||||
|
||||
value
|
||||
}
|
||||
|
||||
///
|
||||
pub fn m_unmap(addr: *const u8, size: usize) {
|
||||
// swap to the process page table here
|
||||
let previous_cr3 = Cr3::read();
|
||||
|
||||
let processes = PROCESSES.read();
|
||||
let process = processes.get(&CURRENT_PROCESS.load(Ordering::SeqCst)).expect("Proccess called m_unmap while missing?");
|
||||
|
||||
unsafe {
|
||||
Cr3::write(process.page_table, Cr3Flags::empty())
|
||||
}
|
||||
|
||||
let mut mapper = unsafe { OffsetPageTable::new(active_l4t(), VirtAddr::new(vmm::PHYS_OFFSET.load(Ordering::Relaxed) as u64)) };
|
||||
|
||||
free_pages(&mut mapper, addr as u64, size).unwrap();
|
||||
|
||||
// anddd switch back
|
||||
unsafe {
|
||||
Cr3::write(previous_cr3.0, previous_cr3.1)
|
||||
}
|
||||
}
|
38
src/syscalls/mod.rs
Normal file
38
src/syscalls/mod.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use crate::print;
|
||||
use crate::println;
|
||||
use crate::syscalls::p_spawn::p_spawn;
|
||||
use libgoatweed::syscalls::Syscall;
|
||||
use crate::syscalls::m_map::{m_map, m_unmap};
|
||||
|
||||
pub mod p_spawn;
|
||||
pub mod m_map;
|
||||
|
||||
pub fn route(rax: u64, rbx: u64, rcx: u64, _rdx: u64, _rsi: u64, _rdi: u64) -> u64 {
|
||||
let syscall: Syscall = rax.into();
|
||||
|
||||
match syscall {
|
||||
Syscall::PSpawn => {
|
||||
p_spawn(rbx as *mut u8, rcx as usize);
|
||||
0
|
||||
},
|
||||
Syscall::KQuirky => {
|
||||
println!("haii from {}", rbx);
|
||||
0
|
||||
}
|
||||
Syscall::KTest => {
|
||||
println!("\t\t[OK]");
|
||||
0
|
||||
}
|
||||
Syscall::MMap => {
|
||||
m_map(rbx as usize) as u64
|
||||
}
|
||||
Syscall::MUnmap => {
|
||||
m_unmap(rbx as *const u8, rcx as usize);
|
||||
0
|
||||
}
|
||||
_ => {
|
||||
println!("Invalid syscall: {}", rax);
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
15
src/syscalls/p_spawn.rs
Normal file
15
src/syscalls/p_spawn.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use core::slice;
|
||||
use crate::{print, trafficcontrol};
|
||||
use crate::println;
|
||||
|
||||
///
|
||||
pub fn p_spawn(elf_location: *mut u8, elf_length: usize) {
|
||||
println!("location: {:#?}, length: {}", elf_location, elf_length);
|
||||
|
||||
if elf_location.is_null() {
|
||||
println!("null pointer");
|
||||
return;
|
||||
}
|
||||
|
||||
trafficcontrol::Process::spawn(unsafe { slice::from_raw_parts(elf_location, elf_length) });
|
||||
}
|
275
src/trafficcontrol/mod.rs
Normal file
275
src/trafficcontrol/mod.rs
Normal file
|
@ -0,0 +1,275 @@
|
|||
use crate::print;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::collections::VecDeque;
|
||||
use alloc::sync::Arc;
|
||||
use core::arch::asm;
|
||||
use core::ptr;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use elf::ElfBytes;
|
||||
use elf::endian::AnyEndian;
|
||||
use spin::lazy::Lazy;
|
||||
use spin::rwlock::RwLock;
|
||||
use x86_64::registers::control::{Cr3, Cr3Flags};
|
||||
use x86_64::structures::idt::{InterruptStackFrame};
|
||||
use x86_64::structures::paging::{FrameAllocator, Mapper, OffsetPageTable, Page, PageTableFlags, PhysFrame, Size4KiB};
|
||||
use x86_64::VirtAddr;
|
||||
use crate::arch::amd64;
|
||||
use crate::arch::amd64::interrupts::gdt::GDT;
|
||||
use crate::arch::amd64::memory::{pmm, vmm};
|
||||
use crate::println;
|
||||
use elf::abi;
|
||||
use hashbrown::HashMap;
|
||||
|
||||
static PID: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
pub static CURRENT_PROCESS: AtomicUsize = AtomicUsize::new(usize::MAX); // MAX = no process
|
||||
|
||||
pub static PROCESSES: Lazy<RwLock<HashMap<usize, Box<Process>>>> = Lazy::new(|| {
|
||||
RwLock::new(HashMap::new())
|
||||
});
|
||||
|
||||
static PROCESS_QUEUE: Lazy<RwLock<VecDeque<usize>>> = Lazy::new(|| {
|
||||
RwLock::new(VecDeque::new())
|
||||
});
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Process {
|
||||
pub id: usize,
|
||||
pub parent: Option<Arc<Process>>,
|
||||
instruction_pointer: u64,
|
||||
stack_pointer: u64,
|
||||
pub page_table: PhysFrame,
|
||||
saved_registers: [u64; 15]
|
||||
}
|
||||
|
||||
impl Process {
|
||||
pub fn spawn(elf: &[u8]) {
|
||||
if let Ok(pid) = Self::load(elf) {
|
||||
println!("pid: {} created", pid);
|
||||
} else {
|
||||
panic!("failed to spawn process!");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(elf: &[u8]) -> Result<usize, ()> {
|
||||
let page_table_frame = pmm::get_frame_allocator().lock().allocate_frame().expect("Failed to allocate frame");
|
||||
let page_table = unsafe { vmm::new_page_table(page_table_frame) };
|
||||
let kernel_page_table = unsafe { vmm::active_l4t() };
|
||||
let kernel_table_frame = Cr3::read().0;
|
||||
|
||||
// this may seem weird to do for security since this allows you to get the kernel layout
|
||||
// in userspace (which is a valid issue) but we need to be able to have kernel code
|
||||
// running when we switch into the process and writing KPTI seems like hell at this stage
|
||||
// darn you meltdown and spectre and friends
|
||||
let pages = page_table.iter_mut().zip(kernel_page_table.iter());
|
||||
for (process, kernel) in pages {
|
||||
*process = kernel.clone();
|
||||
}
|
||||
|
||||
let mut mapper = unsafe { OffsetPageTable::new(page_table, VirtAddr::new(vmm::PHYS_OFFSET.load(Ordering::Relaxed) as u64)) };
|
||||
|
||||
// 128KB stack here, linux does 1MB so idk probably fine
|
||||
const USER_STACK_TOP: u64 = 0x7000_0000_0000;
|
||||
const STACK_SIZE_PAGES: usize = 32;
|
||||
const STACK_SIZE_BYTES: u64 = (STACK_SIZE_PAGES * 4096) as u64;
|
||||
let stack_bottom = USER_STACK_TOP - STACK_SIZE_BYTES;
|
||||
|
||||
println!("Allocating stack: bottom={:#x}, top={:#x}, size={}KB",
|
||||
stack_bottom, USER_STACK_TOP, STACK_SIZE_BYTES / 1024);
|
||||
|
||||
vmm::alloc_pages(
|
||||
&mut mapper,
|
||||
stack_bottom,
|
||||
STACK_SIZE_PAGES,
|
||||
PageTableFlags::PRESENT | PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE
|
||||
).expect("Failed to allocate user stack");
|
||||
|
||||
let initial_stack_pointer = USER_STACK_TOP - 64;
|
||||
let elf_parse = ElfBytes::<AnyEndian>::minimal_parse(elf).expect("Invalid elf!");
|
||||
let entry = elf_parse.ehdr.e_entry;
|
||||
|
||||
for segment in elf_parse.segments().expect("Missing segments?") {
|
||||
if segment.p_type == abi::PT_LOAD {
|
||||
let start_vaddr = segment.p_vaddr;
|
||||
let end_vaddr = segment.p_vaddr + segment.p_memsz;
|
||||
|
||||
let start_page = VirtAddr::new(start_vaddr).align_down(4096u64).as_u64();
|
||||
let end_page = VirtAddr::new(end_vaddr - 1).align_up(4096u64).as_u64();
|
||||
let num_pages = ((end_page - start_page) / 4096) as usize;
|
||||
|
||||
// make all segments writable for the copy operation
|
||||
let mut temp_flags = PageTableFlags::PRESENT | PageTableFlags::USER_ACCESSIBLE | PageTableFlags::WRITABLE;
|
||||
|
||||
// we can still respect the executable flag from the start though
|
||||
if segment.p_flags & abi::PF_X == 0 { // not executable
|
||||
temp_flags |= PageTableFlags::NO_EXECUTE;
|
||||
}
|
||||
|
||||
vmm::alloc_pages(&mut mapper, start_page, num_pages, temp_flags)?;
|
||||
}
|
||||
}
|
||||
|
||||
for i in elf_parse.segments().expect("Missing segments?") {
|
||||
if i.p_type == abi::PT_LOAD {
|
||||
let segment_data = elf.as_ptr().wrapping_add(i.p_offset as usize);
|
||||
unsafe {
|
||||
let (_, cr3_flags) = Cr3::read();
|
||||
Cr3::write(page_table_frame, cr3_flags);
|
||||
|
||||
if i.p_filesz > 0 {
|
||||
ptr::copy_nonoverlapping(segment_data, i.p_vaddr as *mut u8, i.p_filesz as usize);
|
||||
}
|
||||
if i.p_memsz > i.p_filesz {
|
||||
ptr::write_bytes((i.p_vaddr + i.p_filesz) as *mut u8, 0, (i.p_memsz - i.p_filesz) as usize);
|
||||
}
|
||||
|
||||
Cr3::write(kernel_table_frame, cr3_flags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ok now that we have copied the data, write protect the read only sections
|
||||
for segment in elf_parse.segments().expect("Missing segments?") {
|
||||
if segment.p_type == abi::PT_LOAD {
|
||||
let start_vaddr = segment.p_vaddr;
|
||||
let end_vaddr = segment.p_vaddr + segment.p_memsz;
|
||||
|
||||
let start_page_addr = VirtAddr::new(start_vaddr).align_down(4096u64);
|
||||
let end_page_addr = VirtAddr::new(end_vaddr).align_up(4096u64);
|
||||
|
||||
// we should calculate the final flags here
|
||||
let mut final_flags = PageTableFlags::PRESENT | PageTableFlags::USER_ACCESSIBLE;
|
||||
if segment.p_flags & abi::PF_W != 0 {
|
||||
final_flags |= PageTableFlags::WRITABLE;
|
||||
}
|
||||
if segment.p_flags & abi::PF_X == 0 {
|
||||
final_flags |= PageTableFlags::NO_EXECUTE;
|
||||
}
|
||||
|
||||
let mut current_addr = start_page_addr;
|
||||
while current_addr < end_page_addr {
|
||||
let page: Page<Size4KiB> = Page::from_start_address(current_addr)
|
||||
.expect("Address not page aligned");
|
||||
unsafe {
|
||||
// anndd finally replace the flags
|
||||
mapper.update_flags(page, final_flags)
|
||||
.expect("update_flags failed")
|
||||
.flush();
|
||||
}
|
||||
current_addr += 4096u64;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let pid = PID.fetch_add(1, Ordering::SeqCst);
|
||||
let process = Process {
|
||||
id: pid,
|
||||
parent: None,
|
||||
instruction_pointer: entry,
|
||||
stack_pointer: initial_stack_pointer,
|
||||
page_table: page_table_frame,
|
||||
saved_registers: [0; 15],
|
||||
};
|
||||
|
||||
let mut processes = PROCESSES.write();
|
||||
let mut queue = PROCESS_QUEUE.write();
|
||||
|
||||
processes.insert(pid, Box::new(process));
|
||||
queue.push_back(pid);
|
||||
|
||||
println!("Created process {} with entry={:#x}, stack={:#x}", pid, entry, initial_stack_pointer);
|
||||
Ok(pid)
|
||||
}
|
||||
|
||||
pub fn start(&self) {
|
||||
println!("Running pid {}", self.id);
|
||||
|
||||
// lets not clobber the stack or anything with a timer intr
|
||||
amd64::instructions::cli();
|
||||
|
||||
// swap to the process's page table
|
||||
unsafe {
|
||||
Cr3::write(self.page_table, Cr3Flags::empty());
|
||||
}
|
||||
|
||||
unsafe {
|
||||
asm!(
|
||||
// TODO: figure out why the gp registers cause issues
|
||||
"mov rax, [{regs} + 0]",
|
||||
"mov rbx, [{regs} + 8]",
|
||||
"mov rcx, [{regs} + 16]",
|
||||
"mov rdx, [{regs} + 24]",
|
||||
"mov rsi, [{regs} + 32]",
|
||||
"mov rdi, [{regs} + 40]",
|
||||
"mov rbp, [{regs} + 48]",
|
||||
"mov r8, [{regs} + 56]",
|
||||
"mov r9, [{regs} + 64]",
|
||||
"mov r10, [{regs} + 72]",
|
||||
"mov r11, [{regs} + 80]",
|
||||
"mov r12, [{regs} + 88]",
|
||||
"mov r13, [{regs} + 96]",
|
||||
"mov r14, [{regs} + 104]",
|
||||
"mov r15, [{regs} + 112]",
|
||||
|
||||
// all the segments restore here
|
||||
"mov ds, {data_seg:x}",
|
||||
"mov es, {data_seg:x}",
|
||||
"mov fs, {data_seg:x}",
|
||||
"mov gs, {data_seg:x}",
|
||||
|
||||
// iretq stack frame goes here
|
||||
"push {data_seg:r}",
|
||||
"push {stack_ptr}",
|
||||
"push {rflags}",
|
||||
"push {code_seg:r}",
|
||||
"push {instruction_pointer}",
|
||||
|
||||
// lets jump back
|
||||
"iretq",
|
||||
|
||||
regs = in(reg) self.saved_registers.as_ptr(),
|
||||
|
||||
data_seg = in(reg) GDT.1.user_data_selector.0,
|
||||
code_seg = in(reg) GDT.1.user_code_selector.0,
|
||||
|
||||
stack_ptr = in(reg) self.stack_pointer,
|
||||
instruction_pointer = in(reg) self.instruction_pointer,
|
||||
|
||||
rflags = in(reg) 0x200_u64, // this will reenable interrupts
|
||||
|
||||
options(noreturn)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute_next_round_robin(interrupt_stack_frame: InterruptStackFrame, saved_registers: [u64; 15]) {
|
||||
let mut queue = PROCESS_QUEUE.write();
|
||||
let mut processes = PROCESSES.write();
|
||||
if queue.is_empty() || processes.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let current_pid = CURRENT_PROCESS.load(Ordering::SeqCst);
|
||||
if current_pid != usize::MAX {
|
||||
if let Some(process) = processes.get_mut(¤t_pid) {
|
||||
process.saved_registers = saved_registers;
|
||||
process.instruction_pointer = interrupt_stack_frame.instruction_pointer.as_u64();
|
||||
process.stack_pointer = interrupt_stack_frame.stack_pointer.as_u64();
|
||||
}
|
||||
}
|
||||
|
||||
queue.rotate_left(1);
|
||||
|
||||
let new_pid = queue.front().expect("Missing next pid in queue?");
|
||||
|
||||
if let Some(process) = processes.get(new_pid) {
|
||||
let process = process.as_ref().clone();
|
||||
drop(processes);
|
||||
drop(queue);
|
||||
|
||||
CURRENT_PROCESS.store(process.id, Ordering::SeqCst);
|
||||
|
||||
process.start();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue