Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
env:
XLINGS_VERSION: v2026.10.26.1
XLINGS_NON_INTERACTIVE: '1'
MCPP_VERSION: 2026.11.16.1
MCPP_VERSION: 2026.11.22.1
LLVM: llvm@23.1.3

jobs:
Expand Down Expand Up @@ -102,7 +102,7 @@ jobs:
# ones: the stub and the runtime agree on the hand-over's version.
- name: This repository's openkal-dos and okdos
run: |
for p in examples/hello examples/files tests/fault tests/stack tests/stack-small; do
for p in examples/hello examples/files tests/starts tests/fault tests/stack tests/stack-small; do
printf '\n[target.i686-dos-musl.dependencies]\nmcpplibs.openkal-dos = { path = "%s", override = true }\nmcpplibs.openkal-dos-tools = { path = "%s/tools", override = true }\n' \
"$GITHUB_WORKSPACE" "$GITHUB_WORKSPACE" >> "$p/mcpp.toml"
done
Expand All @@ -126,6 +126,17 @@ jobs:
tr -d '\r' < out.txt | grep -qx 'read 3: appended'
tr -d '\r' < out.txt | grep -qx 'resized to 40'
tr -d '\r' < out.txt | grep -qx 'removed: true'
# A PROGRAM STARTS PROGRAMS (0.3.0): posix_spawn starts the program again by EXEC, with a
# vector, a variable, a stream placed on a file and on NUL; a copy finds no handle of its
# starter's; an absent name and a tail beyond DOS's 126 bytes are refused.
- name: starts (status 42)
working-directory: tests/starts
run: |
set +e; mcpp run --target i686-dos-musl > out.txt; rc=$?; set -e
cat out.txt
if tr -d '\r' < out.txt | grep -q '^ FAIL'; then echo "::error::a check of tests/starts failed"; exit 1; fi
[ "$rc" = 42 ] || { echo "::error::starts ended with $rc, not 42"; exit 1; }
tr -d '\r' < out.txt | grep -qx 'all held'
- name: a page fault (status 139)
working-directory: tests/fault
run: |
Expand Down
85 changes: 79 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ interfaces, its hand-over and its stub may change in any release.

A program names the target and nothing else. The row `i686-dos-musl`, its
compile and link conventions, its seal and its runner are the openkal
platform's ([openkal-platform][plat] 0.28.0): the same C library and C++
platform's ([openkal-platform][plat], from 0.28.0; 0.32.0 holds this release): the same C library and C++
run-time as on every other system, openkal-musl and openkal-llvm-runtime, with
this implementation beneath them, so a DOS program is written in C++23 with
`import std`:

```toml
[target.i686-dos-musl]
platform = "openkal-platform@0.28.0"
platform = "openkal-platform@0.32.0"
runner = { enabled = true } # the platform states the machine runner off until a project enables it
```

