Skip to content

Development Guide for Operating System (OS)

This guide provides detailed instructions for developers working with the Motanamy Operating System (OS) platform to build system-level software, device drivers, and OS components.

Architecture Overview

The OS platform is designed for low-level system development. Key components include:

  • Kernel Framework: Core kernel development and modification tools
  • Driver Framework: Standardized driver development APIs
  • System Services Layer: Framework for building system services
  • Hardware Abstraction Layer (HAL): Cross-platform hardware interfaces
  • Build System: Integrated compilation and deployment tools

Setting Up Development Environment

Prerequisites

  • C/C++ compiler (GCC, Clang)
  • Linux kernel headers
  • Motanamy OS SDK
  • Cross-compilation tools for target architectures
  • Virtual machine or hardware for testing

Installation Steps

  1. Install OS SDK:

    bash
    # Download from Motanamy developer portal
    wget https://developer.motanamy.com/downloads/os-sdk.tar.gz
    tar -xzf os-sdk.tar.gz
    cd os-sdk
    ./install.sh
  2. Set up Build Environment:

    bash
    # Install build dependencies
    sudo apt-get install build-essential linux-headers-$(uname -r)
    # or for macOS
    brew install llvm
  3. Initialize OS Project:

    bash
    motanamy init os-module my-os-module
    cd my-os-module

Kernel Module Development

Basic Kernel Module

c
// my_module.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Example OS Kernel Module");

static int __init my_module_init(void) {
    printk(KERN_INFO "My OS Module loaded!\n");
    return 0;
}

static void __exit my_module_exit(void) {
    printk(KERN_INFO "My OS Module unloaded!\n");
}

module_init(my_module_init);
module_exit(my_module_exit);

Makefile

makefile
obj-m += my_module.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

install:
	sudo insmod my_module.ko

uninstall:
	sudo rmmod my_module

Device Driver Development

Character Device Driver

c
// my_driver.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

#define DEVICE_NAME "mydevice"
#define BUFFER_SIZE 1024

static int major_number;
static char device_buffer[BUFFER_SIZE];
static int buffer_pos = 0;

static int device_open(struct inode *inode, struct file *file) {
    printk(KERN_INFO "Device opened\n");
    return 0;
}

static int device_release(struct inode *inode, struct file *file) {
    printk(KERN_INFO "Device closed\n");
    return 0;
}

static ssize_t device_read(struct file *file, char __user *user_buffer, size_t size, loff_t *offset) {
    int bytes_read = 0;
    if (buffer_pos > 0) {
        bytes_read = min(size, (size_t)buffer_pos);
        if (copy_to_user(user_buffer, device_buffer, bytes_read)) {
            return -EFAULT;
        }
        buffer_pos = 0;
    }
    return bytes_read;
}

static ssize_t device_write(struct file *file, const char __user *user_buffer, size_t size, loff_t *offset) {
    int bytes_written = min(size, (size_t)(BUFFER_SIZE - buffer_pos));
    if (copy_from_user(device_buffer + buffer_pos, user_buffer, bytes_written)) {
        return -EFAULT;
    }
    buffer_pos += bytes_written;
    return bytes_written;
}

static struct file_operations fops = {
    .open = device_open,
    .release = device_release,
    .read = device_read,
    .write = device_write,
};

static int __init driver_init(void) {
    major_number = register_chrdev(0, DEVICE_NAME, &fops);
    if (major_number < 0) {
        printk(KERN_ALERT "Failed to register device\n");
        return major_number;
    }
    printk(KERN_INFO "Device registered with major number %d\n", major_number);
    return 0;
}

static void __exit driver_exit(void) {
    unregister_chrdev(major_number, DEVICE_NAME);
    printk(KERN_INFO "Device unregistered\n");
}

module_init(driver_init);
module_exit(driver_exit);

System Service Development

Creating a System Service

c
// my_service.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>

void signal_handler(int sig) {
    syslog(LOG_INFO, "Received signal %d, shutting down", sig);
    exit(0);
}

