Compare commits

33 Commits

Author SHA1 Message Date
cbc1752e73 Working on reorganizing kernel.c 2023-12-16 09:51:02 -06:00
df90bd7313 process struct now uses address_space struct 2023-11-09 23:00:12 -06:00
1375596310 Changed some length arguments to size_t in syscalls.h 2023-11-09 21:26:55 -06:00
61e5ebf513 Removed unused #include in sys/syscalls.h 2023-11-09 21:25:30 -06:00
58ae7f9f89 Removed syscallret_t typedef.
Syscalls just return an int
2023-11-09 21:24:07 -06:00
b4ea4b8ca1 Merged sighandler.h into sigaction.h 2023-11-09 21:19:13 -06:00
f36fe615cc Wrote wrappers for each available system call
Many calls aren't implemented, and simply return an error.
2023-11-09 21:17:08 -06:00
c43b23bed3 Added sigaction.h and sighandler.h to installed headers 2023-11-09 21:16:22 -06:00
e52948830e Added enum for future kernel log levels 2023-11-08 14:16:15 -06:00
31f7eb34a6 Renamed some syscalls 2023-11-08 14:15:46 -06:00
c5ef8c53a7 Started separating important structs from kernel.c 2023-11-08 14:13:03 -06:00
108e04a8f0 Page fault handler now allocates new page tables
Rather than contantly checking if a page table exists before access, the fault handler will automatically allocate them as needed.
2023-11-07 12:44:23 -06:00
091830e508 Added constructor to x86 context 2023-11-07 12:37:33 -06:00
98528dbc4a Created x86/mp.h 2023-11-07 12:36:59 -06:00
b9208aceba Created types/sigaction.h 2023-11-07 12:34:31 -06:00
aa77b0e7a2 Created sighandler.h 2023-11-07 12:34:06 -06:00
d4ed72cc46 Removed leading underscore from syscallarg.h 2023-11-07 12:33:42 -06:00
8e64741a9e Created types/syscallret.h 2023-11-07 12:33:24 -06:00
4b7cf4e04a Removed leading underscore from syscallarg.h 2023-11-07 12:33:04 -06:00
58d50a5e9a Removed leading underscore from physaddr.h 2023-11-07 12:32:42 -06:00
669ab6ba8a Removed leading underscore from pid.h 2023-11-07 12:32:04 -06:00
185d3f6677 Added function to delete contents of AVL tree 2023-11-07 12:31:16 -06:00
9fb5b69745 Deleted OS-DESIGN.md 2023-11-07 12:30:24 -06:00
85020f2254 Removed leading underscore in oid.h 2023-11-07 12:30:13 -06:00
2dafa56d4b Added document to describe design of OS 2023-10-23 18:48:07 -05:00
a5ce86147d Created small static library to wrap system call ABI 2023-09-07 00:58:57 -05:00
112b4204a7 Removed old commented-out lines from heap.c 2023-09-07 00:57:12 -05:00
e258bfcc9f Moved kernel base 4MiB lower 2023-09-07 00:56:40 -05:00
43440310f2 Added type definitions for process and object IDs 2023-09-07 00:55:45 -05:00
365272e360 Added null pointer check to avltree constructor 2023-09-07 00:54:28 -05:00
08db5fe1f7 Kernel heap now allocates all required pages at once
As opposed to allocating pages individually
2023-09-02 21:51:19 -05:00
29a528780b Added syscalls to map to particular physical address 2023-09-02 21:50:35 -05:00
75b7e08e96 Kernel page allocator and heap use new algorithms 2023-08-31 14:37:38 -05:00
44 changed files with 1483 additions and 1104 deletions

2
.gitignore vendored
View File

@@ -6,7 +6,7 @@ quark-kernel
autom4te.cache/ autom4te.cache/
.deps .deps
.dirstamp .dirstamp
.vscode #.vscode
aclocal.m4 aclocal.m4
ar-lib ar-lib
compile compile

45
.vscode/c_cpp_properties.json vendored Normal file
View File

@@ -0,0 +1,45 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"~/.quark/include"
],
"defines": [],
"compilerPath": "/opt/cross/bin/i686-elf-gcc",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-gcc-x86",
"compilerArgs": [
"-ffreestanding",
"-nostdlib"
]
},
{
"name": "i686-cross",
"includePath": [
"/home/nathan/Documents/projects/quark-os/quark-kernel/**",
"/opt/quark-os/rootfs/include/",
"${workspaceFolder}/include"
],
"defines": [],
"compilerPath": "/opt/cross/bin/i686-elf-gcc",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-x86",
"compilerArgs": [
"-ffreestanding ",
"-nostdlib"
],
"cStandard": "c11",
"mergeConfigurations": false,
"browse": {
"path": [
"${workspaceFolder}/**"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}

37
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,37 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/opt/quark-os/rootfs/apps/quark-kernel",
"args": [],
"stopAtEntry": true,
"stopAtConnect": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerServerAddress": "localhost:1234",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}/*,
{
"text": "-target-select remote localhost:1234",
"ignoreFailures": false
}*/
],
"preLaunchTask": "Launch QEMU (i386)"
/*"debugServerPath": "/bin/qemu-system-i386",
"debugServerArgs": "-hda /opt/quark-os/quark.img -serial stdio -s -S",
"serverLaunchTimeout": 2000*/
}
]
}

28
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,28 @@
{
"files.exclude": {
"**/*.o": true,
"**/ar-lib": true,
"**/Makefile": true,
"**/*.in": true,
"**/stamp-h1": true,
"**/*.a": true,
"aclocal.m4": true,
"compile": true,
"config.log": true,
"config.status": true,
"configure*": true,
"depcomp": true,
"install-sh": true,
"missing": true,
"**/.deps": true,
"autom4te.cache": true,
"**/quark-kernel": true
},
"C_Cpp.default.cStandard": "c11",
"files.associations": {
"memmap.h": "c",
"pid.h": "c",
"sighandler.h": "c",
"string.h": "c"
}
}

27
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,27 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make",
"type": "shell",
"command": "PATH=\"$PATH:/opt/cross/bin\" make",
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Launch QEMU (i386)",
"type": "shell",
"command": "qemu-system-i386 -hda /opt/quark-os/quark.img -serial stdio -s -S",
"isBackground": true,
"group": {
"kind": "test",
"isDefault": true
}
}
]
}

674
LICENSE
View File

@@ -1,674 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. 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
them 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 prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. 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.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey 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;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If 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 convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU 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 that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
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.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
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.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
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
state 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 3 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, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program 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, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU 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. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

View File

@@ -1 +1,3 @@
nobase_include_HEADERS = types/status.h types/syscallid.h types/physaddr.h types/syscallarg.h nobase_include_HEADERS = types/status.h types/syscallid.h types/physaddr.h \
types/syscallarg.h sys/syscalls.h types/pid.h types/oid.h \
types/sigaction.h types/message.h

20
include/addressspace.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef QUARK_ADDRESSSPACE_H
#define QUARK_ADDRESSSPACE_H
#include "types/physaddr.h"
typedef struct address_space_t
{
physaddr_t page_table;
int refcount;
} address_space_t;
address_space_t *address_space_construct();
void address_space_switch(address_space_t *address_space);
address_space_t *address_space_release(address_space_t *address_space);
void address_space_destroy(address_space_t *address_space);
#endif

View File

@@ -39,3 +39,11 @@ struct avltree_t *avl_remove(struct avltree_t *tree, int key);
* @return void* * @return void*
*/ */
void *avl_get(struct avltree_t *tree, int key); void *avl_get(struct avltree_t *tree, int key);
/**
* @brief Removes every node present on the given tree.
*
* @param tree
* @return NULL
*/
void *avl_clear(struct avltree_t *tree);

View File

