diff --git a/examples/financial-sim/CMakeLists.txt b/examples/financial-sim/CMakeLists.txt index 6e2ed43e3d19..33429bec4fc7 100644 --- a/examples/financial-sim/CMakeLists.txt +++ b/examples/financial-sim/CMakeLists.txt @@ -35,6 +35,24 @@ add_executable(${VDEV_TEST_TARGET} target_link_libraries(${VDEV_TEST_TARGET} PRIVATE ${CMAKE_THREAD_LIBS_INIT}) target_compile_features(${VDEV_TEST_TARGET} PRIVATE cxx_std_17) +# Financial PCI protocol model and ABI tests (no Bochs or kernel headers required) +set(FINANCIAL_PCI_MODEL_TARGET test-financial-pci-model) +add_executable(${FINANCIAL_PCI_MODEL_TARGET} + bochs/test-financial-pci-model.cpp + bochs/financial-pci-model.cpp +) +target_include_directories(${FINANCIAL_PCI_MODEL_TARGET} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/bochs +) +target_compile_features(${FINANCIAL_PCI_MODEL_TARGET} PRIVATE cxx_std_17) + +set(FINANCIAL_PCI_ABI_TARGET test-financial-pci-abi) +add_executable(${FINANCIAL_PCI_ABI_TARGET} bochs/test-financial-pci-abi.c) +target_include_directories(${FINANCIAL_PCI_ABI_TARGET} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/bochs +) +target_compile_features(${FINANCIAL_PCI_ABI_TARGET} PRIVATE c_std_11) + # Test for financial logic (no model required) set(TEST_TARGET test-financial-logic) add_executable(${TEST_TARGET} test-financial-logic.cpp) diff --git a/examples/financial-sim/bochs/README.md b/examples/financial-sim/bochs/README.md new file mode 100644 index 000000000000..667f2ee95bd8 --- /dev/null +++ b/examples/financial-sim/bochs/README.md @@ -0,0 +1,137 @@ +# GGNuCash PCI device MVP + +This directory defines a small, versioned PCI I/O BAR interface for guest +software. It includes a standalone register-level reference model, a Linux PCI +driver, a userspace utility, and ABI/model tests. It does not change or depend +on the existing in-process `VirtualPCB` simulator. + +## Integration boundary + +There is no Bochs source tree, device registration configuration, or Bochs +build target in this repository. `FinancialPciModel` is the register behavior +reference, not a Bochs `bx_pci_device_c` plugin. A Bochs-specific PCI device +adapter and its plugin/build registration still need to be added against the +exact Bochs revision selected by the integrator. The adapter must implement +the register contract below and run the same protocol tests. No device should +be advertised in a guest until that adapter is installed. + +The PCI identity `1234:4747` is a local Bochs-development placeholder, not an +assigned GGNuCash vendor identity. Keep it confined to test VMs and replace it +with an appropriately assigned identity before distributing a device or +driver. The Linux module matches only that identity. + +## Guest-facing contract, ABI version 1 + +The device exposes BAR 0 as a 32-bit PCI I/O BAR of at least `0x120` bytes. +Registers and record data use little-endian dwords. Register accesses must be +aligned 32-bit reads or writes; unsupported accesses read as all ones or are +ignored. The initial implementation uses programmed I/O only: no DMA, PCI +interrupts, MSI/MSI-X, or asynchronous queue. + +| Offset | Register | Access and meaning | +| --- | --- | --- | +| `0x00` | Device ID | Read-only `0x434e4747` (`GGNC`) | +| `0x04` | ABI version | Read-only `1` | +| `0x08` | Capabilities | Read-only; bit 0 means PIO transaction submission | +| `0x0c` | Status | Read-only; bit 0 ready, bit 1 busy, bit 2 error | +| `0x10` | Command | Write `1` to submit or `2` to reset | +| `0x14` | Length | Read/write payload length in bytes | +| `0x18` | Accepted transaction count | Read-only 32-bit count | +| `0x1c` | Error | Read-only device error code | +| `0x20`–`0x11f` | Data window | Read/write 256-byte payload window | + +A submit payload is exactly one `ggnucash_transaction_record` (40 bytes). +Version 1 contains a nonzero transaction ID, distinct nonzero debit/credit +account codes, a positive amount in minor currency units, and a numeric +ISO-4217 currency code from 1 through 999. The record's version and size must +match the ABI; its reserved field must be zero. Records are submitted as ten +aligned dword writes to the data window, followed by the length and submit +command. An accepted record increments the volatile count; this device does +not post, persist, or reconcile accounting entries. + +Error codes are `0` none, `1` unknown command, `2` wrong payload length, +`3` invalid record, and `4` count overflow. A failed command sets the status +error bit and does not increment the accepted count. Writing zero to the error +register clears the error. Reset clears the status error, count, length, and +payload. The contract is bounded to one serialized submission at a time. + +The ioctl ABI is defined by +`include/ggnucash/financial_pci.h`. `GET_INFO` returns the version, +capabilities, status, error, and accepted count. `SUBMIT` validates and sends +one transaction record and returns the resulting status, error, and count. +The ioctl structures contain only fixed-width fields and no user pointers. + +## Linux guest driver and utility + +Build the out-of-tree module and userspace utility inside the Linux guest with +matching kernel headers: + +```sh +cd examples/financial-sim/bochs/kernel +make +sudo insmod ggnucash_pci.ko + +cd ../userspace +make +``` + +The driver verifies the PCI identity, BAR type/size, device ID, ABI version, +and PIO capability before registering a mode-`0600` device node named +`/dev/ggnucash-`. The kernel driver serializes requests, bounds +the payload to the one fixed-size record, polls for completion with a timeout, +and rejects records that fail the shared ABI checks. It uses no DMA or +interrupts. On removal, existing file descriptors are kept safe and receive +`ENODEV` rather than accessing a removed BAR. + +Inspect the interface or submit a sample transaction: + +```sh +./ggnucashctl info /dev/ggnucash-0000:00:03.0 +./ggnucashctl submit /dev/ggnucash-0000:00:03.0 1 1101 4100 25000 840 +``` + +The final argument is the numeric currency code (for example, `840` for USD); +the amount is an integer number of minor units, never a floating-point value. +The device node is restricted to root by default. Administrators may grant +access to a dedicated group through normal device-node/udev policy. + +## Build and test in this repository + +The host-side tests do not require Bochs or Linux kernel headers: + +```sh +cmake -B build +cmake --build build --target test-financial-pci-model test-financial-pci-abi +./build/bin/test-financial-pci-model +./build/bin/test-financial-pci-abi +``` + +Build the Linux module and utility only inside a Linux guest with the +corresponding kernel headers: + +```sh +make -C examples/financial-sim/bochs/kernel +make -C examples/financial-sim/bochs/userspace +``` + +The existing simulator remains independently testable: + +```sh +cmake --build build --target test-virtual-device +./build/bin/test-virtual-device +``` + +## Compatibility and delivery checklist + +- Do not change the meaning or offsets of ABI v1 registers or fields. Add new + behavior through capability bits or a new ABI version. +- Keep the ioctl structures fixed-width and pointer-free; add compile-time + layout assertions whenever the ABI changes. +- Validate the same record constraints in the Bochs adapter, Linux driver, + and host-side reference-model tests. +- Before a release, select and pin a Bochs revision, implement and build its + native PCI adapter, install it in a test VM, and verify guest discovery, + driver probe, successful and invalid submissions, timeout/error handling, + reset, repeated bind/unbind, and removal with an open file. +- Test the driver against each supported guest kernel. The host CMake test + targets do not build or validate the kernel module or Bochs adapter. diff --git a/examples/financial-sim/bochs/financial-pci-model.cpp b/examples/financial-sim/bochs/financial-pci-model.cpp new file mode 100644 index 000000000000..72591aa4d0e0 --- /dev/null +++ b/examples/financial-sim/bochs/financial-pci-model.cpp @@ -0,0 +1,154 @@ +#include "financial-pci-model.h" + +#include +#include + +namespace ggnucash { +namespace bochs { + +namespace { + +uint32_t load_le32(const uint8_t *bytes) +{ + return static_cast(bytes[0]) | + (static_cast(bytes[1]) << 8) | + (static_cast(bytes[2]) << 16) | + (static_cast(bytes[3]) << 24); +} + +uint64_t load_le64(const uint8_t *bytes) +{ + return static_cast(load_le32(bytes)) | + (static_cast(load_le32(bytes + 4)) << 32); +} + +int64_t signed_le64(const uint8_t *bytes) +{ + const uint64_t value = load_le64(bytes); + int64_t result; + std::memcpy(&result, &value, sizeof(result)); + return result; +} + +} // namespace + +FinancialPciModel::FinancialPciModel() +{ + reset(); +} + +uint32_t FinancialPciModel::read(uint32_t offset, unsigned width) const +{ + if (width != 4 || (offset & 3) != 0 || offset > GGNUCASH_PCI_BAR_SIZE - 4) { + return 0xffffffff; + } + + switch (offset) { + case GGNUCASH_PCI_REG_ID: + return GGNUCASH_PCI_ID; + case GGNUCASH_PCI_REG_ABI_VERSION: + return GGNUCASH_PCI_ABI_VERSION; + case GGNUCASH_PCI_REG_CAPABILITIES: + return GGNUCASH_PCI_CAP_PIO_TX; + case GGNUCASH_PCI_REG_STATUS: + return status; + case GGNUCASH_PCI_REG_LENGTH: + return length; + case GGNUCASH_PCI_REG_TX_COUNT: + return transaction_count; + case GGNUCASH_PCI_REG_ERROR: + return error; + default: + if (offset >= GGNUCASH_PCI_REG_DATA && + offset < GGNUCASH_PCI_REG_DATA + GGNUCASH_PCI_MAX_RECORD_SIZE) { + return load_le32(data.data() + offset - GGNUCASH_PCI_REG_DATA); + } + return 0; + } +} + +void FinancialPciModel::write(uint32_t offset, uint32_t value, unsigned width) +{ + if (width != 4 || (offset & 3) != 0 || offset > GGNUCASH_PCI_BAR_SIZE - 4) { + return; + } + + switch (offset) { + case GGNUCASH_PCI_REG_COMMAND: + if (value == GGNUCASH_PCI_COMMAND_RESET) { + reset(); + } else if (value == GGNUCASH_PCI_COMMAND_SUBMIT) { + submit(); + } else { + error = GGNUCASH_PCI_ERROR_BAD_COMMAND; + status = GGNUCASH_PCI_STATUS_READY | GGNUCASH_PCI_STATUS_ERROR; + } + break; + case GGNUCASH_PCI_REG_LENGTH: + length = value; + break; + case GGNUCASH_PCI_REG_ERROR: + if (value == GGNUCASH_PCI_ERROR_NONE) { + error = GGNUCASH_PCI_ERROR_NONE; + status = GGNUCASH_PCI_STATUS_READY; + } + break; + default: + if (offset >= GGNUCASH_PCI_REG_DATA && + offset < GGNUCASH_PCI_REG_DATA + GGNUCASH_PCI_MAX_RECORD_SIZE) { + const uint32_t index = offset - GGNUCASH_PCI_REG_DATA; + data[index] = static_cast(value); + data[index + 1] = static_cast(value >> 8); + data[index + 2] = static_cast(value >> 16); + data[index + 3] = static_cast(value >> 24); + } + break; + } +} + +void FinancialPciModel::reset() +{ + status = GGNUCASH_PCI_STATUS_READY; + length = 0; + error = GGNUCASH_PCI_ERROR_NONE; + transaction_count = 0; + data.fill(0); +} + +void FinancialPciModel::submit() +{ + status = GGNUCASH_PCI_STATUS_BUSY; + error = GGNUCASH_PCI_ERROR_NONE; + + if (length != sizeof(struct ggnucash_transaction_record) || + length > GGNUCASH_PCI_MAX_RECORD_SIZE) { + error = GGNUCASH_PCI_ERROR_BAD_LENGTH; + } else { + struct ggnucash_transaction_record record = {}; + record.version = load_le32(data.data()); + record.size = load_le32(data.data() + 4); + record.transaction_id = load_le64(data.data() + 8); + record.debit_account = load_le32(data.data() + 16); + record.credit_account = load_le32(data.data() + 20); + const uint64_t amount_bits = load_le64(data.data() + 24); + std::memcpy(&record.amount_minor, &amount_bits, sizeof(record.amount_minor)); + record.currency_numeric = load_le32(data.data() + 32); + record.reserved = load_le32(data.data() + 36); + + if (!ggnucash_transaction_record_valid(&record)) { + error = GGNUCASH_PCI_ERROR_BAD_RECORD; + } else if (transaction_count == std::numeric_limits::max()) { + error = GGNUCASH_PCI_ERROR_COUNT_OVERFLOW; + } else { + ++transaction_count; + } + } + + status = GGNUCASH_PCI_STATUS_READY; + if (error != GGNUCASH_PCI_ERROR_NONE) { + status |= GGNUCASH_PCI_STATUS_ERROR; + } +} + +} // namespace bochs +} // namespace ggnucash diff --git a/examples/financial-sim/bochs/financial-pci-model.h b/examples/financial-sim/bochs/financial-pci-model.h new file mode 100644 index 000000000000..ccc147068955 --- /dev/null +++ b/examples/financial-sim/bochs/financial-pci-model.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +#include "include/ggnucash/financial_pci.h" + +namespace ggnucash { +namespace bochs { + +class FinancialPciModel { +public: + FinancialPciModel(); + + uint32_t read(uint32_t offset, unsigned width = 4) const; + void write(uint32_t offset, uint32_t value, unsigned width = 4); + void reset(); + +private: + void submit(); + uint32_t status; + uint32_t length; + uint32_t error; + uint32_t transaction_count; + std::array data; +}; + +} // namespace bochs +} // namespace ggnucash diff --git a/examples/financial-sim/bochs/include/ggnucash/financial_pci.h b/examples/financial-sim/bochs/include/ggnucash/financial_pci.h new file mode 100644 index 000000000000..6b3d4473ae36 --- /dev/null +++ b/examples/financial-sim/bochs/include/ggnucash/financial_pci.h @@ -0,0 +1,87 @@ +#ifndef GGNUCASH_FINANCIAL_PCI_H +#define GGNUCASH_FINANCIAL_PCI_H + +#include +#include + +#define GGNUCASH_PCI_VENDOR_ID 0x1234 +#define GGNUCASH_PCI_DEVICE_ID 0x4747 + +#define GGNUCASH_PCI_ABI_VERSION 1 +#define GGNUCASH_PCI_BAR 0 +#define GGNUCASH_PCI_BAR_SIZE 0x120 +#define GGNUCASH_PCI_MAX_RECORD_SIZE 0x100 + +#define GGNUCASH_PCI_REG_ID 0x00 +#define GGNUCASH_PCI_REG_ABI_VERSION 0x04 +#define GGNUCASH_PCI_REG_CAPABILITIES 0x08 +#define GGNUCASH_PCI_REG_STATUS 0x0c +#define GGNUCASH_PCI_REG_COMMAND 0x10 +#define GGNUCASH_PCI_REG_LENGTH 0x14 +#define GGNUCASH_PCI_REG_TX_COUNT 0x18 +#define GGNUCASH_PCI_REG_ERROR 0x1c +#define GGNUCASH_PCI_REG_DATA 0x20 + +#define GGNUCASH_PCI_ID 0x434e4747 + +#define GGNUCASH_PCI_CAP_PIO_TX (1U << 0) + +#define GGNUCASH_PCI_STATUS_READY (1U << 0) +#define GGNUCASH_PCI_STATUS_BUSY (1U << 1) +#define GGNUCASH_PCI_STATUS_ERROR (1U << 2) + +#define GGNUCASH_PCI_COMMAND_SUBMIT 1 +#define GGNUCASH_PCI_COMMAND_RESET 2 + +#define GGNUCASH_PCI_ERROR_NONE 0 +#define GGNUCASH_PCI_ERROR_BAD_COMMAND 1 +#define GGNUCASH_PCI_ERROR_BAD_LENGTH 2 +#define GGNUCASH_PCI_ERROR_BAD_RECORD 3 +#define GGNUCASH_PCI_ERROR_COUNT_OVERFLOW 4 + +struct ggnucash_transaction_record { + __u32 version; + __u32 size; + __u64 transaction_id; + __u32 debit_account; + __u32 credit_account; + __s64 amount_minor; + __u32 currency_numeric; + __u32 reserved; +}; + +struct ggnucash_device_info { + __u32 abi_version; + __u32 capabilities; + __u32 status; + __u32 error; + __u64 transaction_count; +}; + +struct ggnucash_submit { + struct ggnucash_transaction_record record; + __u32 status; + __u32 error; + __u64 transaction_count; +}; + +#define GGNUCASH_IOCTL_TYPE 'G' +#define GGNUCASH_IOCTL_GET_INFO _IOR(GGNUCASH_IOCTL_TYPE, 0x00, struct ggnucash_device_info) +#define GGNUCASH_IOCTL_SUBMIT _IOWR(GGNUCASH_IOCTL_TYPE, 0x01, struct ggnucash_submit) + +static inline int ggnucash_transaction_record_valid(const struct ggnucash_transaction_record *record) +{ + return record != 0 && + record->version == GGNUCASH_PCI_ABI_VERSION && + record->size == sizeof(*record) && + record->transaction_id != 0 && + record->debit_account != 0 && + record->credit_account != 0 && + record->debit_account != record->credit_account && + record->amount_minor > 0 && + record->currency_numeric > 0 && + record->currency_numeric <= 999 && + record->reserved == 0; +} + +#endif diff --git a/examples/financial-sim/bochs/kernel/Makefile b/examples/financial-sim/bochs/kernel/Makefile new file mode 100644 index 000000000000..bdf9568f8466 --- /dev/null +++ b/examples/financial-sim/bochs/kernel/Makefile @@ -0,0 +1,9 @@ +obj-m += ggnucash_pci.o +ccflags-y += -I$(src)/../include + +.PHONY: all clean +all: + $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) modules + +clean: + $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) clean diff --git a/examples/financial-sim/bochs/kernel/ggnucash_pci.c b/examples/financial-sim/bochs/kernel/ggnucash_pci.c new file mode 100644 index 000000000000..2667adb15dc2 --- /dev/null +++ b/examples/financial-sim/bochs/kernel/ggnucash_pci.c @@ -0,0 +1,284 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "ggnucash/financial_pci.h" + +#define GGNUCASH_PCI_TIMEOUT_US 1000000 + +struct ggnucash_pci { + struct pci_dev *pdev; + void __iomem *bar; + struct miscdevice misc; + struct kref references; + struct mutex lock; + char misc_name[32]; + bool removed; +}; + +static const struct pci_device_id ggnucash_pci_ids[] = { + { PCI_DEVICE(GGNUCASH_PCI_VENDOR_ID, GGNUCASH_PCI_DEVICE_ID) }, + { } +}; +MODULE_DEVICE_TABLE(pci, ggnucash_pci_ids); + +static void ggnucash_release_device(struct kref *references); + +static long ggnucash_get_info(struct ggnucash_pci *device, unsigned long arg) +{ + struct ggnucash_device_info info; + + if (device->removed) { + return -ENODEV; + } + info.abi_version = ioread32(device->bar + GGNUCASH_PCI_REG_ABI_VERSION); + info.capabilities = ioread32(device->bar + GGNUCASH_PCI_REG_CAPABILITIES); + info.status = ioread32(device->bar + GGNUCASH_PCI_REG_STATUS); + info.error = ioread32(device->bar + GGNUCASH_PCI_REG_ERROR); + info.transaction_count = ioread32(device->bar + GGNUCASH_PCI_REG_TX_COUNT); + + if (copy_to_user((void __user *) arg, &info, sizeof(info))) { + return -EFAULT; + } + return 0; +} + +static void ggnucash_write_record(struct ggnucash_pci *device, + const struct ggnucash_transaction_record *record) +{ + const u32 words[] = { + record->version, + record->size, + lower_32_bits(record->transaction_id), + upper_32_bits(record->transaction_id), + record->debit_account, + record->credit_account, + lower_32_bits((u64) record->amount_minor), + upper_32_bits((u64) record->amount_minor), + record->currency_numeric, + record->reserved, + }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(words); ++i) { + iowrite32(words[i], device->bar + GGNUCASH_PCI_REG_DATA + i * sizeof(u32)); + } +} + +static long ggnucash_submit(struct ggnucash_pci *device, unsigned long arg) +{ + struct ggnucash_submit submit; + u32 status; + int ret; + + if (copy_from_user(&submit, (void __user *) arg, sizeof(submit))) { + return -EFAULT; + } + if (!ggnucash_transaction_record_valid(&submit.record)) { + return -EINVAL; + } + + mutex_lock(&device->lock); + if (device->removed) { + ret = -ENODEV; + goto out_unlock; + } + status = ioread32(device->bar + GGNUCASH_PCI_REG_STATUS); + if (!(status & GGNUCASH_PCI_STATUS_READY) || (status & GGNUCASH_PCI_STATUS_BUSY)) { + ret = -EBUSY; + goto out_unlock; + } + + iowrite32(sizeof(submit.record), device->bar + GGNUCASH_PCI_REG_LENGTH); + ggnucash_write_record(device, &submit.record); + iowrite32(GGNUCASH_PCI_COMMAND_SUBMIT, device->bar + GGNUCASH_PCI_REG_COMMAND); + + ret = read_poll_timeout(ioread32, status, + !(status & GGNUCASH_PCI_STATUS_BUSY), + 1000, GGNUCASH_PCI_TIMEOUT_US, false, + device->bar + GGNUCASH_PCI_REG_STATUS); + if (ret) { + goto out_unlock; + } + + submit.status = status; + submit.error = ioread32(device->bar + GGNUCASH_PCI_REG_ERROR); + submit.transaction_count = ioread32(device->bar + GGNUCASH_PCI_REG_TX_COUNT); + if (copy_to_user((void __user *) arg, &submit, sizeof(submit))) { + ret = -EFAULT; + goto out_unlock; + } + if (status & GGNUCASH_PCI_STATUS_ERROR) { + ret = -EIO; + } + +out_unlock: + mutex_unlock(&device->lock); + return ret; +} + +static long ggnucash_ioctl(struct file *file, unsigned int command, unsigned long arg) +{ + struct ggnucash_pci *device = file->private_data; + long ret; + + switch (command) { + case GGNUCASH_IOCTL_GET_INFO: + mutex_lock(&device->lock); + ret = ggnucash_get_info(device, arg); + mutex_unlock(&device->lock); + return ret; + case GGNUCASH_IOCTL_SUBMIT: + return ggnucash_submit(device, arg); + default: + return -ENOTTY; + } +} + +static int ggnucash_open(struct inode *inode, struct file *file) +{ + struct miscdevice *misc = file->private_data; + struct ggnucash_pci *device = container_of(misc, struct ggnucash_pci, misc); + int ret; + + mutex_lock(&device->lock); + if (device->removed) { + mutex_unlock(&device->lock); + return -ENODEV; + } + kref_get(&device->references); + mutex_unlock(&device->lock); + file->private_data = device; + ret = nonseekable_open(inode, file); + if (ret) { + kref_put(&device->references, ggnucash_release_device); + } + return ret; +} + +static void ggnucash_release_device(struct kref *references) +{ + struct ggnucash_pci *device = container_of(references, struct ggnucash_pci, references); + + kfree(device); +} + +static int ggnucash_release_file(struct inode *inode, struct file *file) +{ + struct ggnucash_pci *device = file->private_data; + + kref_put(&device->references, ggnucash_release_device); + return 0; +} + +static const struct file_operations ggnucash_fops = { + .owner = THIS_MODULE, + .open = ggnucash_open, + .release = ggnucash_release_file, + .unlocked_ioctl = ggnucash_ioctl, + .compat_ioctl = ggnucash_ioctl, + .llseek = noop_llseek, +}; + +static int ggnucash_probe(struct pci_dev *pdev, const struct pci_device_id *id) +{ + struct ggnucash_pci *device; + u32 capabilities; + int ret; + + ret = pci_enable_device(pdev); + if (ret) { + return ret; + } + if (!(pci_resource_flags(pdev, GGNUCASH_PCI_BAR) & IORESOURCE_IO) || + pci_resource_len(pdev, GGNUCASH_PCI_BAR) < GGNUCASH_PCI_BAR_SIZE) { + ret = -ENODEV; + goto err_disable; + } + ret = pci_request_region(pdev, GGNUCASH_PCI_BAR, "ggnucash-pci"); + if (ret) { + goto err_disable; + } + + device = kzalloc(sizeof(*device), GFP_KERNEL); + if (!device) { + ret = -ENOMEM; + goto err_release_region; + } + device->pdev = pdev; + kref_init(&device->references); + mutex_init(&device->lock); + device->bar = pci_iomap(pdev, GGNUCASH_PCI_BAR, GGNUCASH_PCI_BAR_SIZE); + if (!device->bar) { + ret = -ENOMEM; + goto err_free_device; + } + + if (ioread32(device->bar + GGNUCASH_PCI_REG_ID) != GGNUCASH_PCI_ID || + ioread32(device->bar + GGNUCASH_PCI_REG_ABI_VERSION) != GGNUCASH_PCI_ABI_VERSION) { + ret = -ENODEV; + goto err_iounmap; + } + capabilities = ioread32(device->bar + GGNUCASH_PCI_REG_CAPABILITIES); + if (!(capabilities & GGNUCASH_PCI_CAP_PIO_TX)) { + ret = -ENODEV; + goto err_iounmap; + } + + snprintf(device->misc_name, sizeof(device->misc_name), "ggnucash-%s", + pci_name(pdev)); + device->misc.minor = MISC_DYNAMIC_MINOR; + device->misc.name = device->misc_name; + device->misc.fops = &ggnucash_fops; + device->misc.parent = &pdev->dev; + device->misc.mode = 0600; + + ret = misc_register(&device->misc); + if (ret) { + goto err_iounmap; + } + pci_set_drvdata(pdev, device); + dev_info(&pdev->dev, "registered /dev/%s (ABI %u)\n", + device->misc_name, GGNUCASH_PCI_ABI_VERSION); + return 0; + +err_iounmap: + pci_iounmap(pdev, device->bar); +err_free_device: + kfree(device); +err_release_region: + pci_release_region(pdev, GGNUCASH_PCI_BAR); +err_disable: + pci_disable_device(pdev); + return ret; +} + +static void ggnucash_remove(struct pci_dev *pdev) +{ + struct ggnucash_pci *device = pci_get_drvdata(pdev); + + misc_deregister(&device->misc); + mutex_lock(&device->lock); + device->removed = true; + pci_iounmap(pdev, device->bar); + pci_release_region(pdev, GGNUCASH_PCI_BAR); + pci_disable_device(pdev); + mutex_unlock(&device->lock); + kref_put(&device->references, ggnucash_release_device); +} + +static struct pci_driver ggnucash_driver = { + .name = "ggnucash-pci", + .id_table = ggnucash_pci_ids, + .probe = ggnucash_probe, + .remove = ggnucash_remove, +}; +module_pci_driver(ggnucash_driver); + +MODULE_AUTHOR("GGNuCash contributors"); +MODULE_DESCRIPTION("GGNuCash financial PCI device driver"); +MODULE_LICENSE("GPL"); diff --git a/examples/financial-sim/bochs/test-financial-pci-abi.c b/examples/financial-sim/bochs/test-financial-pci-abi.c new file mode 100644 index 000000000000..ecb26fee3909 --- /dev/null +++ b/examples/financial-sim/bochs/test-financial-pci-abi.c @@ -0,0 +1,35 @@ +#include +#include + +#include "include/ggnucash/financial_pci.h" + +_Static_assert(sizeof(struct ggnucash_transaction_record) == 40, "transaction ABI size changed"); +_Static_assert(offsetof(struct ggnucash_transaction_record, transaction_id) == 8, + "transaction_id ABI offset changed"); +_Static_assert(offsetof(struct ggnucash_transaction_record, amount_minor) == 24, + "amount_minor ABI offset changed"); +_Static_assert(sizeof(struct ggnucash_device_info) == 24, "device info ABI size changed"); +_Static_assert(sizeof(struct ggnucash_submit) == 56, "submit ioctl ABI size changed"); + +int main(void) +{ + struct ggnucash_transaction_record record = { + .version = GGNUCASH_PCI_ABI_VERSION, + .size = sizeof(record), + .transaction_id = 1, + .debit_account = 1101, + .credit_account = 4100, + .amount_minor = 100, + .currency_numeric = 840, + }; + + if (!ggnucash_transaction_record_valid(&record)) { + return 1; + } + record.credit_account = record.debit_account; + if (ggnucash_transaction_record_valid(&record)) { + return 1; + } + puts("Financial PCI ABI tests passed"); + return 0; +} diff --git a/examples/financial-sim/bochs/test-financial-pci-model.cpp b/examples/financial-sim/bochs/test-financial-pci-model.cpp new file mode 100644 index 000000000000..3bc0498dff15 --- /dev/null +++ b/examples/financial-sim/bochs/test-financial-pci-model.cpp @@ -0,0 +1,87 @@ +#include "financial-pci-model.h" + +#include +#include + +namespace { + +void expect(bool condition, const char *message) +{ + if (!condition) { + std::cerr << "FAIL: " << message << '\n'; + std::exit(1); + } +} + +void write_record(ggnucash::bochs::FinancialPciModel &device, + const ggnucash_transaction_record &record) +{ + const uint32_t words[] = { + record.version, + record.size, + static_cast(record.transaction_id), + static_cast(record.transaction_id >> 32), + record.debit_account, + record.credit_account, + static_cast(record.amount_minor), + static_cast(static_cast(record.amount_minor) >> 32), + record.currency_numeric, + record.reserved, + }; + + device.write(GGNUCASH_PCI_REG_LENGTH, sizeof(record)); + for (uint32_t i = 0; i < sizeof(words) / sizeof(words[0]); ++i) { + device.write(GGNUCASH_PCI_REG_DATA + i * 4, words[i]); + } +} + +} // namespace + +int main() +{ + using ggnucash::bochs::FinancialPciModel; + FinancialPciModel device; + + expect(device.read(GGNUCASH_PCI_REG_ID) == GGNUCASH_PCI_ID, "device ID"); + expect(device.read(GGNUCASH_PCI_REG_ABI_VERSION) == GGNUCASH_PCI_ABI_VERSION, "ABI version"); + expect(device.read(GGNUCASH_PCI_REG_CAPABILITIES) == GGNUCASH_PCI_CAP_PIO_TX, "PIO capability"); + expect(device.read(GGNUCASH_PCI_REG_STATUS) == GGNUCASH_PCI_STATUS_READY, "initial ready status"); + expect(device.read(GGNUCASH_PCI_REG_ID, 2) == 0xffffffff, "reject non-dword reads"); + + ggnucash_transaction_record record = {}; + record.version = GGNUCASH_PCI_ABI_VERSION; + record.size = sizeof(record); + record.transaction_id = 1; + record.debit_account = 1101; + record.credit_account = 4100; + record.amount_minor = 25000; + record.currency_numeric = 840; + + write_record(device, record); + device.write(GGNUCASH_PCI_REG_COMMAND, GGNUCASH_PCI_COMMAND_SUBMIT); + expect(device.read(GGNUCASH_PCI_REG_TX_COUNT) == 1, "valid transaction increments count"); + expect(device.read(GGNUCASH_PCI_REG_STATUS) == GGNUCASH_PCI_STATUS_READY, "successful submit completes"); + + device.write(GGNUCASH_PCI_REG_LENGTH, sizeof(record) - 4); + device.write(GGNUCASH_PCI_REG_COMMAND, GGNUCASH_PCI_COMMAND_SUBMIT); + expect(device.read(GGNUCASH_PCI_REG_ERROR) == GGNUCASH_PCI_ERROR_BAD_LENGTH, "bad length is reported"); + expect(device.read(GGNUCASH_PCI_REG_STATUS) == + (GGNUCASH_PCI_STATUS_READY | GGNUCASH_PCI_STATUS_ERROR), "bad length sets error status"); + expect(device.read(GGNUCASH_PCI_REG_TX_COUNT) == 1, "invalid transaction does not increment count"); + + write_record(device, record); + device.write(GGNUCASH_PCI_REG_DATA + 20, record.debit_account); + device.write(GGNUCASH_PCI_REG_COMMAND, GGNUCASH_PCI_COMMAND_SUBMIT); + expect(device.read(GGNUCASH_PCI_REG_ERROR) == GGNUCASH_PCI_ERROR_BAD_RECORD, "invalid record is rejected"); + + device.write(GGNUCASH_PCI_REG_COMMAND, 0xffffffff); + expect(device.read(GGNUCASH_PCI_REG_ERROR) == GGNUCASH_PCI_ERROR_BAD_COMMAND, "unknown command is reported"); + + device.write(GGNUCASH_PCI_REG_COMMAND, GGNUCASH_PCI_COMMAND_RESET); + expect(device.read(GGNUCASH_PCI_REG_TX_COUNT) == 0, "reset clears transaction count"); + expect(device.read(GGNUCASH_PCI_REG_ERROR) == GGNUCASH_PCI_ERROR_NONE, "reset clears errors"); + expect(device.read(GGNUCASH_PCI_REG_STATUS) == GGNUCASH_PCI_STATUS_READY, "reset returns ready state"); + + std::cout << "Financial PCI model tests passed\n"; + return 0; +} diff --git a/examples/financial-sim/bochs/userspace/Makefile b/examples/financial-sim/bochs/userspace/Makefile new file mode 100644 index 000000000000..c11dd4f0314a --- /dev/null +++ b/examples/financial-sim/bochs/userspace/Makefile @@ -0,0 +1,11 @@ +CC ?= cc +CFLAGS ?= -O2 -Wall -Wextra -Werror + +.PHONY: all clean +all: ggnucashctl + +ggnucashctl: ggnucashctl.c ../include/ggnucash/financial_pci.h + $(CC) $(CFLAGS) -I../include -o $@ ggnucashctl.c + +clean: + rm -f ggnucashctl diff --git a/examples/financial-sim/bochs/userspace/ggnucashctl.c b/examples/financial-sim/bochs/userspace/ggnucashctl.c new file mode 100644 index 000000000000..d92ac86de9a3 --- /dev/null +++ b/examples/financial-sim/bochs/userspace/ggnucashctl.c @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ggnucash/financial_pci.h" + +static int parse_u32(const char *text, __u32 *value) +{ + char *end = NULL; + unsigned long parsed; + + if (text[0] == '-') { + return -1; + } + errno = 0; + parsed = strtoul(text, &end, 10); + if (errno || end == text || *end != '\0' || parsed > UINT32_MAX) { + return -1; + } + *value = (__u32) parsed; + return 0; +} + +static int parse_u64(const char *text, __u64 *value) +{ + char *end = NULL; + unsigned long long parsed; + + if (text[0] == '-') { + return -1; + } + errno = 0; + parsed = strtoull(text, &end, 10); + if (errno || end == text || *end != '\0' || parsed == 0) { + return -1; + } + *value = (__u64) parsed; + return 0; +} + +static int parse_amount(const char *text, __s64 *value) +{ + char *end = NULL; + long long parsed; + + errno = 0; + parsed = strtoll(text, &end, 10); + if (errno || end == text || *end != '\0' || parsed <= 0) { + return -1; + } + *value = (__s64) parsed; + return 0; +} + +static int show_info(int fd) +{ + struct ggnucash_device_info info; + + if (ioctl(fd, GGNUCASH_IOCTL_GET_INFO, &info) < 0) { + perror("GGNUCASH_IOCTL_GET_INFO"); + return 1; + } + printf("ABI: %u\nCapabilities: 0x%08x\nStatus: 0x%08x\n" + "Error: %u\nAccepted transactions: %" PRIu64 "\n", + info.abi_version, info.capabilities, info.status, info.error, + (uint64_t) info.transaction_count); + return 0; +} + +static int submit_transaction(int fd, int argc, char **argv) +{ + struct ggnucash_submit request = { 0 }; + + if (argc != 8 || + parse_u64(argv[3], &request.record.transaction_id) || + parse_u32(argv[4], &request.record.debit_account) || + parse_u32(argv[5], &request.record.credit_account) || + parse_amount(argv[6], &request.record.amount_minor)) { + fprintf(stderr, "usage: %s submit DEVICE TX_ID DEBIT CREDIT AMOUNT_MINOR CURRENCY_NUMERIC\n", + argv[0]); + return 2; + } + if (parse_u32(argv[7], &request.record.currency_numeric)) { + fprintf(stderr, "invalid numeric currency code\n"); + return 2; + } + + request.record.version = GGNUCASH_PCI_ABI_VERSION; + request.record.size = sizeof(request.record); + if (!ggnucash_transaction_record_valid(&request.record)) { + fprintf(stderr, "transaction record is invalid\n"); + return 2; + } + + if (ioctl(fd, GGNUCASH_IOCTL_SUBMIT, &request) < 0) { + perror("GGNUCASH_IOCTL_SUBMIT"); + if (errno == EIO) { + fprintf(stderr, "device error %u, status 0x%08x\n", + request.error, request.status); + } + return 1; + } + printf("Accepted transaction %" PRIu64 "; device count=%" PRIu64 "\n", + (uint64_t) request.record.transaction_id, + (uint64_t) request.transaction_count); + return 0; +} + +int main(int argc, char **argv) +{ + int fd; + int result; + + if (argc < 3) { + fprintf(stderr, "usage: %s info DEVICE | submit DEVICE TX_ID DEBIT CREDIT AMOUNT_MINOR CURRENCY_NUMERIC\n", + argv[0]); + return 2; + } + + fd = open(argv[2], O_RDWR | O_CLOEXEC); + if (fd < 0) { + perror(argv[2]); + return 1; + } + + if (strcmp(argv[1], "info") == 0 && argc == 3) { + result = show_info(fd); + } else if (strcmp(argv[1], "submit") == 0) { + result = submit_transaction(fd, argc, argv); + } else { + fprintf(stderr, "unknown command or wrong number of arguments\n"); + result = 2; + } + + close(fd); + return result; +}