/* * nc_ptr_check: validate that instruction pointer is not out of range. * If not - advance by number of arguments and fetch specified argument. */ static int nc_ptr_check(uintptr_t *iptr, const void *nc, size_t sz, u_int nargs, uint32_t *val, u_int r) { const uint32_t *tptr = (const uint32_t *)*iptr; u_int i; KASSERT(ALIGNED_POINTER(*iptr, uint32_t)); KASSERT(nargs > 0); if ((uintptr_t)tptr < (uintptr_t)nc) return NPF_ERR_JUMP; if ((uintptr_t)tptr + (nargs * sizeof(uint32_t)) > (uintptr_t)nc + sz) return NPF_ERR_RANGE; for (i = 1; i <= nargs; i++) { if (val && i == r) { *val = *tptr; } tptr++; } *iptr = (uintptr_t)tptr; return 0; }
static void stack_capture(struct stack *st, struct frame *frame) { struct frame *fp; vm_offset_t callpc; stack_zero(st); fp = frame; for (;;) { if (!INKERNEL((vm_offset_t)fp) || !ALIGNED_POINTER(fp, uint64_t)) break; callpc = fp->fr_pc; if (!INKERNEL(callpc)) break; /* Don't bother traversing trap frames. */ if ((callpc > (uint64_t)tl_trap_begin && callpc < (uint64_t)tl_trap_end) || (callpc > (uint64_t)tl_text_begin && callpc < (uint64_t)tl_text_end)) break; if (stack_put(st, callpc) == -1) break; if (v9next_frame(fp) <= fp || v9next_frame(fp) >= frame + KSTACK_PAGES * PAGE_SIZE) break; fp = v9next_frame(fp); } }
TEST dct_speed(const int width) { const int size = width * width; uint64_t call_cnt = 0; dct_func * tested_func = test_env.strategy->fptr; KVZ_CLOCK_T clock_now; KVZ_GET_TIME(&clock_now); double test_end = KVZ_CLOCK_T_AS_DOUBLE(clock_now) + TIME_PER_TEST; int16_t _tmp_residual[32 * 32 + SIMD_ALIGNMENT]; int16_t _tmp_coeffs[32 * 32 + SIMD_ALIGNMENT]; int16_t *tmp_residual = ALIGNED_POINTER(_tmp_residual, SIMD_ALIGNMENT); int16_t *tmp_coeffs = ALIGNED_POINTER(_tmp_coeffs, SIMD_ALIGNMENT); // Loop until time allocated for test has passed. for (unsigned i = 0; test_end > KVZ_CLOCK_T_AS_DOUBLE(clock_now); ++i) { int test = i % NUM_TESTS; uint64_t sum = 0; for (int offset = 0; offset < NUM_CHUNKS * 64 * 64; offset += NUM_CHUNKS * size) { // Compare the first chunk against the 35 other chunks to simulate real usage. for (int chunk = 0; chunk < NUM_CHUNKS; ++chunk) { kvz_pixel * buf1 = &bufs[test][offset]; kvz_pixel * buf2 = &bufs[test][chunk * size + offset]; for (int p = 0; p < size; ++p) { tmp_residual[p] = (int16_t)(buf1[p] - buf2[p]); } tested_func(8, tmp_residual, tmp_coeffs); ++call_cnt; sum += tmp_coeffs[0]; } } ASSERT(sum > 0); KVZ_GET_TIME(&clock_now) } sprintf(test_env.msg, "%.3fM x %s:%s", (double)call_cnt / 1000000.0, test_env.strategy->type, test_env.strategy->strategy_name); PASSm(test_env.msg); }
/* * nc_fetch_word: fetch a word (32 bits) from the n-code and increase * instruction pointer by one word. */ static inline const void * nc_fetch_word(const void *iptr, uint32_t *a) { const uint32_t *tptr = (const uint32_t *)iptr; KASSERT(ALIGNED_POINTER(iptr, uint32_t)); *a = *tptr++; return tptr; }
/* * Receive incoming data on our hook. Send it out the socket. */ static int ng_ksocket_rcvdata(hook_p hook, item_p item) { struct thread *td = curthread; /* XXX broken */ const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); struct socket *const so = priv->so; struct sockaddr *sa = NULL; int error; struct mbuf *m; #ifdef ALIGNED_POINTER struct mbuf *n; #endif /* ALIGNED_POINTER */ struct sa_tag *stag; /* Extract data */ NGI_GET_M(item, m); NG_FREE_ITEM(item); #ifdef ALIGNED_POINTER if (!ALIGNED_POINTER(mtod(m, caddr_t), uint32_t)) { n = m_defrag(m, M_NOWAIT); if (n == NULL) { m_freem(m); return (ENOBUFS); } m = n; } #endif /* ALIGNED_POINTER */ /* * Look if socket address is stored in packet tags. * If sockaddr is ours, or provided by a third party (zero id), * then we accept it. */ if (((stag = (struct sa_tag *)m_tag_locate(m, NGM_KSOCKET_COOKIE, NG_KSOCKET_TAG_SOCKADDR, NULL)) != NULL) && (stag->id == NG_NODE_ID(node) || stag->id == 0)) sa = &stag->sa; /* Reset specific mbuf flags to prevent addressing problems. */ m->m_flags &= ~(M_BCAST|M_MCAST); /* Send packet */ error = sosend(so, sa, 0, m, 0, 0, td); return (error); }
static void setup_tests() { for (int test = 0; test < NUM_TESTS; ++test) { unsigned size = NUM_CHUNKS * 64 * 64; actual_bufs[test] = malloc(size * sizeof(kvz_pixel) + SIMD_ALIGNMENT); bufs[test] = ALIGNED_POINTER(actual_bufs[test], SIMD_ALIGNMENT); } for (int test = 0; test < NUM_TESTS; ++test) { for (int chunk = 0; chunk < NUM_CHUNKS; ++chunk) { const int width = 64; int x = (test + chunk) % width; int y = (test + chunk) / width; init_gradient(width - x, y, width, 255 / width, &bufs[test][chunk * 64*64]); } } }
static void readisaac (struct isaac *isaac, void *p, size_t size) { size_t inbytes = isaac->buffered; while (true) { char *char_p = p; if (size <= inbytes) { memcpy (p, isaac->data.b + ISAAC_BYTES - inbytes, size); isaac->buffered = inbytes - size; return; } memcpy (p, isaac->data.b + ISAAC_BYTES - inbytes, inbytes); p = char_p + inbytes; size -= inbytes; /* If P is aligned, write to *P directly to avoid the overhead of copying from the buffer. */ if (ALIGNED_POINTER (p, isaac_word)) { isaac_word *wp = p; while (ISAAC_BYTES <= size) { isaac_refill (&isaac->state, wp); wp += ISAAC_WORDS; size -= ISAAC_BYTES; if (size == 0) { isaac->buffered = 0; return; } } p = wp; } isaac_refill (&isaac->state, isaac->data.w); inbytes = ISAAC_BYTES; } }
/* flags: fseq: contain SEQ field, fack: contain ACK field */ static int pptp_call_gre_output(pptp_call *_this, int fseq, int fack, u_char *pkt, int lpkt) { int sz; struct pptp_gre_header *grehdr; u_char buf[65535], *opkt; struct sockaddr_storage peer, sock; #ifndef USE_LIBSOCKUTIL socklen_t peerlen; #endif memset(buf, 0, sizeof(buf)); opkt = buf; grehdr = (struct pptp_gre_header *)opkt; opkt += sizeof(struct pptp_gre_header); /* GRE header */ grehdr->K = 1; grehdr->ver = PPTP_GRE_VERSION; grehdr->protocol_type = htons(PPTP_GRE_PROTOCOL_TYPE); grehdr->payload_length = htons(lpkt); grehdr->call_id = htons(_this->peers_call_id); #ifdef PPTP_CALL_DEBUG if (debuglevel >= 2 && (fseq || fack)) { pptp_call_log(_this, LOG_DEBUG, "Sending data packet seq=%u ack=%u", _this->snd_nxt, _this->rcv_nxt - 1); } #endif PPTP_CALL_ASSERT(ALIGNED_POINTER(opkt, uint32_t)); if (fseq) { grehdr->S = 1; *(uint32_t *)opkt = htonl(_this->snd_nxt++); opkt += 4; } if (fack) { grehdr->A = 1; _this->rcv_acked = _this->rcv_nxt; *(uint32_t *)opkt = htonl(_this->rcv_nxt - 1); opkt += 4; } if (lpkt > 0) { memcpy(opkt, pkt, lpkt); opkt += lpkt; } memcpy(&peer, &_this->ctrl->peer, sizeof(peer)); memcpy(&sock, &_this->ctrl->our, sizeof(sock)); switch (peer.ss_family) { case AF_INET: ((struct sockaddr_in *)&peer)->sin_port = 0; ((struct sockaddr_in *)&sock)->sin_port = 0; #ifndef USE_LIBSOCKUTIL peerlen = sizeof(struct sockaddr_in); #endif break; default: return 1; } if (PPTP_CTRL_CONF(_this->ctrl)->data_out_pktdump != 0) { pptp_call_log(_this, LOG_DEBUG, "PPTP Data output packet dump"); show_hd(debug_get_debugfp(), buf, opkt - buf); } #ifdef USE_LIBSOCKUTIL sz = sendfromto(pptp_ctrl_sock_gre(_this->ctrl), buf, opkt - buf, 0, (struct sockaddr *)&sock, (struct sockaddr *)&peer); #else sz = sendto(pptp_ctrl_sock_gre(_this->ctrl), buf, opkt - buf, 0, (struct sockaddr *)&peer, peerlen); #endif if (sz <= 0) pptp_call_log(_this, LOG_WARNING, "sendto(%d) failed: %m", pptp_ctrl_sock_gre(_this->ctrl)); return (sz > 0)? 0 : 1; }
static void setup_tests() { for (int test = 0; test < NUM_TESTS; ++test) { dct_actual_bufs[test] = malloc(LCU_WIDTH*LCU_WIDTH*sizeof(int16_t) + SIMD_ALIGNMENT); dct_bufs[test] = ALIGNED_POINTER(dct_actual_bufs[test], SIMD_ALIGNMENT); } for (int test = 0; test < NUM_TESTS; ++test) { const int width = LCU_WIDTH; init_gradient(width, width, width, 255 / width, dct_bufs[test]); } // Select buffer width according to function name for dct function. int block = 0; for (int s = 0; s < strategies.count && block < NUM_SIZES; ++s) { strategy_t *strat = &strategies.strategies[s]; dct_func* dct_generic = 0; if ( ( strcmp(strat->type, "fast_forward_dst_4x4") == 0 || strcmp(strat->type, "dct_4x4") == 0 || strcmp(strat->type, "dct_8x8") == 0 || strcmp(strat->type, "dct_16x16") == 0 || strcmp(strat->type, "dct_32x32") == 0 ) && strcmp(strat->strategy_name, "generic") == 0 ) { dct_generic = strat->fptr; dct_generic(KVZ_BIT_DEPTH, dct_bufs[block], dct_result[block]); ++block; } } block = 0; for (int s = 0; s < strategies.count && block < NUM_SIZES; ++s) { strategy_t *strat = &strategies.strategies[s]; dct_func* idct_generic = 0; if ( ( strcmp(strat->type, "fast_inverse_dst_4x4") == 0 || strcmp(strat->type, "idct_4x4") == 0 || strcmp(strat->type, "idct_8x8") == 0 || strcmp(strat->type, "idct_16x16") == 0 || strcmp(strat->type, "idct_32x32") == 0 ) && strcmp(strat->strategy_name, "generic") == 0 ) { idct_generic = strat->fptr; idct_generic(KVZ_BIT_DEPTH, dct_bufs[block], idct_result[block]); ++block; } } }