@@ -8,13 +8,11 @@
* This function contructs the heap's internal tables, allocating pages as needed * This function contructs the heap's internal tables, allocating pages as needed
* to do so. * to do so.
* *
* @param page_stack Pointer to the page stack descriptor
* @param base Base location of the heap to contruct * @param base Base location of the heap to contruct
* @param heap_size Total size in bytes of the heap * @param heap_size Total size in bytes of the heap
* @param block_size Size in bytes of a single unit of allocation
* @return a status code * @return a status code
*/ */
int kminit(void *base, void* start, size_t heap_size, size_t block_size); int kminit(void *base, size_t heap_size);
/** /**
* @brief Allocates a block of memory containing at least `size` bytes. * @brief Allocates a block of memory containing at least `size` bytes.

View File

@@ -5,13 +5,19 @@
#include "queue.h" #include "queue.h"
#include "mmgr.h" #include "mmgr.h"
#include "syscalls.h" #include "syscalls.h"
#include "addressspace.h"
#include "process.h"
#include "types/message.h"
#include "types/syscallid.h" #include "types/syscallid.h"
#include "types/status.h" #include "types/status.h"
#include "types/pid.h"
#include "types/oid.h"
#include "types/sighandler.h"
#include <libmalloc/memmap.h> #include <libmalloc/memmap.h>
#include <stddef.h> #include <stddef.h>
#define MAX_SYSCALL_ID 256 #define MAX_SYSCALL_ID 256
#define module_limit 8 #define MODULE_LIMIT 8
#define IO_OP 1 << 0 #define IO_OP 1 << 0
#define IO_SYNC 0 << 0 #define IO_SYNC 0 << 0
@@ -22,8 +28,6 @@
#define IO_PORT 1 << 1 #define IO_PORT 1 << 1
#define IO_MAILBOX 2 << 1 #define IO_MAILBOX 2 << 1
typedef unsigned long (*signal_handler_t)(void*, void*);
struct process_context_t; struct process_context_t;
struct module_t struct module_t
@@ -40,94 +44,42 @@ struct boot_info_t
size_t memory_size; size_t memory_size;
memory_map_t map; memory_map_t map;
size_t module_count; size_t module_count;
struct module_t modules[module_limit]; struct module_t modules[MODULE_LIMIT];
};
struct message_t
{
unsigned long sender;
unsigned long code;
unsigned long args[6];
};
enum process_state_t
{
PROCESS_ACTIVE,
PROCESS_REQUESTING,
PROCESS_SENDING
};
struct process_t
{
unsigned long pid;
int priority;
physaddr_t page_table;
enum process_state_t state;
struct queue_t sending_queue;
struct queue_t message_queue;
struct message_t *message_buffer;
struct process_context_t *ctx;
};
struct port_t
{
unsigned long id;
unsigned long owner_pid;
};
struct signal_action_t
{
unsigned long pid;
signal_handler_t func_ptr;
void (*trampoline_ptr)();
void *userdata;
};
struct signal_context_t
{
unsigned long signal_id;
};
struct kernel_t
{
struct syscall_t syscall_table[MAX_SYSCALL_ID];
struct priority_queue_t priority_queue;
struct avltree_t *interrupt_handlers;
struct avltree_t *port_table;
struct avltree_t *process_table;
struct process_t *active_process;
int next_pid;
}; };
void kernel_initialize(struct boot_info_t *boot_info); void kernel_initialize(struct boot_info_t *boot_info);
error_t set_syscall(int id, int arg_count, int pid, void *func_ptr); process_t *kernel_get_process(pid_t pid);
size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags); process_t *kernel_get_active_process();
error_t kernel_set_syscall(int id, int arg_count, void *func_ptr);
size_t kernel_do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags);
error_t kernel_load_module(struct module_t *module); error_t kernel_load_module(struct module_t *module);
unsigned long kernel_current_pid(); pid_t kernel_current_pid();
struct process_context_t *kernel_current_context(); struct process_context_t *kernel_current_context();
error_t kernel_store_active_context(struct process_context_t *context); error_t kernel_store_active_context(struct process_context_t *context);
unsigned long kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space); pid_t kernel_spawn_process(void *program_entry, int priority, address_space_t *address_space);
struct process_context_t *kernel_advance_scheduler(); struct process_context_t *kernel_advance_scheduler();
error_t kernel_terminate_process(size_t process_id); void kernel_schedule_process(process_t *process);
error_t kernel_terminate_process(pid_t process_id);
error_t kernel_create_port(unsigned long id); error_t kernel_create_port(unsigned long id);
error_t kernel_remove_port(unsigned long id); error_t kernel_remove_port(unsigned long id);
unsigned long kernel_get_port_owner(unsigned long id); pid_t kernel_get_port_owner(unsigned long id);
error_t kernel_send_message(unsigned long recipient, struct message_t *message); int kernel_receive_message(struct message_t *buffer, int flags);
error_t kernel_queue_message(unsigned long recipient, struct message_t *message);
error_t kernel_register_interrupt_handler(unsigned long interrupt, signal_handler_t handler, void *userdata); error_t kernel_register_interrupt_handler(unsigned long interrupt, signal_handler_t handler, void *userdata);
@@ -137,6 +89,10 @@ error_t kernel_execute_interrupt_handler(unsigned long interrupt);
error_t kernel_signal_return(); error_t kernel_signal_return();
int receive_message(struct message_t *buffer, int flags); error_t kernel_create_object(size_t size, unsigned long flags, oid_t *id);
void panic(const char *message) __attribute__ ((noreturn)); error_t kernel_attach_object(oid_t id, void *virt_addr);
error_t kernel_release_object(oid_t id);
void kernel_panic(const char *message) __attribute__ ((noreturn));

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "sharedobject.h"
#include "platform/paging.h" #include "platform/paging.h"
#include "types/physaddr.h" #include "types/physaddr.h"
#include "types/status.h" #include "types/status.h"
@@ -23,7 +24,7 @@ physaddr_t reserve_pages(size_t size);
* @param size * @param size
* @return int * @return int
*/ */
int free_pages(physaddr_t location, size_t size); size_t free_pages(physaddr_t location);
/** /**
* @brief Reserves a single page and returns its physical address. * @brief Reserves a single page and returns its physical address.
@@ -92,6 +93,36 @@ physaddr_t create_address_space();
*/ */
physaddr_t current_address_space(); physaddr_t current_address_space();
/**
* @brief Maps a region in virtual memory with the specified flags.
*
* @param page
* @param frame
* @param size
* @param flags
*/
error_t map_region(void *page, physaddr_t frame, size_t size, int flags);
/**
* @brief Maps a shared object into virtual memory.
*
* @param location
* @param object
* @return
*/
error_t map_object(void *location, shared_object_t *object);
/**
* @brief Unmaps a region in virtual memory. All pages which contain some part
* of the region specified will be unmapped.
*
* @param page
* @param size
*
* @returns The physical address of the first page that was unmapped
*/
physaddr_t unmap_region(void *page, size_t size);
/** /**
* @brief Maps a single page with the specified flags. * @brief Maps a single page with the specified flags.
* *
@@ -100,7 +131,7 @@ physaddr_t current_address_space();
* @param flags * @param flags
* @return int * @return int
*/ */
int map_page(void *page, physaddr_t frame, int flags); error_t map_page(void *page, physaddr_t frame, int flags);
/** /**
* @brief Unmaps a single page, returning the physical address of the frame it * @brief Unmaps a single page, returning the physical address of the frame it

View File

@@ -33,6 +33,8 @@ struct process_context_t
void load_context(struct process_context_t *context); void load_context(struct process_context_t *context);
void context_construct(struct process_context_t *context);
void *get_context_pc(struct process_context_t *context); void *get_context_pc(struct process_context_t *context);
void set_context_pc(struct process_context_t *context, void *pc); void set_context_pc(struct process_context_t *context, void *pc);

39
include/process.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef QUARK_PROCESS_H
#define QUARK_PROCESS_H
#include "types/message.h"
#include "types/pid.h"
#include "types/physaddr.h"
#include "types/status.h"
#include "queue.h"
#include "addressspace.h"
#include "sharedobject.h"
typedef enum process_state_t
{
PROCESS_ACTIVE,
PROCESS_REQUESTING,
PROCESS_SENDING
} process_state_t;
typedef struct process_t
{
pid_t pid;
int priority;
address_space_t *address_space;
struct avltree_t *shared_objects;
process_state_t state;
struct queue_t message_queue;
struct message_t *message_buffer;
struct process_context_t *ctx;
} process_t;
process_t *process_construct(pid_t pid, void *entry, void *stack, int priority, address_space_t *address_space);
error_t process_queue_message(process_t *process, message_t *msg);
error_t process_store_object(process_t *process, int id, void *location);
void *process_get_object(process_t *process, int id);
#endif

19
include/sharedobject.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef QUARK_SHARED_OBJECT_H
#define QUARK_SHARED_OBJECT_H
#include "types/physaddr.h"
#include <stddef.h>
typedef struct shared_object_t
{
physaddr_t phys_addr;
size_t size;
unsigned long access_flags;
unsigned long refcount;
} shared_object_t;
struct shared_object *create_shared_object(size_t size, unsigned long flags);
void destroy_shared_object(struct shared_object_t *obj);
#endif

236
include/sys/syscalls.h Normal file
View File

@@ -0,0 +1,236 @@
#ifndef _QUARK_SYSCALLS_H
#define _QUARK_SYSCALLS_H
#include <types/syscallid.h>
#include <types/physaddr.h>
#include <types/pid.h>
#include <types/sigaction.h>
#include <stddef.h>
/**
* @brief Executes a system call
* @param id The ID of the syscall to execute
* @return An error code, with 0 indicating success
*/
int _do_syscall(syscall_id_t id, ...);
/**
* @brief
* @param s The string to print
* @return
*/
static inline int kprint(const char *s)
{
return _do_syscall(SYSCALL_TEST, s);
}
/**
* @brief
* @param addr
* @param length
* @param flags
* @return
*/
static inline int map_anon(void *addr, size_t length, long flags)
{
return _do_syscall(SYSCALL_MAP_ANON, addr, length, flags);
}
/**
* @brief
* @param addr
* @param length
* @return
*/
static inline int unmap_anon(void *addr)
{
return _do_syscall(SYSCALL_UNMAP_ANON, addr);
}
/**
* @brief
* @param addr
* @param phys_addr
* @param length
* @return
*/
static inline int map_physical(void *addr, physaddr_t phys_addr, size_t length)
{
return _do_syscall(SYSCALL_MAP_PHYS, addr, phys_addr, length);
}
/**
* @brief
* @param addr
* @param length
* @return
*/
static inline int unmap_physical(void *addr, size_t length)
{
return _do_syscall(SYSCALL_UNMAP_PHYS, addr, length);
}
/**
* @brief
* @param id
* @return
*/
static inline int open_port(int id)
{
return _do_syscall(SYSCALL_OPEN_PORT, id);
}
/**
* @brief
* @param id
* @return
*/
static inline int close_port(int id)
{
return _do_syscall(SYSCALL_CLOSE_PORT, id);
}
/**
* @brief
* @param recipient
* @param message
* @param flags
* @return
*/
static inline int send_pid(pid_t recipient, void *message, int flags)
{
return _do_syscall(SYSCALL_SEND_PID, recipient, message, flags);
}
/**
* @brief
* @param recipient
* @param message
* @param flags
* @return
*/
static inline int send_port(int recipient, void *message, int flags)
{
return _do_syscall(SYSCALL_SEND_PORT, recipient, message, flags);
}
/**
* @brief
* @param buffer
* @param flags
* @return
*/
static inline int receive(void *buffer, int flags)
{
return _do_syscall(SYSCALL_RECEIVE, buffer, flags);
}
/**
* @brief
* @param location
* @param size
* @param flags
* @return
*/
static inline int create_object(void *location, size_t size, int flags)
{
return _do_syscall(SYSCALL_CREATE_OBJECT, location, size, flags);
}
/**
* @brief
* @param id
* @param location
* @return
*/
static inline int aquire_object(int id, void *location)
{
return _do_syscall(SYSCALL_AQUIRE_OBJECT, id, location);
}
/**
* @brief
* @param id
* @return
*/
static inline int release_object(int id)
{
return _do_syscall(SYSCALL_RELEASE_OBJECT, id);
}
/**
* @brief
* @return
*/
static inline int get_pid()
{
return _do_syscall(SYSCALL_GET_PID);
}
/**
* @brief
* @param entry
* @param arg
* @param stack
* @param flags
* @return
*/
static inline int clone(void (*entry)(void*), void *arg, void *stack, int flags)
{
return _do_syscall(SYSCALL_CLONE, entry, arg, stack, flags);
}
/**
* @brief
* @param id
* @param action
* @param flags
* @return
*/
static inline int signal_action(int id, struct signal_action_t *action, int flags)
{
return _do_syscall(SYSCALL_SIGNAL_ACTION, id, action, flags);
}
/**
* @brief
* @return
*/
static inline int signal_return()
{
return _do_syscall(SYSCALL_SIGNAL_RETURN);
}
/**
* @brief
* @param pid
* @param sigid
* @return
*/
static inline int signal_raise(pid_t pid, int sigid)
{
return _do_syscall(SYSCALL_SIGNAL_RAISE, pid, sigid);
}
/**
* @brief
* @param id
* @param action
* @param flags
* @return
*/
static inline int intr_action(int id, struct signal_action_t *action, int flags)
{
return _do_syscall(SYSCALL_INTR_ACTION, id, action, flags);
}
/**
* @brief
* @return
*/
static inline int intr_return()
{
return _do_syscall(SYSCALL_INTR_RETURN);
}
#endif