```sh
Expand Down Expand Up @@ -102,7 +103,8 @@ C library states `c-abi = false` for the row and calls openkal directly.
|---|---|
| `stream` | DOS handles: 0, 1 and 2, and a file's (int 21h, 3Fh and 40h) through the transfer buffer; a handle's properties from IOCTL 4400h |
| `memory` | DPMI blocks (0501h, 0502h), 4 KiB granular; the free memory from 0500h |
| `env` | the arguments from the command tail (offset 80h of the segment prefix), the variables from the environment block, the program's name, the system name `dos` |
| `env` | the arguments from the command tail (offset 80h of the segment prefix), divided as DOS's C compilers divide it (0.3.0), the first element as its starter gave it (`KAL_ARG0`, 0.3.0), the variables from the environment block, the program's name, the system name `dos` |
| `process` | EXEC (int 21h, 4B00h), the started program run to its end before the start returns, its status from 4Dh (0.3.0) |
| `time` | the BIOS tick count at 0040:006C (54.925494 ms, made monotonic across midnight); the wall clock from int 21h 2Ah and 2Ch, read as UTC; a sleep by int 15h, 86h |
| `fs` | the current directory and each volume as supplied directories (`C:\WORK`, `C:`, `Z:`), a directory held as its name; open, create (5Bh refuses a name that exists), truncate, seek, transfers at an offset, find first and next for enquiry and listing, make and remove a directory, remove, rename, a file's time (5700h, 5701h) and a volume's capacity (36h) |
| `terminal` | the console's mode by bit 5 of its device word (cooked: line assembly and echo; raw: every byte as typed), ANSI.SYS's sequences where it answers int 2Fh 1A00h, the size from the BIOS data area |
Expand All @@ -121,13 +123,22 @@ name that exists, which is the rename that does not replace
first and is not atomic. A name that holds DOS's `\` or a volume's `:` is
refused, as a name that ascends is.

Absent (openkal clause 6.2): contexts, processes as openkal states them, the
network, and regions --- DOS cannot reserve an address apart from backing it: the
Absent (openkal clause 6.2): contexts, the network, and regions --- DOS cannot reserve an address apart from backing it: the
DPMI host CWSDPMI is DPMI 0.90 and refuses 0504h (measured), which is the case
`openkal/region.h` names for leaving the interface out. DOSBox Staging's console
accepts the request for raw mode and stays cooked (measured);
`kal_terminal_get_mode` reports what is in effect.

0.2.0 is written against openkal 0.26, and nothing but the specification's
version changes; `openkal-dos-tools` carries the same version, as the two are one
archive. What 0.26 requires is of `openkal.process`, which was absent until 0.3.0: a
start bounded by the environment alone (clause 7.6) and a unit claimed only
where a reference can be made (clause 7.18). What a program receives is bounded
by DOS alone already: the command tail of the segment prefix holds at most 127
bytes, the room `src/start.cpp` keeps for its words is that bound's, and the
variables are read from the environment block where it lies, however many it
holds.

An i386 ok image (`i686-openkal`) runs on DOS as on firmware: the platform's
runner `hosts` include `dos-i686`, so `mcpp pack --pkg-format single-file
--systems dos-i686` builds openkal-run's core runner as a program of this row,
Expand All @@ -140,6 +151,68 @@ with the image in its `.oke` section, and the runner places the image through
lists, resizes (the extension reads as zero) and removes them, and returns 9
(measured under DOSBox Staging).

## Starting a program (0.3.0)

`openkal.process` is provided through EXEC (int 21h, 4B00h). DOS runs one
program: EXEC suspends the caller until the program it started ends, so
`kal_process_spawn` returns after the end and `kal_process_wait` answers the
status at once, int 21h 4Dh's (`KAL_PROCESS_PROP_RUNS_TO_END`, openkal clause
7.19). A program ended by Ctrl-C is reported ended with 130, one ended by a
critical error ended with its code. `TERMINATE` and `CHANNEL` are not claimed;
`BOUND_LIFETIME` is, a started program being unable to outlive its starter;
`EXIT_STATUS`, `STREAM_PASSING`, `NULL_STREAMS` and `ONE_STRING` are claimed. A
started program of this implementation enters protected mode through the DPMI
host already resident, as a client of its own.

| what a start gives | how it reaches the started program |
|---|---|
| the vector after the first element | the command tail: a blank before each element, and an element that is empty or holds a blank or a quote quoted, its quotes preceded by a backslash and the backslashes before a quote doubled; `KAL_SPAWN_ONE_STRING` joins the elements as they are |
| the first element | `KAL_ARG0=<the program's name>\|<first element>` among the variables: DOS gives a program no first element, only the name EXEC was given; a program of this implementation reads it where the name is its own, and does not list it among its variables |
| the variables | an environment block of their own in conventional memory (DPMI 0100h) |
| the directory it runs in | DOS's current drive and directory for the call, restored after it |
| the three streams | for the call, the caller's handle table (PSP:34h) holds at positions 0, 1 and 2 the caller's own handles, those it placed, or NUL opened for the position, then DOS's AUX and PRN at 3 and 4, and nothing else; the table is restored after |
| the resource preference | `KAL_RESOURCES` among the variables, which `kal_env_resources` reads |

