/APPS/PYTHON.ELF, from the Start menu, Files, and the IDE app) is a cross-compiled, freestanding build of real CPython 3.11.9, not MicroPython. See the Python Progress page for the current status and the Licenses page for how the three Python-related components in this tree (the MicroPython port, a small original hand-written subset interpreter, and the CPython port that actually ships) relate to each other. The plan below is kept for its options-analysis reasoning, not as a description of the current system.Python Support Implementation Plan for MayteraOS
Version: 1.0 Date: January 2026 Status: Planning
Executive Summary
This document outlines a comprehensive plan for implementing Python support in MayteraOS. After analyzing the kernel's capabilities and researching available options, we recommend porting MicroPython as the most practical approach. This document covers options analysis, requirements, implementation phases, challenges, and complexity estimates.
Table of Contents
- Options Analysis
- Requirements Assessment
- Implementation Phases
- Challenges and Mitigations
- Estimated Complexity
- Appendix: MayteraOS Kernel Capabilities
1. Options Analysis
Option A: Port MicroPython (RECOMMENDED)
Description: MicroPython is a lean and efficient implementation of Python 3 designed specifically for microcontrollers and constrained systems. It includes a Python compiler to bytecode and a runtime interpreter.
Minimum Requirements:
- 256KB code space
- 16KB RAM (minimal), 128KB RAM (full-featured)
- C99 compiler
Advantages:
- Purpose-built for embedded/constrained environments
- No external dependencies for core VM
- Modular architecture - features can be enabled/disabled
- Well-documented porting guide
- Active community and maintenance
- Provides "minimal" and "bare-arm" reference ports
- Supports setjmp/longjmp for exception handling (no assembly required)
- Python 3.4+ syntax support including async/await
Disadvantages:
- Limited standard library compared to CPython
- Some CPython libraries not compatible
- Performance lower than native code (but offers Viper compiler for near-C speed)
Assessment: MicroPython is the ideal choice for MayteraOS. Its minimal footprint, embedded focus, and lack of external dependencies align perfectly with our kernel's architecture.
Option B: Port CPython
Description: CPython is the reference implementation of Python, providing the full Python language and standard library.
Minimum Requirements:
- 4-8MB code space (full installation ~100MB+)
- 32MB+ RAM minimum
- POSIX-compatible libc
- Dynamic linking support
- Full file system with
/usr/lib/python*hierarchy
Advantages:
- Full Python compatibility
- Complete standard library
- All third-party packages work
Disadvantages:
- Massive code size (~25MB for interpreter alone)
- Heavy memory requirements
- Requires extensive POSIX compatibility layer
- Needs dynamic linking (dlopen, dlsym)
- Complex build system (autoconf, Make, Python bootstrap)
- Threading model requires pthreads
- Not designed for bare-metal environments
Assessment: Not feasible for MayteraOS in the near term. Would require implementing a POSIX compatibility layer, dynamic linker, and significant infrastructure that doesn't exist in the current kernel.
Option C: Custom Minimal Interpreter
Description: Build a custom Python-like interpreter from scratch, implementing only essential features.
Potential Implementation Approaches:
- Subset interpreter (Python-like syntax, limited features)
- Bytecode interpreter only (pre-compile on host)
- REPL-only with minimal built-ins
Advantages:
- Full control over implementation
- Can be optimized specifically for MayteraOS
- No external code dependencies
- Minimal footprint possible
Disadvantages:
- Massive development effort (person-years of work)
- No existing ecosystem compatibility
- Must implement lexer, parser, AST, bytecode compiler, VM
- Documentation and maintenance burden
- Likely to have bugs/incompatibilities
Assessment: Not recommended. The development effort would be enormous, and MicroPython already provides a battle-tested solution with similar goals.
Recommendation: Option A - MicroPython
Justification:
| Criterion | MicroPython | CPython | Custom |
|---|---|---|---|
| Memory Footprint | 256KB code, 16KB RAM | 25MB+, 32MB+ RAM | Variable |
| Porting Effort | Medium (weeks) | Very High (months+) | Extreme (years) |
| Python Compatibility | Good (3.4+) | Full | Limited |
| External Dependencies | None | Many | None |
| Documentation | Excellent | N/A | None |
| Community Support | Active | N/A | None |
| Maintenance Burden | Low | High | Extreme |
MicroPython is specifically designed for exactly our use case: a custom OS with limited resources and no POSIX layer. The porting guide provides clear steps, and reference implementations (bare-arm, minimal) serve as templates.
2. Requirements Assessment
2.1 Kernel Features Required
Memory Management
| Feature | Required | Available in MayteraOS | Notes |
|---|---|---|---|
| Heap Allocator (malloc/free) | Yes | Yes | kmalloc(), kfree(), krealloc() in mm/heap.c |
| Memory Region Allocation | Yes | Yes | pmm_alloc_page(), pmm_alloc_pages() |
| Page-aligned Allocation | Yes | Yes | kmalloc_aligned() available |
| Zeroed Allocation | Yes | Yes | kzalloc() available |
| Heap Expansion | Yes | Yes | Auto-expands up to 256MB |
Status: FULLY AVAILABLE
File I/O
| Feature | Required | Available in MayteraOS | Notes |
|---|---|---|---|
| File Open/Close | Yes | Yes | fat_open(), fat_close() |
| File Read | Yes | Yes | fat_read(), fat_read_file() |
| File Write | No (initially) | Partial | Stubs exist, not implemented |
| File Seek | Yes | Yes | fat_seek() |
| File Size | Yes | Yes | fat_size() |
| Directory Listing | Yes | Yes | fat_readdir(), fat_list_dir() |
| File Existence Check | Yes | Yes | fat_exists() |
Status: READ OPERATIONS COMPLETE, WRITE OPERATIONS NEED IMPLEMENTATION
Console/Terminal I/O
| Feature | Required | Available in MayteraOS | Notes |
|---|---|---|---|
| Character Input (blocking) | Yes | Yes | keyboard_get_char() |
| Character Output | Yes | Yes | kputc(), console_putc() |
| String Output | Yes | Yes | kprintf(), console_puts() |
| Input Available Check | Yes | Yes | keyboard_has_char() |
Status: FULLY AVAILABLE
Standard Library Functions
| Feature | Required | Available in MayteraOS | Notes |
|---|---|---|---|
| memset, memcpy, memmove | Yes | Yes | string.c |
| strlen, strcpy, strcmp | Yes | Yes | string.c |
| strcat, strchr, strstr | Yes | Yes | string.c |
| atoi, itoa | Yes | Yes | string.c |
Status: FULLY AVAILABLE (but will need expansion)
Time Keeping
| Feature | Required | Available in MayteraOS | Notes |
|---|---|---|---|
| System Timer Ticks | Yes | Yes | timer_ticks global, 100Hz |
| Millisecond Delay | Yes | Yes | proc_sleep() for processes |
| Time Since Boot | Yes | Yes | Via timer_ticks |
Status: AVAILABLE (may need wrapper functions)
Exception Handling
| Feature | Required | Available in MayteraOS | Notes |
|---|---|---|---|
| setjmp/longjmp | Yes | No | NEEDS IMPLEMENTATION |
| Stack Unwinding | Optional | No | Not strictly required |
Status: NEEDS IMPLEMENTATION (setjmp/longjmp functions)
2.2 Features to be Added
Priority 1: Essential for MicroPython Port
- setjmp/longjmp Implementation
- Required for MicroPython's NLR (non-local return) exception handling
- Can use C implementation or assembly for x86_64
- Estimated effort: 1-2 days
- Standard Math Functions
- Basic: abs, div, ldiv
- Floating point: sin, cos, sqrt, pow, log, exp (if float support enabled)
- Estimated effort: 3-5 days (can use software implementations)
- Time Functions Wrapper
mp_hal_ticks_ms()- milliseconds since bootmp_hal_ticks_us()- microseconds since bootmp_hal_delay_ms()- delay function- Estimated effort: 1 day
Priority 2: Standard Library Support
- String Formatting Enhancements
snprintf(),vsnprintf()- Floating-point formatting (if enabled)
- Estimated effort: 2-3 days
- Extended Memory Functions
calloc()- already have kzalloc, need wrapper- Memory statistics for GC tuning
- Estimated effort: 1 day
Priority 3: Full-Featured Port
- File System Write Operations
fat_create(),fat_write(),fat_delete()- Required for saving .py files, persistent storage
- Estimated effort: 5-7 days
- Random Number Generator
- Required for
randommodule - Can use RDRAND instruction on x86_64
- Estimated effort: 1 day
3. Implementation Phases
Phase 1: Core Interpreter (Weeks 1-4)
Goal: Get MicroPython REPL running with basic Python operations
Week 1: Infrastructure Setup
- [ ] Create
kernel/python/directory structure - [ ] Download MicroPython source (v1.22 or latest stable)
- [ ] Create
mpconfigport.hwith minimal feature set - [ ] Create
mphalport.hwith MayteraOS-specific HAL functions - [ ] Set up Makefile integration with kernel build
Deliverables:
- Build system configured
- MicroPython source integrated into kernel tree
Week 2: HAL Implementation
- [ ] Implement setjmp/longjmp for x86_64
- [ ] Implement
mp_hal_stdin_rx_chr()using keyboard driver - [ ] Implement
mp_hal_stdout_tx_strn()using console/serial - [ ] Implement time functions (ticks_ms, ticks_us, delay_ms)
- [ ] Implement
gc_collect()stack scanning
Deliverables:
- Basic I/O working
- Time functions operational
- GC can run
Week 3: Memory and Core VM
- [ ] Configure GC heap (start with 256KB allocation)
- [ ] Implement memory allocation wrappers
- [ ] Disable file system imports initially (MICROPY_READER_POSIX = 0)
- [ ] Disable floating point initially (MICROPY_FLOAT_IMPL = NONE)
- [ ] Build and test core VM initialization
Deliverables:
- MicroPython VM initializes successfully
- Basic memory management working
Week 4: REPL Integration
- [ ] Create Python shell application (
gui/python.c) - [ ] Integrate REPL into kernel shell (via
pythoncommand) - [ ] Test basic operations: arithmetic, strings, lists
- [ ] Test function definitions and calls
- [ ] Test class definitions and instances
Deliverables:
- Working Python REPL in MayteraOS
- Basic Python programs execute correctly
Phase 1 Configuration (mpconfigport.h):
// Minimal MicroPython configuration for MayteraOS
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_MINIMUM)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_HELPER_REPL (1)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG)
#define MICROPY_ENABLE_SOURCE_LINE (0)
#define MICROPY_STREAMS (0)
#define MICROPY_READER_POSIX (0)
#define MICROPY_PY_BUILTINS_BYTEARRAY (1)
#define MICROPY_PY_BUILTINS_DICT_FROMKEYS (0)
#define MICROPY_PY_BUILTINS_SET (0)
#define MICROPY_PY_SYS (0)
#define MICROPY_PY_IO (0)
Estimated Complexity: Medium Estimated Effort: 80-120 hours
Phase 2: Standard Library Subset (Weeks 5-8)
Goal: Enable essential Python modules for practical programming
Week 5: Math and Core Modules
- [ ] Enable floating-point support (software float)
- [ ] Implement/port math functions (sin, cos, sqrt, etc.)
- [ ] Enable
mathmodule - [ ] Enable
sysmodule (partial) - [ ] Enable
collectionsmodule
Deliverables:
- Floating-point arithmetic working
- Math operations available
Week 6: Data Structures
- [ ] Enable
arraymodule - [ ] Enable
structmodule - [ ] Enable
jsonmodule (parser only initially) - [ ] Enable
remodule (basic regex)
Deliverables:
- Binary data manipulation working
- JSON parsing available
Week 7: File System Integration
- [ ] Implement
mp_import_stat()using FAT driver - [ ] Implement
mp_lexer_new_from_file() - [ ] Enable importing .py files from disk
- [ ] Test
importstatement with local modules
Deliverables:
- Can import Python files from FAT filesystem
- Module loading functional
Week 8: Time and OS Basics
- [ ] Enable
timemodule - [ ] Implement
time.sleep(),time.ticks_ms() - [ ] Create basic
mayteramodule (OS-specific) - [ ] Add version info, system info functions
Deliverables:
- Time operations available
- MayteraOS-specific module started
Phase 2 Additional Configuration:
// Phase 2 additions to mpconfigport.h
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
#define MICROPY_PY_MATH (1)
#define MICROPY_PY_CMATH (0)
#define MICROPY_PY_SYS (1)
#define MICROPY_PY_SYS_EXIT (1)
#define MICROPY_PY_COLLECTIONS (1)
#define MICROPY_PY_ARRAY (1)
#define MICROPY_PY_STRUCT (1)
#define MICROPY_PY_JSON (1)
#define MICROPY_PY_RE (1)
#define MICROPY_PY_TIME (1)
#define MICROPY_PY_TIME_GMTIME (0)
#define MICROPY_READER_VFS (1)
#define MICROPY_VFS (1)
#define MICROPY_VFS_FAT (1)
Estimated Complexity: Medium-High Estimated Effort: 120-160 hours
Phase 3: MayteraOS-Specific Modules (Weeks 9-16)
Goal: Expose MayteraOS features to Python programs
Weeks 9-10: GUI Module (maytera.gui)
Functions to implement:
# maytera.gui module API
import maytera.gui as gui
# Window management
win = gui.create_window("My App", x=100, y=100, width=400, height=300)
win.show()
win.hide()
win.close()
win.title = "New Title"
# Drawing primitives
win.fill_rect(x, y, width, height, color)
win.draw_rect(x, y, width, height, color)
win.draw_line(x1, y1, x2, y2, color)
win.draw_text(x, y, "Hello", color)
# Widgets
button = gui.Button(win, x, y, width, height, "Click Me")
button.on_click = my_callback
label = gui.Label(win, x, y, "Status:")
textbox = gui.TextBox(win, x, y, width, height)
# Events
event = gui.poll_event()
if event.type == gui.EVENT_CLICK:
handle_click(event)
Implementation approach:
- Create C module:
python/modgui.c - Wrap existing window.c functions
- Create Python object types for Window, Widget
- Use callbacks for event handling
Weeks 11-12: Networking Module (maytera.net)
Functions to implement:
# maytera.net module API
import maytera.net as net
# Network status
info = net.status() # Returns dict with IP, MAC, gateway, etc.
net.configure(ip="198.51.100.20", gateway="198.51.100.1", netmask="255.255.255.0")
# ICMP
result = net.ping("198.51.100.1", count=4, timeout=1000)
# UDP (if available)
sock = net.UDPSocket()
sock.bind(port=5000)
sock.sendto(data, ("198.51.100.1", 5000))
data, addr = sock.recvfrom()
# DNS (future)
ip = net.resolve("example.com")
Implementation approach:
- Create C module:
python/modnet.c - Wrap net/net.c, net/udp.c, net/icmp.c functions
- Implement basic socket-like API
Weeks 13-14: System Module (maytera.sys)
Functions to implement:
# maytera.sys module API
import maytera.sys as sys
# Memory info
mem = sys.memory() # {"total": 1024*1024*1024, "used": ..., "free": ...}
# Process info
procs = sys.processes() # List of process info dicts
# Hardware info
info = sys.hardware() # CPU, etc.
# Power management
sys.reboot()
sys.shutdown()
# File system
fs = sys.filesystem() # FAT info
Weeks 15-16: File I/O Module (maytera.fs)
Functions to implement:
# maytera.fs module API
import maytera.fs as fs
# Directory operations
contents = fs.listdir("/")
fs.mkdir("/mydir")
fs.chdir("/mydir")
cwd = fs.getcwd()
# File operations
data = fs.read_file("/path/to/file.txt")
# fs.write_file("/path/to/file.txt", data) # When write support added
# File info
exists = fs.exists("/path/to/file")
size = fs.size("/path/to/file")
is_dir = fs.isdir("/path")
Implementation approach:
- Create C module:
python/modfs.c - Wrap FAT filesystem functions
- Provide Pythonic file handling
Phase 3 Module Structure:
kernel/python/
modules/
modgui.c # maytera.gui
modnet.c # maytera.net
modsys.c # maytera.sys
modfs.c # maytera.fs
mpconfigport.h # Updated config
mphalport.c # HAL implementation
mphalport.h # HAL declarations
qstrdefsport.h # QSTR definitions for modules
Estimated Complexity: High Estimated Effort: 200-300 hours
4. Challenges and Mitigations
4.1 Memory Constraints
Challenge: MayteraOS heap is capped at 256MB, and MicroPython's GC can be memory-hungry for complex programs.
Mitigations:
- Start with conservative GC heap (256KB-1MB)
- Implement memory limit configuration
- Add GC threshold tuning
- Monitor memory usage during development
- Provide
gc.collect()in Python for manual GC
Risk Level: Medium
4.2 No Dynamic Linking
Challenge: MicroPython's native emitter and some advanced features expect dynamic code generation. MayteraOS has no dynamic linker.
Mitigations:
- Disable native emitter (
MICROPY_EMIT_NATIVE = 0) - Disable Viper emitter (
MICROPY_EMIT_VIPER = 0) - Use bytecode interpreter only (still fast enough for most uses)
- If needed later, implement JIT in-memory (requires W^X handling)
Risk Level: Low (bytecode is sufficient)
4.3 Limited libc Equivalent
Challenge: MicroPython expects certain libc functions. MayteraOS has a minimal string.c.
Missing Functions (need implementation):
| Function | Difficulty | Priority |
|---|---|---|
| setjmp/longjmp | Medium | P1 |
| snprintf/vsnprintf | Easy | P1 |
| qsort | Medium | P2 |
| bsearch | Easy | P2 |
| strtol/strtoul | Easy | P1 |
| strtod | Medium | P2 (if float enabled) |
| isalpha/isdigit/etc | Easy | P1 (already have some) |
| abs/labs | Trivial | P1 |
| rand/srand | Easy | P2 |
Mitigations:
- Implement required functions in
kernel/stdlib/directory - Use MicroPython's built-in fallbacks where available
- Prioritize implementation based on build errors
Risk Level: Medium (well-defined scope)
4.4 File System Write Operations
Challenge: FAT write operations are not implemented, limiting Python's ability to save files.
Mitigations:
- Phase 1-2 work without file writes (REPL-only, import from read-only FS)
- Implement FAT write before Phase 3 file I/O module
- Consider RAM-based "virtual filesystem" for intermediate solution
Risk Level: Medium (deferred to later phase)
4.5 Threading
Challenge: MicroPython supports threading on some platforms. MayteraOS has basic process support but no pthreads.
Mitigations:
- Disable threading (
MICROPY_PY_THREAD = 0) - Use single-threaded model (MicroPython works fine this way)
- Each Python "app" can be separate process if needed
Risk Level: Low
4.6 Floating-Point ABI
Challenge: x86_64 uses SSE for floating-point. Need to ensure FPU state is handled correctly.
Mitigations:
- MayteraOS already runs on x86_64 with UEFI (FPU should be available)
- Test floating-point early in Phase 2
- If issues, use software float (
MICROPY_FLOAT_IMPL_FLOAT)
Risk Level: Low
5. Estimated Complexity
Summary by Phase
| Phase | Duration | Effort (Hours) | Complexity | Dependencies |
|---|---|---|---|---|
| Phase 1: Core Interpreter | 4 weeks | 80-120 | Medium | setjmp/longjmp |
| Phase 2: Standard Library | 4 weeks | 120-160 | Medium-High | Math functions, VFS |
| Phase 3: MayteraOS Modules | 8 weeks | 200-300 | High | FAT write (optional) |
| Total | 16 weeks | 400-580 | - | - |
Detailed Breakdown
Phase 1 Tasks
| Task | Effort | Complexity | Notes |
|---|---|---|---|
| Build system setup | 8h | Low | Makefile integration |
| setjmp/longjmp | 16h | Medium | x86_64 assembly |
| HAL I/O functions | 8h | Low | Wrap existing drivers |
| HAL time functions | 4h | Low | Use timer_ticks |
| Memory/GC setup | 16h | Medium | Heap allocation, GC |
| VM initialization | 8h | Low | Follow minimal port |
| REPL integration | 16h | Medium | Shell command, GUI app |
| Testing & debugging | 24h | Medium | Various edge cases |
Phase 2 Tasks
| Task | Effort | Complexity | Notes |
|---|---|---|---|
| Floating-point | 16h | Medium | Software or hardware |
| Math module | 24h | Medium | Port/implement functions |
| String formatting | 16h | Medium | snprintf, etc. |
| VFS integration | 24h | Medium | Wrap FAT driver |
| Module imports | 16h | Medium | File-based imports |
| Time module | 8h | Low | Wrap timer functions |
| Testing | 24h | Medium | Module testing |
Phase 3 Tasks
| Task | Effort | Complexity | Notes |
|---|---|---|---|
| GUI module design | 16h | Medium | API design |
| GUI module impl | 48h | High | Window/widget wrappers |
| Net module design | 8h | Low | API design |
| Net module impl | 32h | Medium | Network wrappers |
| Sys module | 24h | Medium | System info wrappers |
| FS module | 24h | Medium | File operations |
| FAT write support | 40h | High | New kernel feature |
| Integration testing | 40h | High | Full system testing |
| Documentation | 24h | Low | API docs, examples |
Appendix: MayteraOS Kernel Capabilities
Current Kernel Features (v1.0+)
Based on source code analysis of the kernel/ source tree:
Memory Management (mm/)
- PMM (pmm.c): Bitmap-based physical page allocator, supports up to 64GB RAM
- VMM (vmm.c): 4-level paging for x86_64, page mapping/unmapping
- Heap (heap.c): Linked-list allocator with coalescing, 16-byte alignment
- Functions: kmalloc, kfree, krealloc, kzalloc, kmalloc_aligned
- Initial size: 16MB, max: 256MB
- Thread-safe with spinlock
File System (fs/)
- FAT (fat.c): FAT12/FAT16/FAT32 support
- Read operations: complete
- Write operations: stubs only
- Long filename support: partial (LFN entries skipped)
Networking (net/)
- Drivers: E1000 is the network driver actually initialized at boot. A VirtIO-net driver exists in the tree and builds, but has no caller wiring it up, so it is not active on a running system.
- Protocols: Ethernet, ARP, IP, ICMP, UDP, DHCP
- TCP: Implementation exists (tcp.c)
GUI (gui/)
- Window Manager (window.c): Full windowing system
- Widgets (widget.c): Buttons, labels, text fields, etc.
- Applications: Calculator, Editor, Terminal, Settings, File Browser
Process Management (proc/)
- Scheduler: Priority-based preemptive scheduling
- Processes: Creation, termination, yield, sleep
- Max processes: Configurable (MAX_PROCESSES)
Hardware Drivers
- ATA/IDE disk
- PS/2 keyboard and mouse
- PCI enumeration
- Sound (basic)
- ACPI (shutdown/reboot)
Required Additions for Python
| Feature | Status | Location | Priority |
|---|---|---|---|
| setjmp/longjmp | Missing | New file | P1 |
| Math functions | Missing | New file | P1 |
| snprintf | Missing | string.c | P1 |
| qsort | Missing | New file | P2 |
| strtol/strtoul | Missing | string.c | P1 |
| FAT write | Stubs only | fs/fat.c | P3 |
References
- MicroPython Official Site
- MicroPython GitHub Repository
- MicroPython Porting Guide
- MicroPython GitHub Discussions - Porting to New Processor
- MicroPython Wiki FAQ
Document History
| Version | Date | Author | Changes |
|---|---|---|---|
| 1.0 | January 2026 | Planning Team | Initial document |