View File

@@ -1,22 +1,27 @@
#pragma once #ifndef QUARK_SYSCALL_DEF_H
#define QUARK_SYSCALL_DEF_H
#include "types/syscallarg.h" #include "types/syscallarg.h"
#include "types/sigaction.h"
#include "types/pid.h"
#include "types/oid.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
typedef size_t (*syscall_ptr_0_t)(); extern void *syscall_table[];
typedef size_t (*syscall_ptr_1_t)(syscall_arg_t); typedef int (*syscall_ptr_0_t)();
typedef size_t (*syscall_ptr_2_t)(syscall_arg_t, syscall_arg_t); typedef int (*syscall_ptr_1_t)(syscall_arg_t);
typedef size_t (*syscall_ptr_3_t)(syscall_arg_t, syscall_arg_t, syscall_arg_t); typedef int (*syscall_ptr_2_t)(syscall_arg_t, syscall_arg_t);
typedef int (*syscall_ptr_3_t)(syscall_arg_t, syscall_arg_t, syscall_arg_t);
struct syscall_t struct syscall_t
{ {
bool defined; bool defined;
int arg_count; int arg_count;
int process_id;
union union
{ {
syscall_ptr_0_t func_ptr_0; syscall_ptr_0_t func_ptr_0;
@@ -26,18 +31,106 @@ struct syscall_t
}; };
}; };
size_t test_syscall(syscall_arg_t str); /**
* @brief Outputs the given string from kernel mode.
*
* @param str The string to output.
*/
int test_syscall(syscall_arg_t str);
size_t mmap(syscall_arg_t location, syscall_arg_t length, syscall_arg_t flags); /**
*
*/
int syscall_map_anon(syscall_arg_t location, syscall_arg_t length, syscall_arg_t flags);
size_t munmap(syscall_arg_t location, syscall_arg_t length); /**
*
*/
int syscall_unmap_anon(syscall_arg_t location);
size_t terminate_self(); /**
*
*/
int syscall_map_physical(syscall_arg_t arg_addr, syscall_arg_t arg_phys_addr, syscall_arg_t arg_length);
size_t send(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags); /**
*
*/
int syscall_unmap_physical(syscall_arg_t arg_addr, syscall_arg_t arg_length);
size_t receive(syscall_arg_t buffer, syscall_arg_t flags); /**
*
*/
int syscall_open_port(syscall_arg_t id);
size_t open_port(syscall_arg_t id); /**
*
*/
int syscall_close_port(syscall_arg_t id);
size_t close_port(syscall_arg_t id); /**
*
*/
int syscall_send_pid(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags);
/**
*
*/
int syscall_send_port(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags);
/**
*
*/
int syscall_receive(syscall_arg_t buffer, syscall_arg_t flags);
/**
*
*/
int syscall_create_object(void *location, size_t size, int flags);
/**
*
*/
int syscall_aquire_object(oid_t id, void *location);
/**
*
*/
int syscall_release_object(oid_t id);
/**
*
*/
int syscall_get_pid();
/**
*
*/
int syscall_clone(void (*entry)(void*), void *arg, void *stack, int flags);
/**
*
*/
int syscall_signal_action(int id, struct signal_action_t *action, int flags);
/**
*
*/
int syscall_signal_return();
/**
*
*/
int syscall_signal_raise(pid_t pid, int sigid);
/**
*
*/
int syscall_intr_action(int id, struct signal_action_t *action, int flags);
/**
*
*/
int syscall_intr_return();
#endif

13
include/types/message.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef QUARK_MESSAGE_H
#define QUARK_MESSAGE_H
#include <types/pid.h>
typedef struct message_t
{
pid_t sender;
unsigned long code;
unsigned long args[6];
} message_t;
#endif

6
include/types/oid.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef QUARK_OID_H
#define QUARK_OID_H
typedef unsigned int oid_t;
#endif

View File

@@ -1,5 +1,5 @@
#ifndef _QUARK_PHYSADDR_H #ifndef QUARK_PHYSADDR_H
#define _QUARK_PHYSADDR_H #define QUARK_PHYSADDR_H
typedef unsigned long int physaddr_t; typedef unsigned long int physaddr_t;

6
include/types/pid.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef QUARK_PID_H
#define QUARK_PID_H
typedef unsigned int pid_t;
#endif

16
include/types/sigaction.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef QUARK_SIGACTION_H
#define QUARK_SIGACTION_H
#include "types/pid.h"
typedef unsigned long (*signal_handler_t)(void*, void*);
struct signal_action_t
{
pid_t pid;
signal_handler_t func_ptr;
void (*trampoline_ptr)();
void *userdata;
};
#endif

10
include/types/signalid.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef QUARK_SIGNALID_H
#define QUARK_SIGNALID_H
typedef enum signalid_t
{
SIG_KILL = 1,
SIG_STOP
} signalid_t;
#endif

View File

@@ -1,5 +1,5 @@
#ifndef _QUARK_ERROR_H #ifndef QUARK_ERROR_H
#define _QUARK_ERROR_H #define QUARK_ERROR_H
typedef enum typedef enum
{ {

View File

@@ -1,11 +1,12 @@
#ifndef _QUARK_SYSCALLARG_H #ifndef QUARK_SYSCALLARG_H
#define _QUARK_SYSCALLARG_H #define QUARK_SYSCALLARG_H
typedef union typedef union
{ {
long int signed_int; long int signed_int;
unsigned long int unsigned_int; unsigned long int unsigned_int;
void *ptr; void *ptr;
char *str;
} syscall_arg_t; } syscall_arg_t;
#endif #endif

View File

@@ -4,14 +4,25 @@
typedef enum typedef enum
{ {
SYSCALL_TEST = 1, SYSCALL_TEST = 1,
SYSCALL_MMAP, SYSCALL_MAP_ANON,
SYSCALL_MUNMAP, SYSCALL_UNMAP_ANON,
SYSCALL_YIELD, SYSCALL_MAP_PHYS,
SYSCALL_EXIT, SYSCALL_UNMAP_PHYS,
SYSCALL_SEND,
SYSCALL_RECEIVE,
SYSCALL_OPEN_PORT, SYSCALL_OPEN_PORT,
SYSCALL_CLOSE_PORT SYSCALL_CLOSE_PORT,
SYSCALL_SEND_PID,
SYSCALL_SEND_PORT,
SYSCALL_RECEIVE,
SYSCALL_CREATE_OBJECT,
SYSCALL_AQUIRE_OBJECT,
SYSCALL_RELEASE_OBJECT,
SYSCALL_GET_PID,
SYSCALL_CLONE,
SYSCALL_SIGNAL_ACTION,
SYSCALL_SIGNAL_RETURN,
SYSCALL_SIGNAL_RAISE,
SYSCALL_INTR_ACTION,
SYSCALL_INTR_RETURN
} syscall_id_t; } syscall_id_t;
#endif #endif

53
include/x86/mp.h Normal file
View File

@@ -0,0 +1,53 @@
#include <stdint.h>
#define MP_SIGNATURE (uint32_t) 0x5F504D5F
struct mp_floating_pointer
{
/**
* @brief The ASCII string "_MP_" indicates the presence of the pointer structure.
*
*/
uint32_t signature;
/**
* @brief The physical address of the MP configuration table. All zeroes if
* the table does not exist.
*
*/
uint32_t physical_addr;
/**
* @brief The length of the floating pointer structure in 16-byte units.
* Should contain the value '0x01'.
*
*/
uint8_t length;
/**
* @brief The version number of the MP specification supported.
*
*/
uint8_t spec_rev;
/**
* @brief A checksum of the pointer structure.
*
*/
uint8_t checksum;
/**
* @brief When zero, the configuration table is present. Otherwise, the
* value indicates a default configuration.
*
*/
uint8_t config_type;
uint8_t undef : 7;
/**
* @brief
*
*/
uint8_t imcrp : 1;
} __attribute__ ((packed));

View File

@@ -1,9 +1,12 @@
bin_PROGRAMS = quark-kernel bin_PROGRAMS = quark-kernel
quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c avltree.c queue.c math.c quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c avltree.c queue.c math.c process.c sharedobject.c addressspace.c
quark_kernel_LDADD = -lgcc -lmalloc quark_kernel_LDADD = -lgcc -lmalloc
quark_kernel_CFLAGS = -I$(top_srcdir)/include -I$(prefix)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb quark_kernel_CFLAGS = -I$(top_srcdir)/include -I$(prefix)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
quark_kernel_LDFLAGS = -L$(prefix)/lib -nostdlib quark_kernel_LDFLAGS = -L$(prefix)/lib -nostdlib
lib_LIBRARIES = libquark-syscall.a
libquark_syscall_a_CFLAGS = -I$(top_srcdir)/include -I$(prefix)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
if x86 if x86
quark_kernel_SOURCES += x86/paging.c \ quark_kernel_SOURCES += x86/paging.c \
x86/putc.c \ x86/putc.c \
@@ -18,6 +21,7 @@ quark_kernel_SOURCES += x86/paging.c \
x86/preempt.S \ x86/preempt.S \
x86/quark_x86.c \ x86/quark_x86.c \
x86/entry.S x86/entry.S
libquark_syscall_a_SOURCES = x86/do_syscall.S
quark_kernel_LDFLAGS += -T x86/linker.ld quark_kernel_LDFLAGS += -T x86/linker.ld
quark_kernel_DEPENDENCIES = x86/linker.ld quark_kernel_DEPENDENCIES = x86/linker.ld
endif endif

44
src/addressspace.c Normal file
View File

@@ -0,0 +1,44 @@
#include "addressspace.h"
#include "heap.h"
#include "mmgr.h"
address_space_t *address_space_construct()
{
address_space_t *as = kmalloc(sizeof(address_space_t));
if(as == NULL)
{
return NULL;
}
as->page_table = create_address_space();
if(as->page_table == ENOMEM)
{
kfree(as);
return NULL;
}
as->refcount = 0;
return as;
}
void address_space_switch(address_space_t *address_space)
{
paging_load_address_space(address_space->page_table);
}
address_space_t *address_space_release(address_space_t *address_space)
{
address_space->refcount--;
if(address_space->refcount <= 0)
{
address_space_destroy(address_space);
return NULL;
}
return address_space;
}
void address_space_destroy(address_space_t *address_space)
{
// TODO: Not implemented.
kfree(address_space);
}

View File

@@ -5,10 +5,13 @@
struct avltree_t *avl_new(int key, void *value) struct avltree_t *avl_new(int key, void *value)
{ {
struct avltree_t *new_tree = kmalloc(sizeof(struct avltree_t)); struct avltree_t *new_tree = kmalloc(sizeof(struct avltree_t));
if(new_tree != NULL)
{
new_tree->height = 1; new_tree->height = 1;
new_tree->left = new_tree->right = NULL; new_tree->left = new_tree->right = NULL;
new_tree->key = key; new_tree->key = key;
new_tree->value = value; new_tree->value = value;
}
return new_tree; return new_tree;
} }
@@ -196,3 +199,12 @@ void *avl_get(struct avltree_t *tree, int key)
} }
return NULL; return NULL;
} }
void *avl_clear(struct avltree_t *tree)
{
while(tree != NULL)
{
tree = avl_remove(tree, tree->key);
}
return tree;
}