THE STARTED PROGRAM RECEIVES NO OTHER HANDLE (clause 7.13). DOS gives a started
program every handle of its starter that was opened to be inherited, which is
every handle DOS's 3Ch, 3Dh and 5Bh open: the stub's own file, the files a
program holds. Measured: a copy that probes DOS handles 5 to 19 finds only its
own program's file, which its stub opens, while its starter holds a file open;
with the caller's handle table left as it was, it finds the starter's too.

THE BOUNDS ARE DOS'S (clause 7.6): the command tail holds 126 bytes before its
carriage return and an environment 32 KiB, and a start beyond either is
`kal_err_no_space`; nothing is shortened. A start that cannot happen is refused
by its reason before DOS is asked (a name that is not there, a directory, an
entry without `=`), and EXEC's own refusals are mapped: not found, access
denied, no memory, an environment DOS does not take (`kal_err_no_space`), a file
that is no program (`kal_err_not_program`).

THE TAIL IS DIVIDED AS DOS'S C COMPILERS DIVIDE IT. Blanks separate; a double
quote begins or ends a part in which they do not; backslashes before a quote are
halved, and an odd one makes the quote a character; other backslashes are
themselves. 0.2.0 divided at blanks alone, so `"a b"` arrived as two arguments
with their quotes, and no starter could pass an argument holding a blank.

`kal_interfaces()` names the nine interfaces the manifest states. Until 0.3.0
it named five, leaving out `exec`, `fs` and `terminal`.

`tests/starts`, C beneath openkal-musl on openkal-platform 0.31.2's row with this
repository's openkal-dos, run by dirun under DOSBox Staging 0.83.0-2 (where the
program is itself started by REDIR, a DJGPP program): `posix_spawn` starts the
program again with the first element `kid`, `a b`, an empty argument, `q"uote`
and `C:\A B\`, and a variable set by `setenv`, and `waitpid` reads its status 3;
a copy's standard output placed on a file writes the file, and on `/dev/null`
writes nothing; a copy finds no handle of its starter's; an absent name is
`ENOENT`; a tail of more than 126 bytes is `E2BIG`. The program ends with 42.

The examples and tests name openkal-platform 0.31.2, whose group holds openkal
0.26 (under 0.28.0 the build refused the specification's version), and enable
its runner. `tests/fault` writes at offset 0x7ffffff0 of the block rather than
0xfffffff0: under the platform's runner the program is a DPMI client started by
REDIR, and the address 16 bytes below its block is REDIR's memory, where the
write did not fault (measured with openkal-dos 0.2.0, status 3 in place of 139).

## The row's conventions

The row compiles for `i386-unknown-none-elf` at `-march=i486`: DOSBox Staging
Expand All @@ -162,7 +235,7 @@ Windows and refuses a file that is no ELF32 i386 executable; and that the
examples and the tests, built for `i686-dos-musl` with this repository's
openkal-dos and seal and run under DOSBox Staging, print what they state and end
with their statuses: `examples/hello` 7 (unoptimised and optimised),
`examples/files` 9, `tests/fault` 139, `tests/stack` 0 (sixty-four frames of
`examples/files` 9, `tests/starts` 42 (0.3.0), `tests/fault` 139, `tests/stack` 0 (sixty-four frames of
8 KiB within the default stack) and `tests/stack-small` 139 (the same recursion
with a stack of 64 KiB, stopped by the guard page).

Expand Down
3 changes: 2 additions & 1 deletion examples/files/mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ kind = "bin"
main = "src/main.cpp"

[target.i686-dos-musl]
platform = "openkal-platform@0.28.0"
platform = "openkal-platform@0.31.2"
runner = { enabled = true }
3 changes: 2 additions & 1 deletion examples/hello/mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ kind = "bin"
main = "src/main.cpp"

[target.i686-dos-musl]
platform = "openkal-platform@0.28.0"
platform = "openkal-platform@0.31.2"
runner = { enabled = true }
13 changes: 7 additions & 6 deletions mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
[package]
namespace = "mcpplibs"
name = "openkal-dos"
version = "0.1.1"
version = "0.3.0"
description = "An implementation of openkal on DOS through DPMI (technical experiment only)"
license = "Apache-2.0"
provides = ["mcpp:kernel-abi=openkal"]
authors = ["mcpplibs"]
repo = "https://github.com/mcpplibs/openkal-dos"

# The interfaces DOS has a counterpart for. Absent (openkal clause 6.2): contexts, processes as
# openkal states them, the network, and regions --- the DPMI host reserves no address apart from
# backing it (CWSDPMI is DPMI 0.90 and refuses 0504h, measured).
# The interfaces DOS has a counterpart for; `openkal.process' is EXEC, a started program run to its
# end before the start returns (0.3.0, openkal clause 7.19). Absent (openkal clause 6.2): contexts,
# the network, and regions --- the DPMI host reserves no address apart from backing it (CWSDPMI is
# DPMI 0.90 and refuses 0504h, measured).
[kernel-abi]
provides-interfaces = ["openkal.abort", "openkal.env", "openkal.exec", "openkal.fs", "openkal.memory",
"openkal.stream", "openkal.terminal", "openkal.time"]
"openkal.process", "openkal.stream", "openkal.terminal", "openkal.time"]