int main() {
    // Fork to background
    pid_t pid = fork();
    if (pid < 0) exit(1);
    if (pid > 0) exit(0);

    // Set up logging
    openlog("my_service", LOG_PID, LOG_DAEMON);

    // Handle signals
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);

    syslog(LOG_INFO, "Service started");

    while (1) {
        // Service logic here
        sleep(60);
        syslog(LOG_INFO, "Service heartbeat");
    }

    closelog();
    return 0;
}

Systemd Service File

ini
[Unit]
Description=My OS Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/my_service
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Hardware Abstraction

HAL Interface

c
// hal.h
#ifndef HAL_H
#define HAL_H

typedef struct {
    int (*init)(void);
    void (*cleanup)(void);
    int (*read)(void *buffer, size_t size);
    int (*write)(const void *buffer, size_t size);
} hal_device_t;

hal_device_t *hal_get_device(const char *name);

#endif

HAL Implementation

c
// hal_linux.c
#include "hal.h"
#include <fcntl.h>
#include <unistd.h>

static int linux_init(void) {
    // Linux-specific initialization
    return 0;
}

static void linux_cleanup(void) {
    // Cleanup
}

static int linux_read(void *buffer, size_t size) {
    int fd = open("/dev/mydevice", O_RDONLY);
    if (fd < 0) return -1;
    int result = read(fd, buffer, size);
    close(fd);
    return result;
}

static int linux_write(const void *buffer, size_t size) {
    int fd = open("/dev/mydevice", O_WRONLY);
    if (fd < 0) return -1;
    int result = write(fd, buffer, size);
    close(fd);
    return result;
}

static hal_device_t linux_device = {
    .init = linux_init,
    .cleanup = linux_cleanup,
    .read = linux_read,
    .write = linux_write
};

hal_device_t *hal_get_device(const char *name) {
    if (strcmp(name, "linux") == 0) {
        return &linux_device;
    }
    return NULL;
}

Building and Testing

Compilation

bash
# Compile kernel module
make

# Cross-compile for different architectures
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-

Testing

bash
# Load module
sudo insmod my_module.ko

# Check logs
dmesg | tail

# Test device
echo "test" > /dev/mydevice
cat /dev/mydevice

# Unload module
sudo rmmod my_module

Debugging

bash
# Enable kernel debugging
echo 1 > /sys/kernel/debug/tracing/tracing_on
cat /sys/kernel/debug/tracing/trace

# Use gdb for kernel debugging
gdb vmlinux /proc/kcore

Deployment

Packaging

bash
# Create Debian package
dpkg-buildpackage -us -uc

# Create RPM package
rpmbuild -ba my-module.spec

Installation

bash
# Install kernel module
sudo make modules_install
sudo depmod

# Install service
sudo cp my_service /usr/local/bin/
sudo cp my-service.service /etc/systemd/system/
sudo systemctl enable my-service
sudo systemctl start my-service

Best Practices

Memory Management

  • Always check return values from memory allocation
  • Use appropriate memory barriers for SMP systems
  • Avoid memory leaks in kernel code

Thread Safety

  • Use appropriate locking mechanisms (spinlocks, mutexes)
  • Avoid deadlocks by consistent lock ordering
  • Use RCU for read-mostly data

Error Handling

  • Check all return values
  • Use goto for cleanup in error paths
  • Log errors appropriately

Performance

  • Minimize context switches
  • Use appropriate data structures
  • Profile and optimize critical paths

Security Considerations

  • Validate all inputs
  • Use secure coding practices
  • Implement proper privilege separation
  • Keep kernel interfaces minimal

Troubleshooting

Common Issues

  • Module not loading: Check kernel version compatibility
  • Device not accessible: Verify permissions and device nodes
  • Service not starting: Check systemd logs

Debug Tools

  • strace: Trace system calls
  • perf: Performance profiling
  • crash: Kernel crash analysis
  • SystemTap: Dynamic tracing

Resources

For additional help, visit our developer community or contact support.