View File

@@ -1,34 +1,24 @@
#include <stdbool.h> #include <stdbool.h>
#include <libmalloc/bitmap_alloc.h> #include <libmalloc/list_alloc.h>
#include "heap.h" #include "heap.h"
#include "mmgr.h" #include "mmgr.h"
#include "math.h" #include "math.h"
#include "stdio.h" #include "stdio.h"
#include "types/status.h" #include "types/status.h"
bitmap_heap_descriptor_t system_heap; list_alloc_descriptor_t system_heap;
static int map_block(void *location, unsigned long size)
{
for(int n = 0; n < size; n += page_size)
{
if(!(page_type(location + n) & PAGE_PRESENT))
{
int status = map_page(location + n, reserve_page(), PAGE_RW);
if(status)
{
return status;
}
}
}
return ENONE;
}
static int mmap_callback(void *location, unsigned long size) static int mmap_callback(void *location, unsigned long size)
{ {
size += (unsigned long)location % page_size;
location -= (unsigned long)location % page_size;
int status = ENONE; int status = ENONE;
for(unsigned long i = 0; i < size; i += page_size) for(unsigned long i = 0; i < size; i += page_size)
{ {
if((page_type(location + i) & PAGE_PRESENT))
{
continue;
}
physaddr_t frame = reserve_page(); physaddr_t frame = reserve_page();
if(frame == ENOMEM) if(frame == ENOMEM)
{ {
@@ -42,44 +32,48 @@ static int mmap_callback(void *location, unsigned long size)
return status; return status;
} }
int kminit(void *base, void* start, size_t heap_size, size_t block_size) int kminit(void *base, size_t heap_size)
{ {
static unsigned long heap_cache[16];
system_heap.bitmap = NULL;
system_heap.cache = heap_cache;
system_heap.cache_capacity = 16;
system_heap.block_bits = 4;
system_heap.block_size = block_size;
system_heap.offset = (unsigned long)base;
memory_region_t map_array[8]; memory_region_t map_array[8];
memory_map_t map = { memory_map_t map = {
.array = map_array, .array = map_array,
.capacity = 8, .capacity = 8,
.size = 0 .size = 0
}; };
memmap_insert_region(&map, 0, heap_size, M_AVAILABLE); memmap_insert_region(&map, base, heap_size, M_AVAILABLE);
memmap_insert_region(&map, 0, start - base, M_UNAVAILABLE); physaddr_t phys_addr = reserve_pages(heap_size);
return initialize_heap(&system_heap, &map, mmap_callback); if(phys_addr == ENOMEM)
{
return ENOMEM;
}
for(unsigned long off = 0; off < heap_size; off += page_size)
{
if((page_type(base + off) & PAGE_PRESENT))
{
continue;
}
else if(map_page(base + off, phys_addr + off, PAGE_RW))
{
return ENOMEM;
}
}
return list_alloc_init(&system_heap, &map);
} }
void *kmalloc(size_t size) void *kmalloc(size_t size)
{ {
unsigned long loc = reserve_region(&system_heap, size); void *p = list_alloc_reserve(&system_heap, size);
if(loc == NOMEM) if(p == NOMEM)
{
return NULL;
}
else if(map_block((void*)loc, size))
{ {
return NULL; return NULL;
} }
else else
{ {
return (void*)loc; return p;
} }
} }
void kfree(void *ptr) void kfree(void *ptr)
{ {
free_region(&system_heap, (unsigned long)ptr, 0); list_alloc_free(&system_heap, (unsigned long)ptr);
} }

View File

@@ -7,13 +7,58 @@
#include "string.h" #include "string.h"
#include "config.h" #include "config.h"
#include "system.h" #include "system.h"
#include "process.h"
#include "platform/interrupts.h" #include "platform/interrupts.h"
#include "platform/context.h" #include "platform/context.h"
#include "platform/putc.h" #include "platform/putc.h"
#include "types/status.h" #include "types/status.h"
#include "types/syscallid.h" #include "types/syscallid.h"
#include "types/sigaction.h"
struct kernel_t kernel; void *syscall_table[] = {
NULL,
(void*)test_syscall,
(void*)syscall_map_anon,
(void*)syscall_unmap_anon,
(void*)syscall_map_physical,
(void*)syscall_unmap_physical,
(void*)syscall_open_port,
(void*)syscall_close_port,
(void*)syscall_send_pid,
(void*)syscall_receive,
(void*)syscall_create_object,
(void*)syscall_aquire_object,
(void*)syscall_release_object,
(void*)syscall_get_pid,
(void*)syscall_clone,
(void*)syscall_signal_action,
(void*)syscall_signal_return,
(void*)syscall_signal_raise
};
struct port_t
{
unsigned long id;
pid_t owner_pid;
};
struct signal_context_t
{
unsigned long signal_id;
};
struct kernel_t
{
struct syscall_t syscall_table[MAX_SYSCALL_ID];
struct priority_queue_t priority_queue;
struct avltree_t *interrupt_handlers;
struct avltree_t *port_table;
struct avltree_t *object_table;
struct avltree_t *process_table;
struct process_t *active_process;
pid_t next_pid;
oid_t next_oid;
} kernel;
void kernel_initialize(struct boot_info_t *boot_info) void kernel_initialize(struct boot_info_t *boot_info)
{ {
@@ -38,48 +83,72 @@ void kernel_initialize(struct boot_info_t *boot_info)
memmap_insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE); memmap_insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE);
if(initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size)) if(initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size))
{ {
panic("Failed to initialize page allocator."); kernel_panic("Failed to initialize page allocator.");
} }
if(kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64)) if(kminit(page_map_end(), 0xFFC00000 - (size_t)page_map_end()))
{ {
panic("Failed to initialize heap."); kernel_panic("Failed to initialize heap.");
} }
kernel.active_process = NULL; kernel.active_process = NULL;
kernel.next_pid = 1; kernel.next_pid = 1;
kernel.next_oid = 1;
kernel.process_table = NULL; kernel.process_table = NULL;
kernel.port_table = NULL; kernel.port_table = NULL;
kernel.object_table = NULL;
if(construct_priority_queue(&kernel.priority_queue, 512) != ENONE) if(construct_priority_queue(&kernel.priority_queue, 512) != ENONE)
{ {
panic("Failed to construct priority queue."); kernel_panic("Failed to construct priority queue.");
} }
memset(kernel.syscall_table, 0, sizeof(struct syscall_t) * MAX_SYSCALL_ID); memset(kernel.syscall_table, 0, sizeof(struct syscall_t) * MAX_SYSCALL_ID);
set_syscall(SYSCALL_TEST, 1, 0, test_syscall); kernel_set_syscall(SYSCALL_TEST, 1, test_syscall);
set_syscall(SYSCALL_MMAP, 3, 0, mmap); kernel_set_syscall(SYSCALL_MAP_ANON, 3, syscall_map_anon);
set_syscall(SYSCALL_MUNMAP, 2, 0, munmap); kernel_set_syscall(SYSCALL_UNMAP_ANON, 1, syscall_unmap_anon);
set_syscall(SYSCALL_SEND, 3, 0, send); kernel_set_syscall(SYSCALL_MAP_PHYS, 3, syscall_map_physical);
set_syscall(SYSCALL_RECEIVE, 2, 0, receive); kernel_set_syscall(SYSCALL_UNMAP_PHYS, 2, syscall_unmap_physical);
set_syscall(SYSCALL_OPEN_PORT, 1, 0, open_port); kernel_set_syscall(SYSCALL_OPEN_PORT, 1, syscall_open_port);
set_syscall(SYSCALL_CLOSE_PORT, 1, 0, close_port); kernel_set_syscall(SYSCALL_CLOSE_PORT, 1, syscall_close_port);
kernel_set_syscall(SYSCALL_SEND_PID, 3, syscall_send_pid);
kernel_set_syscall(SYSCALL_SEND_PORT, 3, syscall_send_port);
kernel_set_syscall(SYSCALL_RECEIVE, 2, syscall_receive);
kernel_set_syscall(SYSCALL_CREATE_OBJECT, 3, syscall_create_object);
kernel_set_syscall(SYSCALL_AQUIRE_OBJECT, 2, syscall_aquire_object);
kernel_set_syscall(SYSCALL_RELEASE_OBJECT, 1, syscall_release_object);
kernel_set_syscall(SYSCALL_GET_PID, 0, syscall_get_pid);
kernel_set_syscall(SYSCALL_SIGNAL_ACTION, 3, syscall_signal_action);
kernel_set_syscall(SYSCALL_SIGNAL_RETURN, 0, syscall_signal_return);
kernel_set_syscall(SYSCALL_SIGNAL_RAISE, 2, syscall_signal_raise);
kernel_set_syscall(SYSCALL_INTR_ACTION, 3, syscall_intr_action);
kernel_set_syscall(SYSCALL_INTR_RETURN, 0, syscall_intr_return);
for(int i = 0; i < boot_info->module_count; i++) for(int i = 0; i < boot_info->module_count; i++)
{ {
if(kernel_load_module(&boot_info->modules[i]) != ENONE) if(kernel_load_module(&boot_info->modules[i]) != ENONE)
{ {
panic("Failed to load modules."); kernel_panic("Failed to load modules.");
} }
} }
if(initialize_interrupts() != ENONE) if(initialize_interrupts() != ENONE)
{ {
panic("Failed to initialize interrupts."); kernel_panic("Failed to initialize interrupts.");
} }
irq_enable(); irq_enable();
load_context(kernel_advance_scheduler()); load_context(kernel_advance_scheduler());
} }
error_t set_syscall(int id, int arg_count, int pid, void *func_ptr) process_t *kernel_get_process(pid_t pid)
{
return avl_get(kernel.process_table, pid);
}
process_t *kernel_get_active_process()
{
return kernel.active_process;
}
error_t kernel_set_syscall(int id, int arg_count, void *func_ptr)
{ {
if(id < 0 || id > MAX_SYSCALL_ID) if(id < 0 || id > MAX_SYSCALL_ID)
{ {
@@ -93,22 +162,17 @@ error_t set_syscall(int id, int arg_count, int pid, void *func_ptr)
{ {
return EINVALIDARG; return EINVALIDARG;
} }
else if(pid != 0 && avl_get(kernel.process_table, pid) == NULL)
{
return EDOESNTEXIST;
}
else if(func_ptr == NULL) else if(func_ptr == NULL)
{ {
return ENULLPTR; return ENULLPTR;
} }
kernel.syscall_table[id].defined = true; kernel.syscall_table[id].defined = true;
kernel.syscall_table[id].arg_count = arg_count; kernel.syscall_table[id].arg_count = arg_count;
kernel.syscall_table[id].process_id = pid;
kernel.syscall_table[id].func_ptr_0 = func_ptr; kernel.syscall_table[id].func_ptr_0 = func_ptr;
return ENONE; return ENONE;
} }
size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags) size_t kernel_do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags)
{ {
if(id < 0 || id > MAX_SYSCALL_ID) if(id < 0 || id > MAX_SYSCALL_ID)
{ {
@@ -118,18 +182,6 @@ size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, sysca
{ {
return ENOSYSCALL; return ENOSYSCALL;
} }
bool switched_address_space = false;
if(kernel.syscall_table[id].process_id > 0)
{
struct process_t *callee = avl_get(kernel.process_table, kernel.syscall_table[id].process_id);
if(callee == NULL)
{
kernel.syscall_table[id].defined = false;
return ENOSYSCALL;
}
paging_load_address_space(callee->page_table);
switched_address_space = true;
}
set_context_pc(kernel.active_process->ctx, pc); set_context_pc(kernel.active_process->ctx, pc);
set_context_stack(kernel.active_process->ctx, stack); set_context_stack(kernel.active_process->ctx, stack);
set_context_flags(kernel.active_process->ctx, flags); set_context_flags(kernel.active_process->ctx, flags);
@@ -149,59 +201,43 @@ size_t do_syscall(syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, sysca
result = kernel.syscall_table[id].func_ptr_3(arg1, arg2, arg3); result = kernel.syscall_table[id].func_ptr_3(arg1, arg2, arg3);
break; break;
} }
if(switched_address_space)
{
paging_load_address_space(kernel.active_process->page_table);
}
return result; return result;
} }
error_t kernel_load_module(struct module_t *module) error_t kernel_load_module(struct module_t *module)
{ {
physaddr_t module_address_space = create_address_space(); address_space_t *module_address_space = address_space_construct();
if(module_address_space == ENOMEM) { if(module_address_space == NULL) {
panic("failed to create address space for module: out of memory"); kernel_panic("failed to create address space for module: out of memory");
} }
paging_load_address_space(module_address_space); address_space_switch(module_address_space);
void *const load_base = (void*)0x80000000; void *const load_base = (void*)0x80000000;
size_t load_offset = 0; physaddr_t p = module->start & ~(page_size - 1);
for(physaddr_t p = module->start & ~(page_size - 1); p < module->end; p += page_size) map_region(load_base, p, module->end - p, PAGE_RW);
{
int status = map_page(load_base + load_offset, p, PAGE_RW);
switch(status)
{
case ENOMEM:
panic("ran out of memory while mapping module");
case EOUTOFBOUNDS:
panic("got out-of-bounds error while mapping module");
}
load_offset += page_size;
}
int status = load_program(load_base); int status = load_program(load_base);
switch(status) switch(status)
{ {
case ENOMEM: case ENOMEM:
panic("ran out of memory while reading ELF file"); kernel_panic("ran out of memory while reading ELF file");
case EOUTOFBOUNDS: case EOUTOFBOUNDS:
panic("got out-of-bounds error while reading ELF file"); kernel_panic("got out-of-bounds error while reading ELF file");
} }
void *module_entry = ((struct elf_file_header_t*)load_base)->entry; void *module_entry = ((struct elf_file_header_t*)load_base)->entry;
printf("loaded module with entry point %08x\n", (unsigned int)module_entry); printf("loaded module with entry point %08x\n", (unsigned int)module_entry);
load_offset = 0; size_t load_offset = 0;
for(physaddr_t p = module->start & ~(page_size - 1); p < module->end; p += page_size) for(physaddr_t p = module->start & ~(page_size - 1); p < module->end; p += page_size)
{ {
int status = unmap_page(load_base + load_offset); int status = unmap_page(load_base + load_offset);
switch(status) switch(status)
{ {
case ENOMEM: case ENOMEM:
panic("ran out of memory while unmapping module"); kernel_panic("ran out of memory while unmapping module");
case EOUTOFBOUNDS: case EOUTOFBOUNDS:
panic("got out-of-bounds error while unmapping module"); kernel_panic("got out-of-bounds error while unmapping module");
} }
load_offset += page_size; load_offset += page_size;
} }
if(kernel_spawn_process(module_entry, 1, current_address_space()) > 0) if(kernel_spawn_process(module_entry, 1, module_address_space) > 0)
{ {
return ENONE; return ENONE;
} }
@@ -211,7 +247,7 @@ error_t kernel_load_module(struct module_t *module)
} }
} }
unsigned long kernel_current_pid() pid_t kernel_current_pid()
{ {
if(kernel.active_process == NULL) if(kernel.active_process == NULL)
{ {
@@ -235,36 +271,22 @@ struct process_context_t *kernel_current_context()
} }
} }
unsigned long kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space) pid_t kernel_spawn_process(void *program_entry, int priority, address_space_t *address_space)
{ {
struct process_t *new_process = (struct process_t*) kmalloc(sizeof(struct process_t));
if(new_process == NULL)
{
return 0;
}
struct process_context_t *initial_context = kmalloc(sizeof(struct process_context_t));
if(initial_context == NULL)
{
return 0;
}
physaddr_t stack_page = reserve_page(); physaddr_t stack_page = reserve_page();
if(stack_page % page_size) if(stack_page % page_size)
{ {
return 0; return 0;
} }
map_page((void*)&_kernel_start - page_size, stack_page, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW); map_page((void*)&_kernel_start - page_size, stack_page, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW);
memset(initial_context, 0, sizeof(struct process_context_t)); struct process_t *new_process = process_construct(kernel.next_pid, &_kernel_start, program_entry, priority, address_space);
set_context_pc(initial_context, program_entry); if(new_process == NULL)
set_context_flags(initial_context, DEFAULT_FLAGS); {
set_context_stack(initial_context, &_kernel_start); free_page(stack_page);
new_process->priority = priority; return 0;
new_process->pid = kernel.next_pid; }
new_process->page_table = address_space;
new_process->state = PROCESS_ACTIVE;
new_process->message_buffer = NULL;
new_process->ctx = initial_context;
queue_construct(&new_process->sending_queue);
queue_construct(&new_process->message_queue);
kernel.process_table = avl_insert(kernel.process_table, new_process->pid, new_process); kernel.process_table = avl_insert(kernel.process_table, new_process->pid, new_process);
priorityqueue_insert(&kernel.priority_queue, new_process, new_process->priority); priorityqueue_insert(&kernel.priority_queue, new_process, new_process->priority);
kernel.next_pid++; kernel.next_pid++;
@@ -280,16 +302,24 @@ struct process_context_t *kernel_advance_scheduler()
kernel.active_process = priorityqueue_extract_min(&kernel.priority_queue); kernel.active_process = priorityqueue_extract_min(&kernel.priority_queue);
if(kernel.active_process != NULL) if(kernel.active_process != NULL)
{ {
paging_load_address_space(kernel.active_process->page_table); address_space_switch(kernel.active_process->address_space);
printf("entering process %08x cr3=%08x ctx=%08x sched=%i.\n", kernel.active_process->pid, kernel.active_process->page_table, kernel.active_process->ctx, kernel.priority_queue.size); printf("entering process %08x cr3=%08x ctx=%08x sched=%i.\n", kernel.active_process->pid, kernel.active_process->address_space->page_table, kernel.active_process->ctx, kernel.priority_queue.size);
return kernel.active_process->ctx; return kernel.active_process->ctx;
} }
panic("no processes available to enter!"); kernel_panic("no processes available to enter!");
} }
error_t kernel_terminate_process(size_t process_id) void kernel_schedule_process(process_t *process)
{ {
struct process_t *process = avl_get(kernel.process_table, process_id); if(priorityqueue_insert(&kernel.priority_queue, process, process->priority))
{
kernel_panic("Failed to schedule process!");
}
}
error_t kernel_terminate_process(pid_t pid)
{
struct process_t *process = avl_get(kernel.process_table, pid);
if(process == NULL) if(process == NULL)
{ {
return EDOESNTEXIST; return EDOESNTEXIST;
@@ -298,18 +328,12 @@ error_t kernel_terminate_process(size_t process_id)
{ {
kernel.active_process = NULL; kernel.active_process = NULL;
} }
kernel.process_table = avl_remove(kernel.process_table, process_id); kernel.process_table = avl_remove(kernel.process_table, pid);
priorityqueue_remove(&kernel.priority_queue, process); priorityqueue_remove(&kernel.priority_queue, process);
for(struct message_t *msg = queue_get_next(&process->message_queue); msg != NULL; msg = queue_get_next(&process->message_queue)) for(struct message_t *msg = queue_get_next(&process->message_queue); msg != NULL; msg = queue_get_next(&process->message_queue))
{ {
kfree(msg); kfree(msg);
} }
for(struct process_t *sender = queue_get_next(&process->sending_queue); sender != NULL; sender = queue_get_next(&process->sending_queue))
{
sender->state = PROCESS_ACTIVE;
set_context_return(sender->ctx, EEXITED);
priorityqueue_insert(&kernel.priority_queue, sender, sender->priority);
}
kfree(process->ctx); kfree(process->ctx);
kfree(process); kfree(process);
return ENONE; return ENONE;
@@ -359,7 +383,7 @@ error_t kernel_remove_port(unsigned long id)
return ENONE; return ENONE;
} }
unsigned long kernel_get_port_owner(unsigned long id) pid_t kernel_get_port_owner(unsigned long id)
{ {
struct port_t *port = avl_get(kernel.port_table, id); struct port_t *port = avl_get(kernel.port_table, id);
if(port == NULL) if(port == NULL)
@@ -372,56 +396,7 @@ unsigned long kernel_get_port_owner(unsigned long id)
} }
} }
error_t kernel_send_message(unsigned long recipient, struct message_t *message) int kernel_receive_message(struct message_t *buffer, int flags)
{
struct process_t *dest = avl_get(kernel.process_table, recipient);
if(dest == NULL)
{
return EDOESNTEXIST;
}
else if(dest->message_buffer != NULL && dest->state == PROCESS_REQUESTING)
{
printf("Sending message directly from %i to %i\n", kernel.active_process->pid, dest->pid);
struct message_t kernel_buffer;
memcpy(&kernel_buffer, message, sizeof(struct message_t));
kernel_buffer.sender = kernel.active_process->pid;
paging_load_address_space(dest->page_table);
memcpy(dest->message_buffer, &kernel_buffer, sizeof(struct message_t));
paging_load_address_space(kernel.active_process->page_table);
dest->message_buffer = NULL;
dest->state = PROCESS_ACTIVE;
set_context_return(dest->ctx, ENONE);
priorityqueue_insert(&kernel.priority_queue, dest, dest->priority);
return ENONE;
}
else
{
return EBUSY;
}
}
error_t kernel_queue_message(unsigned long recipient, struct message_t *message)
{
struct process_t *dest = avl_get(kernel.process_table, recipient);
if(dest != NULL)
{
printf("Queueing message from %i to %i\n", kernel.active_process->pid, dest->pid);
struct message_t *queued_msg = kmalloc(sizeof(struct message_t));
if(queued_msg == NULL)
{
return ENOMEM;
}
memcpy(queued_msg, message, sizeof(struct message_t));
queue_insert(&dest->message_queue, queued_msg);
return ENONE;
}
else
{
return EDOESNTEXIST;
}
}
int receive_message(struct message_t *buffer, int flags)
{ {
if(kernel.active_process->message_queue.count > 0) if(kernel.active_process->message_queue.count > 0)
{ {
@@ -484,7 +459,7 @@ error_t kernel_execute_interrupt_handler(unsigned long interrupt)
return EDOESNTEXIST; return EDOESNTEXIST;
} }
paging_load_address_space(process->page_table); address_space_switch(process->address_space);
struct signal_context_t siginfo = { struct signal_context_t siginfo = {
.signal_id = interrupt .signal_id = interrupt
@@ -499,7 +474,7 @@ error_t kernel_execute_interrupt_handler(unsigned long interrupt)
priorityqueue_insert(&kernel.priority_queue, process, process->priority); priorityqueue_insert(&kernel.priority_queue, process, process->priority);
} }
paging_load_address_space(kernel.active_process->page_table); address_space_switch(kernel.active_process->address_space);
return ENONE; return ENONE;
} }
@@ -511,13 +486,52 @@ error_t kernel_signal_return()
context_stack_pop_struct(kernel.active_process->ctx, kernel.active_process->ctx, sizeof(*kernel.active_process->ctx)); context_stack_pop_struct(kernel.active_process->ctx, kernel.active_process->ctx, sizeof(*kernel.active_process->ctx));
if(kernel.active_process->state == PROCESS_REQUESTING) if(kernel.active_process->state == PROCESS_REQUESTING)
{ {
receive_message(kernel.active_process->message_buffer, 0); kernel_receive_message(kernel.active_process->message_buffer, 0);
load_context(kernel.active_process->ctx); load_context(kernel.active_process->ctx);
} }
return ENONE; return ENONE;
} }
void panic(const char *message) error_t kernel_create_object(size_t size, unsigned long flags, oid_t *id)
{
physaddr_t phys_addr = reserve_pages(size);
if(phys_addr == ENOMEM)
{
return ENOMEM;
}
struct shared_object_t *obj = kmalloc(sizeof(struct shared_object_t));
if(obj == NULL)
{
free_pages(phys_addr);
return ENOMEM;
}
obj->phys_addr = phys_addr;
obj->size = size;
obj->access_flags = flags;
obj->refcount = 0;
kernel.object_table = avl_insert(kernel.object_table, kernel.next_oid, obj);
*id = kernel.next_oid;
kernel.next_oid++;
if(kernel.next_oid <= 0)
{
kernel.next_oid = 1;
}
return ENONE;
}
error_t kernel_attach_object(oid_t id, void *virt_addr)
{
}
error_t kernel_release_object(oid_t id)
{
}
void kernel_panic(const char *message)
{ {
printf("panic: %s", message); printf("panic: %s", message);
asm("cli"); asm("cli");

View File

@@ -4,17 +4,18 @@
#include "platform/paging.h" #include "platform/paging.h"
#include "types/status.h" #include "types/status.h"
#include "stdio.h" #include "stdio.h"
#include <libmalloc/bitmap_alloc.h> #include <libmalloc/buddy_alloc.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#define MAX_CACHE_SIZE 32 #define AVAIL_LIST_SIZE 20
bitmap_heap_descriptor_t page_map; buddy_descriptor_t page_alloc;
physaddr_t reserve_pages(size_t size) physaddr_t reserve_pages(size_t size)
{ {
unsigned long location = reserve_region(&page_map, size); unsigned long location = buddy_reserve(&page_alloc, size);
printf("Reserved >=%08x pages at %08x\n", size, location);
if(location != NOMEM) if(location != NOMEM)
{ {
return location; return location;
@@ -25,63 +26,61 @@ physaddr_t reserve_pages(size_t size)
} }
} }
int free_pages(physaddr_t location, size_t size) size_t free_pages(physaddr_t location)
{ {
free_region(&page_map, location, size); return buddy_free(&page_alloc, location);
return ENONE;
} }
physaddr_t reserve_page() physaddr_t reserve_page()
{ {
unsigned long loc = reserve_region(&page_map, page_size); unsigned long loc = buddy_reserve(&page_alloc, page_size);
printf("Reserved %08x\n", loc);
if(loc == NOMEM) if(loc == NOMEM)
{ {
return ENOMEM; return ENOMEM;
} }
else else
{ {
printf("Reserved %08x\n", loc);
return loc; return loc;
} }
} }
int free_page(physaddr_t location) int free_page(physaddr_t location)
{ {
free_region(&page_map, location, page_size); buddy_free_size(&page_alloc, location, page_size);
return ENONE; return ENONE;
} }
size_t free_page_count() size_t free_page_count()
{ {
return page_map.free_block_count; return page_alloc.free_block_count;
} }
void *page_map_base() void *page_map_base()
{ {
return (void*)page_map.bitmap; return (void*)page_alloc.block_map;
} }
void *page_map_end() void *page_map_end()
{ {
return (void*)page_map.bitmap + page_map.bitmap_size; return (void*)page_alloc.block_map + page_alloc.block_map_size;
} }
error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, unsigned long block_size) error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, unsigned long block_size)
{ {
static unsigned long page_map_cache[MAX_CACHE_SIZE]; static unsigned long avail_list[AVAIL_LIST_SIZE];
// Round memory_size up to nearest power of 2 // Round memory_size up to nearest power of 2
memory_size = 1 << llog2(memory_size); memory_size = 1 << llog2(memory_size);
page_map.block_size = block_size; page_alloc.avail = avail_list;
page_map.block_bits = 1; page_alloc.block_map = (buddy_block_t*) base;
page_map.offset = 0; page_alloc.block_size = block_size;
page_map.cache = page_map_cache; page_alloc.mmap = NULL;
page_map.cache_capacity = MAX_CACHE_SIZE; page_alloc.offset = 0;
page_map.bitmap = (unsigned long*) base;
/* Allocate pages for bitmap */ /* Allocate pages for bitmap */
int pages_mapped = 0; int pages_mapped = 0;
int pages_needed = (bitmap_size(map, block_size, 1) + page_size - 1) / page_size; int pages_needed = (buddy_map_size(map, block_size) + page_size - 1) / page_size;
for(int i = 0; i < map->size && (pages_mapped < pages_needed); i++) for(int i = 0; i < map->size && (pages_mapped < pages_needed); i++)
{ {
if(map->array[i].type != M_AVAILABLE) if(map->array[i].type != M_AVAILABLE)
@@ -92,7 +91,7 @@ error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, u
physaddr_t region_end = map->array[i].location + map->array[i].size; physaddr_t region_end = map->array[i].location + map->array[i].size;
while(location + page_size <= region_end && (pages_mapped < pages_needed)) while(location + page_size <= region_end && (pages_mapped < pages_needed))
{ {
void *page = (void*)page_map.bitmap + pages_mapped * page_size; void *page = (void*)page_alloc.block_map + pages_mapped * page_size;
for(int level = 0; level < page_table_levels; level++) for(int level = 0; level < page_table_levels; level++)
{ {
if(!(get_pte_type(page, level) & PAGE_PRESENT)) if(!(get_pte_type(page, level) & PAGE_PRESENT))
@@ -118,7 +117,7 @@ error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, u
} }
} }
if(initialize_heap(&page_map, map, NULL)) if(buddy_alloc_init(&page_alloc, map))
{ {
return ENOMEM; return ENOMEM;
} }
@@ -151,26 +150,35 @@ physaddr_t current_address_space()
return paging_current_address_space(); return paging_current_address_space();
} }
int map_page(void *page, physaddr_t frame, int flags) error_t map_region(void *page, physaddr_t frame, size_t size, int flags)
{
if(frame % page_size != 0)
{
return EINVALIDARG;
}
for(size_t p = 0; p < size; p += page_size)
{
set_pte(page + p, page_table_levels - 1, PAGE_PRESENT | flags, frame + p);
}
return ENONE;
}
physaddr_t unmap_region(void *page, size_t size)
{
physaddr_t frame = get_pte_address(page, page_table_levels - 1);
for(size_t p = 0; p < size; p += page_size)
{
set_pte(page + p, page_table_levels - 1, 0, 0);
}
return frame;
}
error_t map_page(void *page, physaddr_t frame, int flags)
{ {
if (frame % page_size != 0) if (frame % page_size != 0)
{ {
return EINVALIDARG; return EINVALIDARG;
} }
for(int level = 0; level < page_table_levels - 1; level++)
{
int present = get_pte_type(page, level) & PAGE_PRESENT;
if(present == 0)
{
physaddr_t new_table = reserve_page();
if(new_table == ENOMEM)
{
return ENOMEM;
}
set_pte(page, level, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW, new_table);
wipe_page_table(page, level + 1);
}
}
set_pte(page, page_table_levels - 1, PAGE_PRESENT | flags, frame); set_pte(page, page_table_levels - 1, PAGE_PRESENT | flags, frame);
return ENONE; return ENONE;
} }

70
src/process.c Normal file
View File

@@ -0,0 +1,70 @@
#include "process.h"
#include "heap.h"
#include "mmgr.h"
#include "platform/context.h"
#include "string.h"
#include "avltree.h"
process_t *process_construct(pid_t pid, void *entry, void *stack, int priority, address_space_t *address_space)
{
process_t *new_process = (process_t*) kmalloc(sizeof(process_t));
if(new_process == NULL)
{
return NULL;
}
struct process_context_t *initial_context = kmalloc(sizeof(struct process_context_t));
if(initial_context == NULL)
{
return NULL;
}
if(address_space == NULL
&& (address_space = address_space_construct()) == NULL)
{
return NULL;
}
context_construct(initial_context);
set_context_pc(initial_context, entry);
set_context_stack(initial_context, stack);
new_process->priority = priority;
new_process->pid = pid;
new_process->address_space = address_space;
new_process->address_space->refcount++;
new_process->shared_objects = NULL;
new_process->state = PROCESS_ACTIVE;
new_process->message_buffer = NULL;
new_process->ctx = initial_context;
queue_construct(&new_process->message_queue);
return new_process;
}
error_t process_queue_message(process_t *process, message_t *msg)
{
return queue_insert(&process->message_queue, msg);
}
error_t process_receive_message(process_t *process, message_t *buffer)
{
if(process->message_queue.count == 0)
{
return EDOESNTEXIST;
}
message_t *queued_message = queue_get_next(&process->message_queue);
memcpy(buffer, queued_message, sizeof(message_t));
kfree(queued_message);
return ENONE;
}
error_t process_store_object(process_t *process, int id, void *location)
{
process->shared_objects = avl_insert(process->shared_objects, id, location);
return ENONE;
}
void *process_get_object(process_t *process, int id)
{
return avl_get(process->shared_objects, id);
}

35
src/sharedobject.c Normal file
View File

@@ -0,0 +1,35 @@
#include "sharedobject.h"
#include "avltree.h"
#include "mmgr.h"
#include "heap.h"
struct shared_object *create_shared_object(size_t size, unsigned long flags)
{
physaddr_t phys_addr = reserve_pages(size);
if(phys_addr == NULL)
{
return NULL;
}
struct shared_object_t *obj = kmalloc(sizeof(struct shared_object_t));
if(obj == NULL)
{
free_pages(phys_addr);
return NULL;
}
obj->phys_addr = phys_addr;
obj->size = size;
obj->access_flags = flags;
obj->refcount = 0;
return obj;
}
void destroy_shared_object(struct shared_object_t *obj)
{
if(obj != NULL)
{
free_pages(obj->phys_addr);
kfree(obj);
}
}

View File

@@ -18,7 +18,18 @@ enum format_flags_t
}; };
char *itoa(unsigned int n, unsigned int base, unsigned int width) typedef enum
{
LOG_PANIC,
LOG_ERROR,
LOG_WARNING,
LOG_INFO,
LOG_DEBUG
} log_levels_t;
static char *itoa(unsigned int n, unsigned int base, unsigned int width)
{ {
if (base < 2 || base > 16) if (base < 2 || base > 16)
{ {

View File

@@ -2,16 +2,23 @@
#include "kernel.h" #include "kernel.h"
#include "mmgr.h" #include "mmgr.h"
#include "stdio.h" #include "stdio.h"
#include "process.h"
#include "sharedobject.h"
#include "system.h"
#include "heap.h"
#include "platform/context.h" #include "platform/context.h"
#include "types/status.h" #include "types/status.h"
size_t test_syscall(syscall_arg_t str) int test_syscall(syscall_arg_t str)
{ {
if(str.ptr != NULL)
{
printf("%s", (char*)str.ptr); printf("%s", (char*)str.ptr);
}
return 17; return 17;
} }
size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t arg_flags) int syscall_map_anon(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t arg_flags)
{ {
unsigned long location = arg_location.unsigned_int; unsigned long location = arg_location.unsigned_int;
unsigned long length = arg_length.unsigned_int; unsigned long length = arg_length.unsigned_int;
@@ -24,6 +31,7 @@ size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t
{ {
return ENULLPTR; return ENULLPTR;
} }
for(size_t i = 0; i < length; i += page_size) for(size_t i = 0; i < length; i += page_size)
{ {
if(page_type((void*)(location + i)) & PAGE_PRESENT) if(page_type((void*)(location + i)) & PAGE_PRESENT)
@@ -31,107 +39,226 @@ size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t
return EEXISTS; return EEXISTS;
} }
} }
size_t n = 0;
physaddr_t frame = reserve_pages(length);
if(frame % page_size != 0)
{
return frame % page_size;
}
error_t status = map_region(location, frame, length, PAGE_USERMODE | PAGE_RW);
if(status != ENONE)
{
kernel_panic("Unexpected failure to map virtual memory.");
}
return ENONE;
}
int syscall_unmap_anon(syscall_arg_t arg_location)
{
void *addr = arg_location.ptr;
if((size_t)addr % page_size != 0)
{
return EINVALIDARG;
}
else if(addr == NULL)
{
return ENULLPTR;
}
else if(addr >= _kernel_start)
{
return EPERM;
}
unmap_region(arg_location.ptr, free_pages(physical_address(arg_location.ptr)));
return ENONE;
}
int syscall_map_physical(syscall_arg_t arg_addr, syscall_arg_t arg_phys_addr, syscall_arg_t arg_length)
{
void *addr = arg_addr.ptr;
physaddr_t frame = arg_phys_addr.unsigned_int;
unsigned long length = arg_length.unsigned_int;
int status = ENONE; int status = ENONE;
while(n < length && status == ENONE) for(unsigned long offset = 0; offset < length; offset += page_size)
{ {
physaddr_t frame = reserve_page(); status = map_page(addr + offset, frame + offset, PAGE_USERMODE | PAGE_RW);
status = frame % page_size; if(status)
if(status == ENONE)
{
status = map_page((void*)(location + n), frame, PAGE_USERMODE | PAGE_RW);
if(status != ENONE && free_page(frame) != ENONE)
{
panic("critical error reached during mmap.");
}
n += page_size;
}
else
{ {
break; break;
} }
} }
if(status != ENONE && munmap(arg_location, arg_length) != ENONE)
{
panic("critical error reached during mmap.");
}
return status; return status;
} }
size_t munmap(syscall_arg_t arg_location, syscall_arg_t arg_length) int syscall_unmap_physical(syscall_arg_t arg_addr, syscall_arg_t arg_length)
{ {
unsigned long location = arg_location.unsigned_int; void *addr = arg_addr.ptr;
unsigned long length = arg_length.unsigned_int; unsigned long length = arg_length.unsigned_int;
if(location % page_size != 0 || length % page_size != 0)
{
return EINVALIDARG;
}
else if(location == 0)
{
return ENULLPTR;
}
size_t n = 0;
int status = ENONE; int status = ENONE;
while(n < length && status == ENONE) for(unsigned long offset = 0; offset < length; offset += page_size)
{ {
int type = page_type((void*)(location + n)); status = unmap_page(addr + offset);
physaddr_t frame; if(status != ENONE)
if(type & PAGE_PRESENT)
{ {
frame = unmap_page((void*)(location + n)); break;
if(type & PAGE_ANON)
{
status = free_page(frame);
}
} }
} }
return status; return status;
} }
size_t terminate_self() int syscall_open_port(syscall_arg_t id)
{
return kernel_terminate_process(kernel_current_pid());
}
size_t send(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags)
{
unsigned long op_type = flags.unsigned_int & IO_OP;
unsigned long dest_type = flags.unsigned_int & IO_RECIPIENT_TYPE;
if((flags.unsigned_int & ~(IO_OP | IO_RECIPIENT_TYPE)) != 0 || dest_type >= IO_MAILBOX)
{
printf("Invalid flags on send()\n");
return EINVALIDARG;
}
if(dest_type == IO_PORT)
{
recipient.unsigned_int = kernel_get_port_owner(recipient.unsigned_int);
if(recipient.unsigned_int == 0)
{
return EDOESNTEXIST;
}
}
error_t status = kernel_send_message(recipient.unsigned_int, message.ptr);
if(status == EBUSY/* && op_type == IO_ASYNC*/)
{
return kernel_queue_message(recipient.unsigned_int, message.ptr);
}
else
{
return status;
}
}
size_t receive(syscall_arg_t buffer, syscall_arg_t flags)
{
return receive_message(buffer.ptr, flags.unsigned_int);
}
size_t open_port(syscall_arg_t id)
{ {
return kernel_create_port(id.unsigned_int); return kernel_create_port(id.unsigned_int);
} }
size_t close_port(syscall_arg_t id) int syscall_close_port(syscall_arg_t id)
{ {
return kernel_remove_port(id.unsigned_int); return kernel_remove_port(id.unsigned_int);
} }
int syscall_send_pid(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags)
{
if(message.ptr == NULL)
{
return ENULLPTR;
}
process_t *process = kernel_get_process(recipient.unsigned_int);
if(process == NULL)
{
return EEXITED;
}
message_t *buffer = kmalloc(sizeof(message_t));
if(buffer == NULL)
{
return ENOMEM;
}
memcpy(buffer, message.ptr, sizeof(message_t));
buffer->sender = kernel_current_pid();
address_space_switch(process->address_space);
int status = process_queue_message(process, buffer);
address_space_switch(kernel_get_active_process()->address_space);
if(status == ENOMEM)
{
kfree(buffer);
kernel_terminate_process(recipient.unsigned_int);
return EEXITED;
}
else
{
return ENONE;
}
}
int syscall_send_port(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags)
{
if(message.ptr == NULL)
{
return ENULLPTR;
}
pid_t recipient_pid = kernel_get_port_owner(recipient.unsigned_int);
if(recipient_pid == 0)
{
return EDOESNTEXIST;
}
process_t *process = kernel_get_process(recipient_pid);
if(process == NULL)
{
return EEXITED;
}
message_t *buffer = kmalloc(sizeof(message_t));
if(buffer == NULL)
{
return ENOMEM;
}
memcpy(buffer, message.ptr, sizeof(message_t));
buffer->sender = kernel_current_pid();
address_space_switch(process->address_space);
int status = process_queue_message(process, buffer);
address_space_switch(kernel_get_active_process()->address_space);
if(status == ENOMEM)
{
kfree(buffer);
kernel_terminate_process(recipient_pid);
return EEXITED;
}
else
{
return ENONE;
}
}
int syscall_receive(syscall_arg_t buffer, syscall_arg_t flags)
{
return kernel_receive_message(buffer.ptr, flags.unsigned_int);
}
int syscall_create_object(void *location, size_t size, int flags)
{
shared_object_t *object = create_shared_object(size, flags);
if(object == NULL)
{
return ENOMEM;
}
if(map_region(location, object->phys_addr, object->size, object->access_flags))
{
destroy_shared_object(object);
return ENOMEM;
}
object->refcount++;
process_store_object(kernel_get_active_process(), 0, location);
return ENONE;
}
int syscall_aquire_object(oid_t id, void *location)
{
return ENOSYSCALL;
}
int syscall_release_object(oid_t id)
{
return ENOSYSCALL;
}
int syscall_get_pid()
{
return kernel_current_pid();
}
int syscall_clone(void (*entry)(void*), void *arg, void *stack, int flags)
{
return ENOSYSCALL;
}
int syscall_signal_action(int id, struct signal_action_t *action, int flags)
{
return ENOSYSCALL;
}
int syscall_signal_return()
{
return ENOSYSCALL;
}
int syscall_signal_raise(pid_t pid, int sigid)
{
return ENOSYSCALL;
}
int syscall_intr_action(int id, struct signal_action_t *action, int flags)
{
return ENOSYSCALL;
}
int syscall_intr_return()
{
return ENOSYSCALL;
}

View File

@@ -6,6 +6,12 @@
#include "system.h" #include "system.h"
#include <stdarg.h> #include <stdarg.h>
void context_construct(struct process_context_t *context)
{
memset(context, 0, sizeof(struct process_context_t));
context->flags = DEFAULT_FLAGS;
}
void *get_context_pc(struct process_context_t *context) void *get_context_pc(struct process_context_t *context)
{ {
return (void*) context->eip; return (void*) context->eip;

19
src/x86/do_syscall.S Normal file
View File

@@ -0,0 +1,19 @@
.global _do_syscall
.type _do_syscall, @function
_do_syscall:
push %ebp
mov %esp, %ebp
push %esi
push %edi
push %ebx
mov 0x08(%ebp), %eax
mov 0x0C(%ebp), %ebx
mov 0x10(%ebp), %ecx
mov 0x14(%ebp), %edx
int $0x80
pop %ebx
pop %edi
pop %esi
pop %ebp
ret
.size _do_syscall, . - _do_syscall

View File

@@ -2,6 +2,9 @@
#include "stdio.h" #include "stdio.h"
#include "x86/apic.h" #include "x86/apic.h"
#include "platform/interrupts.h" #include "platform/interrupts.h"
#include "mmgr.h"
#include "string.h"
#include "kernel.h"
#include <stdint.h> #include <stdint.h>
@@ -14,6 +17,18 @@ struct interrupt_frame_t
uint32_t ss; uint32_t ss;
}; };
typedef struct page_fault_code_t
{
uint32_t present : 1;
uint32_t write : 1;
uint32_t usermode : 1;
uint32_t reserved : 1;
uint32_t inst_fetch : 1;
uint32_t pk : 1;
uint32_t shadow_stack : 1;
uint32_t padding : 25;
} page_fault_code_t;
void isr_generic(struct interrupt_frame_t *frame) void isr_generic(struct interrupt_frame_t *frame)
{ {
printf("Generic interrupt.\n"); printf("Generic interrupt.\n");
@@ -46,6 +61,7 @@ void isr_gp_fault(struct interrupt_frame_t *frame, unsigned int error)
void isr_page_fault(struct interrupt_frame_t *frame, unsigned int error) void isr_page_fault(struct interrupt_frame_t *frame, unsigned int error)
{ {
page_fault_code_t *code = &error;
uint32_t addr; uint32_t addr;
asm("mov %%cr2, %0" asm("mov %%cr2, %0"
: "=r"(addr)); : "=r"(addr));
@@ -55,8 +71,28 @@ void isr_page_fault(struct interrupt_frame_t *frame, unsigned int error)
"mov %%ax, %%fs; " "mov %%ax, %%fs; "
"mov %%ax, %%gs; " :: "mov %%ax, %%gs; " ::
: "ax"); : "ax");
if(code->usermode == 0
&& code->present == 0
&& addr >= 0xFFC00000
&& addr < 0xFFFFF000)
{
printf("Allocating new page table %08x within fault handler.\n", addr);
physaddr_t new_table = reserve_page();
if(new_table == ENOMEM)
{
kernel_panic("Out of memory while allocating page table.\n");
}
set_pte((void*)addr, 1, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW, new_table);
asm volatile("mov %%cr3, %%eax;"
"mov %%eax, %%cr3" ::
: "eax", "memory");
memset((void*)(addr & ~0xFFF), 0, page_size);
}
else
{
printf("Exception: Page fault, code %08x, linear address %08x\n", error, addr); printf("Exception: Page fault, code %08x, linear address %08x\n", error, addr);
asm("hlt"); kernel_panic("Unhandled page fault.\n");
}
} }
void isr_double_fault(struct interrupt_frame_t *frame, unsigned int error) void isr_double_fault(struct interrupt_frame_t *frame, unsigned int error)

View File

@@ -2,7 +2,7 @@ ENTRY(_start)
SECTIONS SECTIONS
{ {
. = 0xFF400000; . = 0xFF000000;
VIRTUAL_BASE = .; VIRTUAL_BASE = .;
PHYSICAL_BASE = 0x100000; PHYSICAL_BASE = 0x100000;
BASE_DIFF = VIRTUAL_BASE - PHYSICAL_BASE; BASE_DIFF = VIRTUAL_BASE - PHYSICAL_BASE;

View File

@@ -32,7 +32,7 @@ struct page_table_entry_t *page_tables = (struct page_table_entry_t *)0xFFC00000
struct page_table_entry_t *page_directory = (struct page_table_entry_t *)0xFFFFF000; struct page_table_entry_t *page_directory = (struct page_table_entry_t *)0xFFFFF000;
struct page_table_entry_t *get_pte_pointer(void *page, int level) struct page_table_entry_t *get_pte_pointer_chk(void *page, int level)
{ {
unsigned int directory_index = (unsigned int)page >> 22; unsigned int directory_index = (unsigned int)page >> 22;
struct page_table_entry_t *entry = NULL; struct page_table_entry_t *entry = NULL;
@@ -48,6 +48,22 @@ struct page_table_entry_t *get_pte_pointer(void *page, int level)
return entry; return entry;
} }
struct page_table_entry_t *get_pte_pointer(void *page, int level)
{
if(level == 0)
{
return &page_directory[(unsigned int)page >> 22];
}
else if(level == 1)
{
return &page_tables[(unsigned int)page >> page_bits];
}
else
{
return NULL;
}
}
int start_paging(void *linear_addr, physaddr_t start, physaddr_t end, uint32_t *directory, uint32_t *table, uint32_t *identity_table) int start_paging(void *linear_addr, physaddr_t start, physaddr_t end, uint32_t *directory, uint32_t *table, uint32_t *identity_table)
{ {
unsigned int directory_index = (unsigned int) linear_addr >> 22; unsigned int directory_index = (unsigned int) linear_addr >> 22;
@@ -114,7 +130,7 @@ void paging_load_address_space(physaddr_t table)
int get_pte_type(void *page, int level) int get_pte_type(void *page, int level)
{ {
struct page_table_entry_t *entry = get_pte_pointer(page, level); struct page_table_entry_t *entry = get_pte_pointer_chk(page, level);
if(entry != NULL) if(entry != NULL)
{ {
int flags = (entry->present ? PAGE_PRESENT | PAGE_EXECUTABLE : 0) int flags = (entry->present ? PAGE_PRESENT | PAGE_EXECUTABLE : 0)
@@ -146,7 +162,7 @@ int set_pte_type(void *page, int level, int flags)
physaddr_t get_pte_address(void *page, int level) physaddr_t get_pte_address(void *page, int level)
{ {
struct page_table_entry_t *entry = get_pte_pointer(page, level); struct page_table_entry_t *entry = get_pte_pointer_chk(page, level);
if(entry != NULL) if(entry != NULL)
{ {
return entry->physical_address << page_bits | ((size_t)page & 0xFFF); return entry->physical_address << page_bits | ((size_t)page & 0xFFF);

View File

@@ -20,7 +20,7 @@ isr_syscall:
mov %ax, %es mov %ax, %es
mov %ax, %fs mov %ax, %fs
mov %ax, %gs mov %ax, %gs
call do_syscall call kernel_do_syscall
mov %ebp, %esp mov %ebp, %esp
mov $0x23, %cx mov $0x23, %cx
mov %cx, %ds mov %cx, %ds