[dependencies]
openkal = "^0.25.0"
openkal = "^0.26.0"

# With no C library this package runs the initialisers and calls `main'; `standalone' is the
# arrangement beneath a C library written above openkal (openkal-musl names it so on
Expand Down
20 changes: 20 additions & 0 deletions src/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// THE BLOCK is [loadable segments][the record's page][the guard page][the stack]; the stack's top
// is the block's end.
#pragma once
#include <openkal/fs.h>

namespace okdos {

Expand Down Expand Up @@ -78,6 +79,25 @@ void install_faults();
void guard_stack();
void check_stack();

// openkal.process (process.cpp, 0.3.0) and the volume (fs.cpp): the whole name of a program beneath
// a directory, in `out' (kPathMax bytes), or the reason there is none; a directory's whole name.
constexpr u32 kPathMax = 128;
int program_path(kal_dir base, const char* name, kal_uintptr len, char* out);
const char* directory_path(kal_dir d);

// THE NAME A STARTED PROGRAM OBSERVES AS ITS OWN (0.3.0, clause 7.6). DOS gives a program no first
// element: the name EXEC was given is written after its environment, and a program finds its file
// by it. A starter of this implementation writes `KAL_ARG0=<that name>|<the first element>' among
// the variables; the program it starts reads the first element there where the name is its own,
// and a variable inherited through a program that does not read it names another program's file.
// The variable is the implementation's and is not among the variables a program enumerates.
inline constexpr char kArg0[] = "KAL_ARG0=";
inline constexpr u32 kArg0Len = sizeof kArg0 - 1;
inline bool names_arg0(const char* e) {
for (u32 i = 0; i < kArg0Len; ++i) if (e[i] != kArg0[i]) return false;
return true;
}

// Days from 1970-01-01 to a civil date (H. Hinnant's algorithm).
inline long long days_from_civil(long long y, unsigned m, unsigned d) {
y -= m <= 2;
Expand Down
29 changes: 22 additions & 7 deletions src/env.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// openkal.env on DOS: the arguments are the command tail of the program's segment prefix (PSP),
// split at blanks; position zero is the name DOS started the program by; the variables are the
// environment block's NAME=VALUE strings.
// divided as DOS's C compilers divide it (start.cpp); position zero is the starter's first element
// where it travelled (KAL_ARG0, dos.h), the name DOS started the program by otherwise; the variables
// are the environment block's NAME=VALUE strings, KAL_ARG0 not among them.
#include <openkal/env.h>

#include "dos.h"
Expand All @@ -21,12 +22,12 @@ kal_intptr give(const char* s, kal_uintptr n, char* out, kal_uintptr cap) {

const char* environment() { return reinterpret_cast<const char*>(low(static_cast<u32>(self().h->env_segment) * 16u)); }

// The I-th NAME=VALUE string, or null.
// The I-th NAME=VALUE string, or null; the implementation's KAL_ARG0 is passed over.
const char* variable(kal_uintptr index) {
const char* p = environment();
for (kal_uintptr i = 0; *p; ++i) {
if (i == index) return p;
p += length(p) + 1;
for (kal_uintptr i = 0; *p; p += length(p) + 1) {
if (names_arg0(p)) continue;
if (i++ == index) return p;
}
return nullptr;
}
Expand Down Expand Up @@ -76,6 +77,20 @@ kal_intptr kal_env_loader(char*, kal_uintptr) { return -kal_err_not_found; }

kal_intptr kal_env_system(char* out, kal_uintptr cap) { return give("dos", 3, out, cap); }

int kal_env_resources(void) { return KAL_RESOURCES_ECONOMY; }
// THE RESOURCE PREFERENCE (openkal 0.23): KAL_RESOURCES where a starter wrote one (0.3.0), as the
// other implementations read it; economy otherwise, this environment's memory being small.
int kal_env_resources(void) {
char v[16];
const kal_intptr n = kal_env_var("KAL_RESOURCES", 13, v, sizeof v);
const auto is = [&](const char* w) {
const kal_uintptr k = length(w);
if (n != static_cast<kal_intptr>(k)) return false;
for (kal_uintptr i = 0; i < k; ++i) if (v[i] != w[i]) return false;
return true;
};
return is("performance") ? KAL_RESOURCES_PERFORMANCE
: is("balanced") ? KAL_RESOURCES_BALANCED
: KAL_RESOURCES_ECONOMY;
}

} // extern "C"
16 changes: 16 additions & 0 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ void preopen_table() {

namespace okdos {
bool append_handle(u16 handle) { return handle < 256 && g_append[handle]; }

// WHAT openkal.process NEEDS OF THE VOLUME (process.cpp, 0.3.0): the whole name of a program beneath
// a directory, with the reason there is none to start, and a directory's whole name.
int program_path(kal_dir base, const char* name, kal_uintptr len, char* out) {
if (int e = join(base, name, len, out)) return e;
if (len == 1 && name[0] == '.') return kal_err_is_directory;
const int a = attributes(out);
if (a < 0) return kal_err_not_found;
if (a & 0x10) return kal_err_is_directory;
return kal_ok;
}

const char* directory_path(kal_dir d) {
const directory* x = dir_of(d);
return x ? x->path : nullptr;
}
}

extern "C" {
Expand Down
11 changes: 7 additions & 4 deletions src/kal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//
// DOS divides its services as openkal does for three interfaces: handles 0 to 2 are the standard
// streams (int 21h, 3Fh and 40h), the program ends with a status (4Ch), and memory beyond the
// first megabyte is the DPMI host's (int 31h, 0501h). The interfaces DOS has no counterpart for
// --- contexts, processes as openkal states them, the network --- are absent rather than present
// and failing (openkal clause 6.2).
// first megabyte is the DPMI host's (int 31h, 0501h). A program is started by EXEC and has ended
// when EXEC returns (process.cpp, 0.3.0). The interfaces DOS has no counterpart for --- contexts,
// the network --- are absent rather than present and failing (openkal clause 6.2).
#include <openkal/abort.h>
#include <openkal/memory.h>
#include <openkal/stream.h>
Expand Down Expand Up @@ -171,8 +171,11 @@ int kal_memory_info(kal_memory_status* out) {
// ── version ─────────────────────────────────────────────────────────────────
kal_u64 kal_version(void) { return KAL_VERSION; }

// The interfaces the manifest states (`[kernel-abi] provides-interfaces'). Until 0.3.0 the word
// named five of the eight, leaving out exec, fs and terminal, which the implementation provides.
kal_u64 kal_interfaces(void) {
return KAL_IFACE_ABORT | KAL_IFACE_STREAM | KAL_IFACE_MEMORY | KAL_IFACE_ENV | KAL_IFACE_TIME;
return KAL_IFACE_ABORT | KAL_IFACE_STREAM | KAL_IFACE_MEMORY | KAL_IFACE_ENV | KAL_IFACE_TIME
| KAL_IFACE_EXEC | KAL_IFACE_FS | KAL_IFACE_TERMINAL | KAL_IFACE_PROCESS;
}

} // extern "C"
Loading
Loading