Fork fix: the SDIO wedge is FIXED (esp-hosted-mcu #167)
Root cause (verified against our exact IDF tree, not the community guess): the "258" in "sdio_write_task: Failed to send data: 258" is NOT a timeout (that is 263). 258 = 0x102 = ESP_ERR_INVALID_ARG. On the ESP32-P4, block- mode CMD53 writes require the SOURCE buffer to be 64-byte (cache-line) aligned; the IDF sdmmc driver rejects a misaligned source with INVALID_ARG BEFORE any bus activity. esp_hosts write loop then declares "Unrecoverable host sdio state" and reboots the whole P4. The audio TX payload is not 64-aligned, so streaming mic audio wedged on the very FIRST frame (which is exactly what we saw: listening -> instant Failed to send -> reboot). This also explains why buffer/queue/clock/retry tuning all did nothing: the write never reached the bus. And why our symptom was instant, not after ~100 writes (the community block-mode-desync theory) — it is the first misaligned buffer, every time. Fix: vendored esp_hosted 2.12.11 as an editable local component (overrides the registry copy) and bounce a misaligned TX payload through one aligned DMA scratch buffer in hosted_sdio_write_block (port_esp_hosted_host_sdio.c). TX is serialized by the bus lock so a single static bounce buffer is safe; freed in hosted_sdio_deinit. Host-only change — no C6 reflash. VERIFIED ON HARDWARE (autonomous self-test): 40s of continuous mic-audio upstream streaming — the traffic that previously wedged on the first frame — ran clean, zero timeouts, zero reboots. A guarded SDIO_TX_SELFTEST harness is kept (compiled out) for future SDIO stress testing. Credit: root cause + patch designed via multi-agent investigation; the precise 258=INVALID_ARG decode (correcting the upstream community timeout assumption) came from checking our actual esp_err.h. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef __MEMPOOL_H__
|
||||
#define __MEMPOOL_H__
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/queue.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#define MEMPOOL_OK 0
|
||||
#define MEMPOOL_FAIL -1
|
||||
|
||||
#define MEMSET_REQUIRED 1
|
||||
#define MEMSET_NOT_REQUIRED 0
|
||||
|
||||
typedef struct hosted_mempool_t hosted_mempool_t;
|
||||
|
||||
// memory capability requested by mempool
|
||||
typedef enum {
|
||||
HOSTED_MEM_CAP_NONE, // generic memory allocation
|
||||
HOSTED_MEM_CAP_DMA, // memory allocated must be DMA capable
|
||||
HOSTED_MEM_CAP_MAX
|
||||
} hosted_mem_cap_t;
|
||||
|
||||
typedef struct {
|
||||
// pointer and size of preallocated memory to use. If NULL, mempool allocates internally
|
||||
void *pre_allocated_mem;
|
||||
size_t pre_allocated_mem_size;
|
||||
|
||||
size_t num_blocks;
|
||||
size_t block_size;
|
||||
int alignment_in_bytes;
|
||||
|
||||
// required functions to malloc, calloc, memset and free memory using capability based allocs
|
||||
void * (*malloc)(size_t size, hosted_mem_cap_t cap);
|
||||
void * (*calloc)(size_t num_elem, size_t size_elem, hosted_mem_cap_t cap);
|
||||
void * (*memset)(void *s, int c, size_t n);
|
||||
void (*free)(void *ptr);
|
||||
} hosted_mempool_config_t;
|
||||
|
||||
hosted_mempool_t * hosted_mempool_create(hosted_mempool_config_t * config);
|
||||
void hosted_mempool_destroy(struct hosted_mempool_t *mempool);
|
||||
void * hosted_mempool_alloc(struct hosted_mempool_t *mempool,
|
||||
size_t nbytes, uint8_t need_memset);
|
||||
int hosted_mempool_free(struct hosted_mempool_t *mempool, void *mem);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "mempool.h"
|
||||
#include "mempool_ll.h"
|
||||
|
||||
static const char *TAG = "HS_MP";
|
||||
|
||||
#define MEMPOOL_NAME_STR_SIZE 32
|
||||
|
||||
#define IS_MEMPOOL_ALIGNED(VAL, BYTES) (!((VAL) & (BYTES - 1)))
|
||||
#define MEMPOOL_ALIGNED(VAL, BYTES) ((VAL) + (BYTES) - \
|
||||
((VAL) & (BYTES - 1)))
|
||||
|
||||
typedef struct hosted_mempool_t {
|
||||
struct os_mempool *pool;
|
||||
uint8_t *heap;
|
||||
uint8_t static_heap;
|
||||
size_t num_blocks;
|
||||
size_t block_size;
|
||||
int alignment_bytes;
|
||||
void * (*malloc)(size_t size, hosted_mem_cap_t cap);
|
||||
void * (*calloc)(size_t num_elem, size_t size_elem, hosted_mem_cap_t cap);
|
||||
void * (*memset)(void *s, int c, size_t n);
|
||||
void (*free)(void *ptr);
|
||||
struct mempool_ops_t *ops;
|
||||
} hosted_mempool_t;
|
||||
|
||||
#define MEMPOOL_FREE(freefn, x) do { \
|
||||
if (x) { \
|
||||
freefn(x); \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
/* For Statically allocated memory, pass as pre_allocated_mem.
|
||||
* If NULL passed, will allocate from heap
|
||||
*/
|
||||
hosted_mempool_t * hosted_mempool_create(hosted_mempool_config_t * config)
|
||||
{
|
||||
if (!config ||
|
||||
!config->malloc ||
|
||||
!config->calloc ||
|
||||
!config->memset ||
|
||||
!config->free) {
|
||||
ESP_LOGE(TAG, "NULL config, or required memory functions not provided");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct hosted_mempool_t *new = NULL;
|
||||
struct os_mempool *pool = NULL;
|
||||
uint8_t *heap = NULL;
|
||||
|
||||
new = (hosted_mempool_t *)config->calloc(1, sizeof(hosted_mempool_t), HOSTED_MEM_CAP_NONE);
|
||||
if (!new) {
|
||||
ESP_LOGE(TAG, "hosted mempool init failed: no mem");
|
||||
goto free_buffs;
|
||||
}
|
||||
new->ops = os_mempool_get_ops();
|
||||
if (!new->ops) {
|
||||
ESP_LOGE(TAG, "hosted mempool init failed: no mempool ops");
|
||||
goto free_buffs;
|
||||
}
|
||||
|
||||
pool = (struct os_mempool *)config->calloc(1, sizeof(struct os_mempool), HOSTED_MEM_CAP_NONE);
|
||||
if (!pool) {
|
||||
ESP_LOGE(TAG, "pool init failed: no mem");
|
||||
goto free_buffs;
|
||||
}
|
||||
|
||||
if (!config->pre_allocated_mem) {
|
||||
/* no pre-allocated mem, allocate new */
|
||||
heap = (uint8_t *)config->malloc(MEMPOOL_ALIGNED(
|
||||
OS_MEMPOOL_BYTES(config->num_blocks, config->block_size),
|
||||
config->alignment_in_bytes),
|
||||
HOSTED_MEM_CAP_DMA);
|
||||
if (!heap) {
|
||||
ESP_LOGE(TAG, "mempool create failed: no mem\n");
|
||||
goto free_buffs;
|
||||
}
|
||||
} else {
|
||||
/* preallocated memory for mem pool */
|
||||
if (config->pre_allocated_mem_size < OS_MEMPOOL_BYTES(config->num_blocks,
|
||||
config->block_size)) {
|
||||
ESP_LOGE(TAG, "mempool create failed: insufficient memory");
|
||||
goto free_buffs;
|
||||
}
|
||||
|
||||
if (!IS_MEMPOOL_ALIGNED((unsigned long)config->pre_allocated_mem,
|
||||
config->alignment_in_bytes)) {
|
||||
ESP_LOGE(TAG, "mempool create failed: mempool start addr unaligned");
|
||||
goto free_buffs;
|
||||
}
|
||||
heap = config->pre_allocated_mem;
|
||||
}
|
||||
|
||||
char str[MEMPOOL_NAME_STR_SIZE] = {0};
|
||||
snprintf(str, MEMPOOL_NAME_STR_SIZE, "hosted_%p", pool);
|
||||
|
||||
if (new->ops->mempool_init(pool, config->num_blocks, config->block_size, heap, str)) {
|
||||
ESP_LOGE(TAG, "mempool_init failed");
|
||||
goto free_buffs;
|
||||
}
|
||||
|
||||
new->heap = heap;
|
||||
new->pool = pool;
|
||||
new->num_blocks = config->num_blocks;
|
||||
new->block_size = config->block_size;
|
||||
|
||||
if (config->pre_allocated_mem)
|
||||
new->static_heap = 1;
|
||||
|
||||
// record the alignment
|
||||
new->alignment_bytes = config->alignment_in_bytes;
|
||||
|
||||
// save the memory functions
|
||||
new->malloc = config->malloc;
|
||||
new->calloc = config->calloc;
|
||||
new->memset = config->memset;
|
||||
new->free = config->free;
|
||||
|
||||
return new;
|
||||
|
||||
free_buffs:
|
||||
MEMPOOL_FREE(config->free, new);
|
||||
MEMPOOL_FREE(config->free, pool);
|
||||
if (!config->pre_allocated_mem)
|
||||
MEMPOOL_FREE(config->free, heap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void hosted_mempool_destroy(hosted_mempool_t *mempool)
|
||||
{
|
||||
if (!mempool)
|
||||
return;
|
||||
|
||||
#if MEMPOOL_DEBUG
|
||||
ESP_LOGI(MEM_TAG, "Destroy mempool %p num_blk[%lu] blk_size:[%lu]", mempool->pool, mempool->num_blocks, mempool->block_size);
|
||||
#endif
|
||||
|
||||
mempool->ops->mempool_unregister(mempool->pool);
|
||||
MEMPOOL_FREE(mempool->free, mempool->pool);
|
||||
|
||||
if (!mempool->static_heap)
|
||||
MEMPOOL_FREE(mempool->free, mempool->heap);
|
||||
|
||||
MEMPOOL_FREE(mempool->free, mempool);
|
||||
}
|
||||
|
||||
void * hosted_mempool_alloc(hosted_mempool_t *mempool,
|
||||
size_t nbytes, uint8_t need_memset)
|
||||
{
|
||||
if (!mempool) {
|
||||
ESP_LOGE(TAG, "mempool %p is NULL", mempool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *mem = NULL;
|
||||
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_CHECK)
|
||||
assert(mempool->heap);
|
||||
assert(mempool->pool);
|
||||
#endif
|
||||
|
||||
if(nbytes > mempool->block_size) {
|
||||
ESP_LOGE(TAG, "Exp alloc bytes[%u] > mempool block size[%u]\n",
|
||||
nbytes, mempool->block_size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mem = mempool->ops->memblock_get(mempool->pool);
|
||||
if (mem && need_memset)
|
||||
mempool->memset(mem, 0, nbytes);
|
||||
|
||||
if (!mem) {
|
||||
ESP_LOGE(TAG, "mempool %p alloc failed nbytes[%u]", mempool, nbytes);
|
||||
}
|
||||
return mem;
|
||||
}
|
||||
|
||||
int hosted_mempool_free(hosted_mempool_t *mempool, void *mem)
|
||||
{
|
||||
if (!mem) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!mempool) {
|
||||
ESP_LOGE(TAG, "%s: mempool %p is NULL", __func__, mempool);
|
||||
return MEMPOOL_FAIL;
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_CHECK)
|
||||
assert(mempool->heap);
|
||||
assert(mempool->pool);
|
||||
#endif
|
||||
|
||||
return mempool->ops->memblock_put(mempool->pool, mem);
|
||||
}
|
||||
@@ -0,0 +1,516 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF)
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* SPDX-FileContributor: 2019-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
/*
|
||||
* NOTICE: File has been changed from original implementation.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include "mempool_ll.h"
|
||||
#include "freertos/portable.h"
|
||||
|
||||
#if CONFIG_ESP_HOSTED_USE_MEMPOOL
|
||||
|
||||
SemaphoreHandle_t hosted_port_mutex;
|
||||
#define OS_INIT_CRITICAL() (hosted_port_mutex = xSemaphoreCreateMutex())
|
||||
#define OS_ENTER_CRITICAL() (xSemaphoreTake((hosted_port_mutex), portMAX_DELAY))
|
||||
#define OS_EXIT_CRITICAL() (xSemaphoreGive((hosted_port_mutex)))
|
||||
|
||||
#define OS_MEM_TRUE_BLOCK_SIZE(bsize) OS_ALIGN(bsize, OS_ALIGNMENT)
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_GUARD)
|
||||
#define OS_MEMPOOL_TRUE_BLOCK_SIZE(mp) \
|
||||
(((mp)->mp_flags & OS_MEMPOOL_F_EXT) ? \
|
||||
OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size) : \
|
||||
(OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size) + sizeof(os_membuf_t)))
|
||||
#else
|
||||
#define OS_MEMPOOL_TRUE_BLOCK_SIZE(mp) OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size)
|
||||
#endif
|
||||
|
||||
static STAILQ_HEAD(, os_mempool) g_os_mempool_list =
|
||||
STAILQ_HEAD_INITIALIZER(g_os_mempool_list);
|
||||
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_POISON)
|
||||
static uint32_t os_mem_poison = 0xde7ec7ed;
|
||||
|
||||
static_assert(sizeof(struct os_memblock) % 4 == 0, "sizeof(struct os_memblock) shall be aligned to 4");
|
||||
static_assert(sizeof(os_mem_poison) == 4, "sizeof(os_mem_poison) shall be 4");
|
||||
|
||||
static void
|
||||
os_mempool_poison(const struct os_mempool *mp, void *start)
|
||||
{
|
||||
uint32_t *p;
|
||||
uint32_t *end;
|
||||
int sz;
|
||||
|
||||
sz = OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size);
|
||||
p = start;
|
||||
end = p + sz / 4;
|
||||
p += sizeof(struct os_memblock) / 4;
|
||||
|
||||
while (p < end) {
|
||||
*p = os_mem_poison;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
os_mempool_poison_check(const struct os_mempool *mp, void *start)
|
||||
{
|
||||
uint32_t *p;
|
||||
uint32_t *end;
|
||||
int sz;
|
||||
|
||||
sz = OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size);
|
||||
p = start;
|
||||
end = p + sz / 4;
|
||||
p += sizeof(struct os_memblock) / 4;
|
||||
|
||||
while (p < end) {
|
||||
assert(*p == os_mem_poison);
|
||||
p++;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define os_mempool_poison(mp, start)
|
||||
#define os_mempool_poison_check(mp, start)
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_GUARD)
|
||||
#define OS_MEMPOOL_GUARD_PATTERN 0xBAFF1ED1
|
||||
|
||||
static void
|
||||
os_mempool_guard(const struct os_mempool *mp, void *start)
|
||||
{
|
||||
uint32_t *tgt;
|
||||
|
||||
if ((mp->mp_flags & OS_MEMPOOL_F_EXT) == 0) {
|
||||
tgt = (uint32_t *)((uintptr_t)start +
|
||||
OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size));
|
||||
*tgt = OS_MEMPOOL_GUARD_PATTERN;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
os_mempool_guard_check(const struct os_mempool *mp, void *start)
|
||||
{
|
||||
uint32_t *tgt;
|
||||
|
||||
if ((mp->mp_flags & OS_MEMPOOL_F_EXT) == 0) {
|
||||
tgt = (uint32_t *)((uintptr_t)start +
|
||||
OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size));
|
||||
assert(*tgt == OS_MEMPOOL_GUARD_PATTERN);
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define os_mempool_guard(mp, start)
|
||||
#define os_mempool_guard_check(mp, start)
|
||||
#endif
|
||||
|
||||
static os_error_t
|
||||
os_mempool_init_internal(struct os_mempool *mp, uint16_t blocks,
|
||||
uint32_t block_size, void *membuf, char *name,
|
||||
uint8_t flags)
|
||||
{
|
||||
int true_block_size;
|
||||
int i;
|
||||
uint8_t *block_addr;
|
||||
struct os_memblock *block_ptr;
|
||||
|
||||
/* Check for valid parameters */
|
||||
if (!mp || (block_size == 0)) {
|
||||
return OS_INVALID_PARM;
|
||||
}
|
||||
|
||||
if ((!membuf) && (blocks != 0)) {
|
||||
return OS_INVALID_PARM;
|
||||
}
|
||||
OS_INIT_CRITICAL();
|
||||
|
||||
if (membuf != NULL) {
|
||||
/* Blocks need to be sized properly and memory buffer should be
|
||||
* aligned
|
||||
*/
|
||||
if (((uintptr_t)membuf & (OS_ALIGNMENT - 1)) != 0) {
|
||||
return OS_MEM_NOT_ALIGNED;
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize the memory pool structure */
|
||||
mp->mp_block_size = block_size;
|
||||
mp->mp_num_free = blocks;
|
||||
mp->mp_min_free = blocks;
|
||||
mp->mp_flags = flags;
|
||||
mp->mp_num_blocks = blocks;
|
||||
mp->mp_membuf_addr = (uintptr_t)membuf;
|
||||
mp->name = name;
|
||||
SLIST_FIRST(mp) = membuf;
|
||||
|
||||
if (blocks > 0) {
|
||||
os_mempool_poison(mp, membuf);
|
||||
os_mempool_guard(mp, membuf);
|
||||
true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp);
|
||||
|
||||
/* Chain the memory blocks to the free list */
|
||||
block_addr = (uint8_t *)membuf;
|
||||
block_ptr = (struct os_memblock *)block_addr;
|
||||
for (i = 1; i < blocks; i++) {
|
||||
block_addr += true_block_size;
|
||||
os_mempool_poison(mp, block_addr);
|
||||
os_mempool_guard(mp, block_addr);
|
||||
SLIST_NEXT(block_ptr, mb_next) = (struct os_memblock *)block_addr;
|
||||
block_ptr = (struct os_memblock *)block_addr;
|
||||
}
|
||||
|
||||
/* Last one in the list should be NULL */
|
||||
SLIST_NEXT(block_ptr, mb_next) = NULL;
|
||||
}
|
||||
|
||||
STAILQ_INSERT_TAIL(&g_os_mempool_list, mp, mp_list);
|
||||
|
||||
return OS_OK;
|
||||
}
|
||||
|
||||
static os_error_t
|
||||
os_mempool_init(struct os_mempool *mp, uint16_t blocks, uint32_t block_size,
|
||||
void *membuf, char *name)
|
||||
{
|
||||
return os_mempool_init_internal(mp, blocks, block_size, membuf, name, 0);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static os_error_t
|
||||
os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks,
|
||||
uint32_t block_size, void *membuf, char *name)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = os_mempool_init_internal(&mpe->mpe_mp, blocks, block_size, membuf,
|
||||
name, OS_MEMPOOL_F_EXT);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
mpe->mpe_put_cb = NULL;
|
||||
mpe->mpe_put_arg = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static os_error_t
|
||||
os_mempool_unregister(struct os_mempool *mp)
|
||||
{
|
||||
struct os_mempool *prev;
|
||||
struct os_mempool *next;
|
||||
struct os_mempool *cur;
|
||||
|
||||
/* Remove the mempool from the global stailq. This is done manually rather
|
||||
* than with `STAILQ_REMOVE` to allow for a graceful failure if the mempool
|
||||
* isn't found.
|
||||
*/
|
||||
|
||||
prev = NULL;
|
||||
STAILQ_FOREACH(cur, &g_os_mempool_list, mp_list) {
|
||||
if (cur == mp) {
|
||||
break;
|
||||
}
|
||||
prev = cur;
|
||||
}
|
||||
|
||||
if (cur == NULL) {
|
||||
return OS_INVALID_PARM;
|
||||
}
|
||||
|
||||
if (prev == NULL) {
|
||||
STAILQ_REMOVE_HEAD(&g_os_mempool_list, mp_list);
|
||||
} else {
|
||||
next = STAILQ_NEXT(cur, mp_list);
|
||||
if (next == NULL) {
|
||||
g_os_mempool_list.stqh_last = &STAILQ_NEXT(prev, mp_list);
|
||||
}
|
||||
|
||||
STAILQ_NEXT(prev, mp_list) = next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static os_error_t
|
||||
os_mempool_clear(struct os_mempool *mp)
|
||||
{
|
||||
struct os_memblock *block_ptr;
|
||||
int true_block_size;
|
||||
uint8_t *block_addr;
|
||||
uint16_t blocks;
|
||||
|
||||
if (!mp) {
|
||||
return OS_INVALID_PARM;
|
||||
}
|
||||
|
||||
true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp);
|
||||
|
||||
/* cleanup the memory pool structure */
|
||||
mp->mp_num_free = mp->mp_num_blocks;
|
||||
mp->mp_min_free = mp->mp_num_blocks;
|
||||
os_mempool_poison(mp, (void *)mp->mp_membuf_addr);
|
||||
os_mempool_guard(mp, (void *)mp->mp_membuf_addr);
|
||||
SLIST_FIRST(mp) = (void *)mp->mp_membuf_addr;
|
||||
|
||||
/* Chain the memory blocks to the free list */
|
||||
block_addr = (uint8_t *)mp->mp_membuf_addr;
|
||||
block_ptr = (struct os_memblock *)block_addr;
|
||||
blocks = mp->mp_num_blocks;
|
||||
|
||||
while (blocks > 1) {
|
||||
block_addr += true_block_size;
|
||||
os_mempool_poison(mp, block_addr);
|
||||
os_mempool_guard(mp, block_addr);
|
||||
SLIST_NEXT(block_ptr, mb_next) = (struct os_memblock *)block_addr;
|
||||
block_ptr = (struct os_memblock *)block_addr;
|
||||
--blocks;
|
||||
}
|
||||
|
||||
/* Last one in the list should be NULL */
|
||||
SLIST_NEXT(block_ptr, mb_next) = NULL;
|
||||
|
||||
return OS_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
os_memblock_from(const struct os_mempool *mp, const void *block_addr)
|
||||
{
|
||||
uint32_t true_block_size;
|
||||
uintptr_t baddr;
|
||||
uintptr_t end;
|
||||
|
||||
baddr = (uintptr_t)block_addr;
|
||||
true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp);
|
||||
end = mp->mp_membuf_addr + (mp->mp_num_blocks * true_block_size);
|
||||
|
||||
/* Check that the block is in the memory buffer range. */
|
||||
if ((baddr < mp->mp_membuf_addr) || (baddr >= end)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* All freed blocks should be on true block size boundaries! */
|
||||
if (((baddr - mp->mp_membuf_addr) % true_block_size) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool
|
||||
os_mempool_is_sane(const struct os_mempool *mp)
|
||||
{
|
||||
struct os_memblock *block;
|
||||
|
||||
/* Verify that each block in the free list belongs to the mempool. */
|
||||
SLIST_FOREACH(block, mp, mb_next) {
|
||||
if (!os_memblock_from(mp, block)) {
|
||||
return false;
|
||||
}
|
||||
os_mempool_poison_check(mp, block);
|
||||
os_mempool_guard_check(mp, block);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void *
|
||||
os_memblock_get(struct os_mempool *mp)
|
||||
{
|
||||
struct os_memblock *block;
|
||||
|
||||
/* Check to make sure they passed in a memory pool (or something) */
|
||||
block = NULL;
|
||||
if (mp) {
|
||||
OS_ENTER_CRITICAL();
|
||||
/* Check for any free */
|
||||
if (mp->mp_num_free) {
|
||||
/* Get a free block */
|
||||
block = SLIST_FIRST(mp);
|
||||
|
||||
/* Set new free list head */
|
||||
SLIST_FIRST(mp) = SLIST_NEXT(block, mb_next);
|
||||
|
||||
/* Decrement number free by 1 */
|
||||
mp->mp_num_free--;
|
||||
if (mp->mp_min_free > mp->mp_num_free) {
|
||||
mp->mp_min_free = mp->mp_num_free;
|
||||
}
|
||||
}
|
||||
OS_EXIT_CRITICAL();
|
||||
|
||||
if (block) {
|
||||
os_mempool_poison_check(mp, block);
|
||||
os_mempool_guard_check(mp, block);
|
||||
}
|
||||
}
|
||||
|
||||
return (void *)block;
|
||||
}
|
||||
|
||||
static os_error_t
|
||||
os_memblock_put_from_cb(struct os_mempool *mp, void *block_addr)
|
||||
{
|
||||
struct os_memblock *block;
|
||||
|
||||
os_mempool_guard_check(mp, block_addr);
|
||||
os_mempool_poison(mp, block_addr);
|
||||
|
||||
block = (struct os_memblock *)block_addr;
|
||||
OS_ENTER_CRITICAL();
|
||||
|
||||
/* Chain current free list pointer to this block; make this block head */
|
||||
SLIST_NEXT(block, mb_next) = SLIST_FIRST(mp);
|
||||
SLIST_FIRST(mp) = block;
|
||||
|
||||
/* XXX: Should we check that the number free <= number blocks? */
|
||||
/* Increment number free */
|
||||
mp->mp_num_free++;
|
||||
|
||||
OS_EXIT_CRITICAL();
|
||||
|
||||
return OS_OK;
|
||||
}
|
||||
|
||||
static os_error_t
|
||||
os_memblock_put(struct os_mempool *mp, void *block_addr)
|
||||
{
|
||||
struct os_mempool_ext *mpe;
|
||||
os_error_t ret;
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_CHECK)
|
||||
struct os_memblock *block;
|
||||
int sr;
|
||||
#endif
|
||||
|
||||
/* Make sure parameters are valid */
|
||||
if ((mp == NULL) || (block_addr == NULL)) {
|
||||
ret = OS_INVALID_PARM;
|
||||
goto done;
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_CHECK)
|
||||
/* Check that the block we are freeing is a valid block! */
|
||||
assert(os_memblock_from(mp, block_addr));
|
||||
|
||||
/*
|
||||
* Check for duplicate free.
|
||||
*/
|
||||
OS_ENTER_CRITICAL();
|
||||
SLIST_FOREACH(block, mp, mb_next) {
|
||||
assert(block != (struct os_memblock *)block_addr);
|
||||
}
|
||||
OS_EXIT_CRITICAL();
|
||||
|
||||
#endif
|
||||
/* If this is an extended mempool with a put callback, call the callback
|
||||
* instead of freeing the block directly.
|
||||
*/
|
||||
if (mp->mp_flags & OS_MEMPOOL_F_EXT) {
|
||||
mpe = (struct os_mempool_ext *)mp;
|
||||
if (mpe->mpe_put_cb != NULL) {
|
||||
ret = mpe->mpe_put_cb(mpe, block_addr, mpe->mpe_put_arg);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
/* No callback; free the block. */
|
||||
ret = os_memblock_put_from_cb(mp, block_addr);
|
||||
|
||||
done:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static struct os_mempool *
|
||||
os_mempool_info_get_next(struct os_mempool *mp, struct os_mempool_info *omi)
|
||||
{
|
||||
struct os_mempool *cur;
|
||||
|
||||
if (mp == NULL) {
|
||||
cur = STAILQ_FIRST(&g_os_mempool_list);
|
||||
} else {
|
||||
cur = STAILQ_NEXT(mp, mp_list);
|
||||
}
|
||||
|
||||
if (cur == NULL) {
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
omi->omi_block_size = cur->mp_block_size;
|
||||
omi->omi_num_blocks = cur->mp_num_blocks;
|
||||
omi->omi_num_free = cur->mp_num_free;
|
||||
omi->omi_min_free = cur->mp_min_free;
|
||||
omi->omi_name[0] = '\0';
|
||||
strncat(omi->omi_name, cur->name, sizeof(omi->omi_name) - 1);
|
||||
|
||||
return (cur);
|
||||
}
|
||||
|
||||
static struct os_mempool *
|
||||
os_mempool_get(const char *mempool_name, struct os_mempool_info *info)
|
||||
{
|
||||
struct os_mempool *mp;
|
||||
|
||||
mp = STAILQ_FIRST(&g_os_mempool_list);
|
||||
while (mp) {
|
||||
if (strcmp(mempool_name, mp->name) == 0) {
|
||||
break;
|
||||
}
|
||||
mp = STAILQ_NEXT(mp, mp_list);
|
||||
}
|
||||
|
||||
if (mp != NULL && info != NULL) {
|
||||
info->omi_block_size = mp->mp_block_size;
|
||||
info->omi_num_blocks = mp->mp_num_blocks;
|
||||
info->omi_num_free = mp->mp_num_free;
|
||||
info->omi_min_free = mp->mp_min_free;
|
||||
info->omi_name[0] = '\0';
|
||||
strncat(info->omi_name, mp->name, sizeof(info->omi_name) - 1);
|
||||
}
|
||||
|
||||
return mp;
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct mempool_ops_t opts = {
|
||||
.mempool_init = os_mempool_init,
|
||||
.mempool_unregister = os_mempool_unregister,
|
||||
.memblock_get = os_memblock_get,
|
||||
.memblock_put = os_memblock_put,
|
||||
};
|
||||
|
||||
struct mempool_ops_t * os_mempool_get_ops(void)
|
||||
{
|
||||
return &opts;
|
||||
}
|
||||
|
||||
#endif // CONFIG_ESP_HOSTED_USE_MEMPOOL
|
||||
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF)
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* SPDX-FileContributor: 2019-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
/*
|
||||
* NOTICE: File has been changed from original implementation.
|
||||
*/
|
||||
|
||||
#ifndef _OS_MEMPOOL_H_
|
||||
#define _OS_MEMPOOL_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "sys/queue.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#if CONFIG_ESP_HOSTED_USE_MEMPOOL
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MYNEWT_VAL(_name) MYNEWT_VAL_ ## _name
|
||||
|
||||
#define OS_ALIGN(__n, __a) ( \
|
||||
(((__n) & ((__a) - 1)) == 0) ? \
|
||||
(__n) : \
|
||||
((__n) + ((__a) - ((__n) & ((__a) - 1)))) \
|
||||
)
|
||||
#define OS_ALIGNMENT 4
|
||||
|
||||
enum os_error {
|
||||
OS_OK = 0,
|
||||
OS_ENOMEM = 1,
|
||||
OS_EINVAL = 2,
|
||||
OS_INVALID_PARM = 3,
|
||||
OS_MEM_NOT_ALIGNED = 4,
|
||||
OS_BAD_MUTEX = 5,
|
||||
OS_TIMEOUT = 6,
|
||||
OS_ERR_IN_ISR = 7, /* Function cannot be called from ISR */
|
||||
OS_ERR_PRIV = 8, /* Privileged access error */
|
||||
OS_NOT_STARTED = 9, /* OS must be started to call this function, but isn't */
|
||||
OS_ENOENT = 10, /* No such thing */
|
||||
OS_EBUSY = 11, /* Resource busy */
|
||||
OS_ERROR = 12, /* Generic Error */
|
||||
};
|
||||
|
||||
typedef enum os_error os_error_t;
|
||||
|
||||
/**
|
||||
* A memory block structure. This simply contains a pointer to the free list
|
||||
* chain and is only used when the block is on the free list. When the block
|
||||
* has been removed from the free list the entire memory block is usable by the
|
||||
* caller.
|
||||
*/
|
||||
struct os_memblock {
|
||||
/** Next memory block in the list. */
|
||||
SLIST_ENTRY(os_memblock) mb_next;
|
||||
};
|
||||
|
||||
/* XXX: Change this structure so that we keep the first address in the pool? */
|
||||
/* XXX: add memory debug structure and associated code */
|
||||
/* XXX: Change how I coded the SLIST_HEAD here. It should be named:
|
||||
SLIST_HEAD(,os_memblock) mp_head; */
|
||||
|
||||
/**
|
||||
* Memory pool
|
||||
*/
|
||||
struct os_mempool {
|
||||
/** Size of the memory blocks, in bytes. */
|
||||
uint32_t mp_block_size;
|
||||
/** The number of memory blocks. */
|
||||
uint16_t mp_num_blocks;
|
||||
/** The number of free blocks left */
|
||||
uint16_t mp_num_free;
|
||||
/** The lowest number of free blocks seen */
|
||||
uint16_t mp_min_free;
|
||||
/** Bitmap of OS_MEMPOOL_F_[...] values. */
|
||||
uint8_t mp_flags;
|
||||
/** Address of memory buffer used by pool */
|
||||
uintptr_t mp_membuf_addr;
|
||||
/** Next memory pool in the list. */
|
||||
STAILQ_ENTRY(os_mempool) mp_list;
|
||||
/** Head of the list of memory blocks. */
|
||||
SLIST_HEAD(,os_memblock);
|
||||
/** Name for memory block */
|
||||
char *name;
|
||||
};
|
||||
|
||||
/**
|
||||
* Indicates an extended mempool. Address can be safely cast to
|
||||
* (struct os_mempool_ext *).
|
||||
*/
|
||||
#define OS_MEMPOOL_F_EXT 0x01
|
||||
|
||||
struct os_mempool_ext;
|
||||
|
||||
/**
|
||||
* Block put callback function. If configured, this callback gets executed
|
||||
* whenever a block is freed to the corresponding extended mempool. Note: The
|
||||
* os_memblock_put() function calls this callback instead of freeing the block
|
||||
* itself. Therefore, it is the callback's responsibility to free the block
|
||||
* via a call to os_memblock_put_from_cb().
|
||||
*
|
||||
* @param ome The extended mempool that a block is being
|
||||
* freed back to.
|
||||
* @param data The block being freed.
|
||||
* @param arg Optional argument configured along with the
|
||||
* callback.
|
||||
*
|
||||
* @return Indicates whether the block was successfully
|
||||
* freed. A non-zero value should only be
|
||||
* returned if the block was not successfully
|
||||
* released back to its pool.
|
||||
*/
|
||||
typedef os_error_t os_mempool_put_fn(struct os_mempool_ext *ome, void *data,
|
||||
void *arg);
|
||||
|
||||
/** Extended memory pool. */
|
||||
struct os_mempool_ext {
|
||||
/** Standard memory pool. */
|
||||
struct os_mempool mpe_mp;
|
||||
|
||||
/** Callback that is executed immediately when a block is freed. */
|
||||
os_mempool_put_fn *mpe_put_cb;
|
||||
|
||||
/** Optional argument passed to the callback function. */
|
||||
void *mpe_put_arg;
|
||||
};
|
||||
|
||||
/** Length of the name of memory pool */
|
||||
#define OS_MEMPOOL_INFO_NAME_LEN (32)
|
||||
|
||||
/**
|
||||
* Information describing a memory pool, used to return OS information
|
||||
* to the management layer.
|
||||
*/
|
||||
struct os_mempool_info {
|
||||
/** Size of the memory blocks in the pool */
|
||||
int omi_block_size;
|
||||
/** Number of memory blocks in the pool */
|
||||
int omi_num_blocks;
|
||||
/** Number of free memory blocks */
|
||||
int omi_num_free;
|
||||
/** Minimum number of free memory blocks ever */
|
||||
int omi_min_free;
|
||||
/** Name of the memory pool */
|
||||
char omi_name[OS_MEMPOOL_INFO_NAME_LEN];
|
||||
};
|
||||
|
||||
/*
|
||||
* To calculate size of the memory buffer needed for the pool. NOTE: This size
|
||||
* is NOT in bytes! The size is the number of os_membuf_t elements required for
|
||||
* the memory pool.
|
||||
*/
|
||||
#if MYNEWT_VAL(OS_MEMPOOL_GUARD)
|
||||
/** Leave extra 4 bytes of guard area at the end. */
|
||||
#define OS_MEMPOOL_BLOCK_SZ(sz) ((sz) + sizeof(os_membuf_t))
|
||||
#else
|
||||
/** Size of a memory pool block. */
|
||||
#define OS_MEMPOOL_BLOCK_SZ(sz) (sz)
|
||||
#endif
|
||||
#if (OS_ALIGNMENT == 4)
|
||||
typedef uint32_t os_membuf_t;
|
||||
#elif (OS_ALIGNMENT == 8)
|
||||
typedef uint64_t os_membuf_t;
|
||||
#elif (OS_ALIGNMENT == 16)
|
||||
typedef __uint128_t os_membuf_t;
|
||||
#else
|
||||
#error "Unhandled `OS_ALIGNMENT` for `os_membuf_t`"
|
||||
#endif /* OS_ALIGNMENT == * */
|
||||
|
||||
/** The total size of a memory pool, including alignment. */
|
||||
#define OS_MEMPOOL_SIZE(n,blksize) (((OS_MEMPOOL_BLOCK_SZ(blksize) + ((OS_ALIGNMENT)-1)) / (OS_ALIGNMENT)) * (n))
|
||||
|
||||
/** Calculates the number of bytes required to initialize a memory pool. */
|
||||
#define OS_MEMPOOL_BYTES(n,blksize) \
|
||||
(sizeof (os_membuf_t) * OS_MEMPOOL_SIZE((n), (blksize)))
|
||||
|
||||
struct mempool_ops_t {
|
||||
os_error_t (*mempool_init)(struct os_mempool *mp, uint16_t blocks, uint32_t block_size, void *membuf, char *name);
|
||||
os_error_t (*mempool_unregister)(struct os_mempool *mp);
|
||||
void * (*memblock_get)(struct os_mempool *mp);
|
||||
os_error_t (*memblock_put)(struct os_mempool *mp, void *block_addr);
|
||||
};
|
||||
|
||||
struct mempool_ops_t * os_mempool_get_ops(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // CONFIG_ESP_HOSTED_USE_MEMPOOL
|
||||
|
||||
#endif /* _OS_MEMPOOL_H_ */
|
||||
Reference in New Issue
Block a user